Qt 5 Get QListWidget Selected Item

The class variables are used to demonstrate object orientation where
several class methods can operate on the class data (only having to
read it once per class instance). The size of the variables demonstrate
how to handle large text files, with thousands of lines, without
having to demonstrate the use of pointers or vectors. The size of the
variables are not significant if the class instance is brief and not
persistent, and not too many class instances occur at the same time.

The following example gets the selected inputs from 9 QListWidgets:

month, day, year, and 6 numbers all less than 100 in value.

It then offers to update a file by the web,
or update the file from the widgets’ selected values.

Procedure flow:

1) Offer to update the file from the web.
2) Get QListWidgetItem values from the selected QLisiWidgets.
3) Checks for non-selected values of all the QListWidgetItem values.
4) Get string values from the selected QListWidgetItem values.
5) Get integer values from those string values.
6) Output the values to the text file in a formatted style,
using the integer values to format the output,
then outputting using a string.
7) Update the date and total lines of the text file to two application text boxes.

For the header file, ‘linedate’ and ‘numbers’ are included as an example
reference, and are given values from reading a text file, for example.
They could be updated here if chosen, so as not have to read the text
file again for further use. Those values, plus linestotal are read from
an input text file in another procedure.

Myclass.h:

#include <QString>
#include <QTextEdit>
#include <QListWidget>
#include <QLineEdit>

class Myclass
{

    public:

    QString linedate [3000];
    int numbers [3000][6];
    int linestotal;

    public:

    void update(QListWidget *month, QListWidget *day,
    QListWidget *year, QListWidget *num1, QListWidget *num2,
    QListWidget *num3, QListWidget *num4, QListWidget *num5,
    QListWidget *num6, QTextEdit *t1, QTextEdit *t2)

};

Myclass.cpp

#include "Myclass.h";
#include <QListWidget>
#include <QListWidgetItem>
#include <QTextEdit>
#include <QFile>
#include <QStandardPaths>
#include <QMessageBox>
#include <QTextStream>

void Myclass::update(QListWidget *month, QListWidget *day,
QListWidget *year, QListWidget *num1, QListWidget *num2,
QListWidget *num3, QListWidget *num4, QListWidget *num5,
QListWidget *num6, QTextEdit *t1, QTextEdit *t2)

{

QListWidgetItem *month_item;
QListWidgetItem *day_item;
QListWidgetItem *year_item;
QListWidgetItem *num1_item;
QListWidgetItem *num2_item;
QListWidgetItem *num3_item;
QListWidgetItem *num4_item;
QListWidgetItem *num5_item;
QListWidgetItem *num6_item;
QString month_str;
QString day_str;
QString year_str;
QString num1_str;
QString num2_str;
QString num3_str;
QString num4_str;
QString num5_str;
QString num6_str;
int num1_int;
int num2_int;
int num3_int;
int num4_int;
int num5_int;
int num6_int;
QMessageBox msgBox;
QString path;
QString temp_str1;
QString temp_str2;
bool ok;
QString localPath;

path = QStandardPaths::writableLocation(QStandardPaths::HomeLocation);

path += "/My Documents/myfolder/";

localPath = path;

path += "myfile.txt";

// Offer to update from the web

QMessageBox msgBox2;
msgBox2.setText("You can download the update to the Powerball input file from the web.");
msgBox2.setInformativeText("Do you want to download the file now?");
msgBox2.setStandardButtons(QMessageBox::Yes | QMessageBox::No);
msgBox2.setDefaultButton(QMessageBox::Yes);
int ret = msgBox2.exec();

if (ret == QMessageBox::Yes) {

    // For the downloadFile procedure, check here:
    // https://tomtheprogrammer.wordpress.com/2013/05/27/qt-5-download-a-file/

    downloadFile ("http://mywebsite.com/myfile.txt", localPath);

    msgBox.setText("Your file should be updated now");
    msgBox.exec();

}

// else update the values from the QListWidgets

else {

    QFile outFile (path);

    if (!outFile.open(QIODevice::Append | QIODevice::Text)) {

        msgBox.setText("Unable to open input file");
        msgBox.exec();

        return;

    }

    QTextStream outStream(&outFile);

    // Need to use QListWidgetItems to check for non-selected values

    month_item = month->currentItem();
    day_item = day->currentItem();
    year_item = year->currentItem();
    num1_item = num1->currentItem();
    num2_item = num2->currentItem();
    num3_item = num3->currentItem();
    num4_item = num4->currentItem();
    num5_item = num5->currentItem();
    num6_item = num6->currentItem();

    // Check if all items are selected, if not exit the procedure
    // For Windows applications, use 'nullptr'
    // For Linux applications use 'NULL'

    if ( month_item == nullptr ||
         day_item == nullptr ||
         year_item == nullptr ||
         num1_item == nullptr ||
         num2_item == nullptr ||
         num3_item == nullptr ||
         num4_item == nullptr ||
         num5_item == nullptr ||
         num6_item == nullptr ) {

         msgBox.setText ("You must select all the items.");
         msgBox.exec();

         return;
    }

    // Get string values from the selected widgets

    month_str = month_item->text();
    day_str = day_item->text();
    year_str = year_item->text();
    num1_str = num1_item->text();
    num2_str = num2_item->text();
    num3_str = num3_item->text();
    num4_str = num4_item->text();
    num5_str = num5_item->text();
    num6_str = num6_item->text();

    // Get integer values from those string values

    num1_int = num1_str.toInt(&ok,10);
    num2_int = num2_str.toInt(&ok,10);
    num3_int = num3_str.toInt(&ok,10);
    num4_int = num4_str.toInt(&ok,10);
    num5_int = num5_str.toInt(&ok,10);
    num6_int = num6_str.toInt(&ok,10);

    temp_str1 = month_str + "/" + day_str + "/" + year_str;
    temp_str2 = temp_str1;

    // You could use linedate [linestotal] = temp_str2
    // to update that class attribute

    // Next format and output the values to the chosen text file

    if ( num1_int < 10 )

        temp_str1 += " ";

    temp_str1 += " " + QString::number(num1_int);

    if ( num2_int < 10 )

        temp_str1 += " ";

    temp_str1 += " " + QString::number(num2_int);

    if ( num3_int < 10 )

        temp_str1 += " ";

    temp_str1 += " " + QString::number(num3_int);

    if ( num4_int < 10 )

        temp_str1 += " ";

    temp_str1 += " " + QString::number(num4_int);

    if ( num5_int < 10 )

        temp_str1 += " ";

    temp_str1 += " " + QString::number(num5_int);

    if ( num6_int < 10 )

        temp_str1 += " ";

    temp_str1 += " " + QString::number(xball2_int);

    outStream << temp_str1 << endl;

    outFile.close();

    linestotal++;

    // Update text boxes on the main form with the new values

    temp_str1 = QString::number(linestotal);

    t1->setPlainText(temp_str1);

    t2->setPlainText(temp_str2);

    }

}

Advertisements