0

I am reading a csv file in qt using the following code:

QStandardItemModel* readFile()
{
    QFile config_file(FILE_PATH);

    if (config_file.open(QIODevice::ReadOnly)) {
        int lineindex = 0;
        QTextStream in(&config_file);

        while (!in.atEnd()) {

            QString fileLine = in.readLine();
            QStringList lineToken = fileLine.split(",", QString::SkipEmptyParts);
            //ignore commented lines
            if(!fileLine.startsWith("#", Qt::CaseSensitive)){
                for (int j = 0; j < lineToken.size(); j++) {
                    QString value = lineToken.at(j);
                    QStandardItem *item = new QStandardItem(value);
                    fileContent->setItem(lineindex, j, item);
                }
                lineindex++;
            }
        }
        return fileContent;
        config_file.close();
    }
}

My csv is like the following:

TYPE_ES, type_1
subtypes_1, a, b
subtypes_2, 1, 2,3
  subtype_3 1,3,4,5
TYPE_ES, type_2
subtypes_1, x, y
subtypes_2, 4,5,6
  subtype_3 1,3,4,5
TYPE_ES, type_3
subtypes_1, x, y
subtypes_2, 4,5,6
 subtype_3 1,3,4,5

I would like to read and save all the lines in the csv that are between ´TYPE_ES, type_1´, ´TYPE_ES, type_2´ and then between ´TYPE_ES, type_2´, ´TYPE_ES, type_3´ and so on.

For accessing the elements from the QstandardItemModel, I am using the following:

QStringList listDeviceData;
if(fileContent->rowCount() > 0)
{
    for (int row = 0; row < fileContent->rowCount(); row++)
    {
        for (int col = 0; col < fileContent->columnCount(); col++)
        {
            QModelIndex index = fileContent->index(row,col,QModelIndex());
            listDeviceData.append(deviceData->data(index).toString());

        }
        }
    }   

 }

This method allows me to retrieve just elements of one column at a time. However if I were to fetch a set of lines as I mentioned above, how can I parse the QStandardItemModel and achieve this?

smyslov
  • 1,279
  • 1
  • 8
  • 29
  • If you want to extract data in "bunches" of lines or different formats maybe you should not use the QStandardItemModel, the model view architecture is great if you want to show data in grids and tree and you want to do it easily and remain flexible, if you want to use data in groups maybe you should use standard containers and store the data the way you want, from what I understood I would use a QMap > > or something like that. – Marco Jun 08 '16 at 09:52
  • How about QStringList @Marco – smyslov Jun 08 '16 at 11:05

1 Answers1

0

May be something like this:

void readFile(QVector<QStringList>& vectorOfStrings)
{
    QFile config_file(FILE_PATH);

    if (config_file.open(QIODevice::ReadOnly)) {
        QTextStream in(&config_file);

        while (!in.atEnd()) {

            QString fileLine = in.readLine();
            QStringList lineToken = fileLine.split(",", QString::SkipEmptyParts);
            //ignore commented lines
            if(!fileLine.startsWith("#", Qt::CaseSensitive)){
                for (int j = 0; j < lineToken.size(); j++) {
                    QString value = lineToken.at(j);
                    if(j == 0 && value == "TYPE_ES"){
                        vectorOfStrings.append(QStringList());
                        break;
                    }
                    if(vectorOfStrings.count() > 0)
                        vectorOfStrings[vectorOfStrings.count() - 1].append(value);
                }
            }
        }
        config_file.close();
    }
}

...

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);    

    QVector<QStringList> vectorOfStrings;
    readFile(vectorOfStrings);

    return a.exec();
}

You get all lines between lines with "TYPE_ES".

Kirill Chernikov
  • 1,387
  • 8
  • 21
  • It is not possible for me to know the size of vectorOfStrings in advance in order to pass it to readFile. I should be declaring it in the function itself. But here I have an issue about what the size of vectorOfStrings should be declared. – smyslov Jun 08 '16 at 11:18
  • it is not necessary to know exact count of elements of vectorOfStrings before calling readFile, because vectorOfStrings is reference. I add example of function calling. Try this. – Kirill Chernikov Jun 08 '16 at 11:27
  • I ask this because I get the following error "Assert failure in QVector::operator[]:"index out of range"" at this line vectorOfStrings[vectorOfStrings.count() - 1].append(value); which I think is because I haven't declared the size – smyslov Jun 08 '16 at 11:31
  • Add check if(vectorOfStrings.count() > 0) before vectorOfStrings[vectorOfStrings.count() - 1].append(value); – Kirill Chernikov Jun 08 '16 at 11:40