It is required to upload data line by line from a plain text file to QTableView (using QStandardItemModel ). I get all the data I am interested in correctly - one value per line, no problems here. The table has 5 columns and an unlimited number of rows.

The question, actually, is how to add 5 lines from a file into a single line of the table?

While all data is loaded in the first column, the number of rows is as in a text file ...

    1 answer 1

    Well, for example, like this:

     //допустим, что у вас уже считаны пять строк из файла (например в QStringList columns) QList<QStandardItem*> newRow; for (auto columnText:columns) { auto newItem = new QStandardItem(columnText); newRow.append(newItem); } model.appendRow(newRow); 

    If without unnecessary frills combine this option and your example from the comments, you can get something like this:

     int columnIndex = 0; QRegularExpression re("(.+)="); QStringList columns; QTextStream in(&file); while(!in.atEnd()) { auto readLine = in.readLine().split(re, QString::SkipEmptyParts); if (!readLine.isEmpty()) { columns.append(readLine.at(0)); ++columnIndex; if (columnIndex == 5) { QList<QStandardItem*> newRow; for (auto columnText:columns) { auto newItem = new QStandardItem(columnText); newRow.append(newItem); } model.appendRow(newRow); columns.clear(); columnIndex = 0; } } } 
    • Unfortunately, all the code in the comment does not fit. - 33c0c3
    • You understand that in this form it is not very readable? ) - slav-yrich
    • I'm trying to insert as code, something is not very good, perhaps because of Linux. - 33c0c3
    • QTextStream in (& file); int lineIndex (0); while (! in.atEnd ()) {QRegularExpression re ("(. +) ="); QStringList readLine = in.readLine (). Split (re, QString :: SkipEmptyParts); for (int col = 0; col <readLine.size (); col ++) {QString value = readLine.at (col); QStandardItem * item = new QStandardItem (value); model-> setItem (lineIndex, col, item); } lineIndex ++; } I can't insert the formatted code, I don’t know why. - 33c0c3
    • Well .. if I correctly understood the format of the input data, then you have readLine each time consisting of one element. - slav-yrich