PDA

View Full Version : Sequence of inverted data row in Qtableview



Eduardo Huerta
26th December 2019, 00:34
Hi,
I have a problem reading data from a file and presenting it in a QtableView, the sequence of the data row is reversed. I am using the insertRow (int position, int rows, const QModelIndex & index) function of the AddressBook example in the QtableModel, that is, beginInsertRows (QModelIndex (), position, position + rows - 1); .... endInsertRows (). When calling the function insertRow () I put
int rowPosition = 0;
tableModel-> insertRows (rowPosition, 1, QModelIndex ());
rowPosition ++; // Number of rows I have added
but the rows are presented in an inverted order as in the figures.
How do I present the rows in the proper order?
thanks for help me.

ChrisW67
26th December 2019, 09:30
The beginInsertRows() and endInsertRows() functions are for use inside the model implementation around the logic that populates the model's internal data structures.
The insertRows() function is a public API for manipulating the model content from outside the model.

Are you implementing a table model of your own, or are you simply populating a QStandardItemModel (or similar existing model)?

if you are doing the latter then this works:

#include <QApplication>
#include <QStandardItemModel>
#include <QTableView>
#include <QFile>
#include <QTextStream>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QStandardItemModel model;
model.setColumnCount(10);

QFile file("test.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return 1;

QTextStream in(&file);
while (!in.atEnd()) {
int row = model.rowCount();
model.insertRow(row);
double value;
for (int col = 0; col < 10; ++col) {
in >> value;
model.setData(model.index(row, col), value);
}
in.skipWhiteSpace();
}

QTableView view;
view.setModel(&model);
view.show();
return app.exec();
}

Test.txt:

1.23 1.12 1.19 2.12 2.22 2.01 3.1 3.2 3.01 3.1
1.70 1.11 1.88 2.01 2.10 2.92 3.01 3.12 3.21 3.2
1.70 1.11 1.88 2.01 2.10 2.92 3.01 3.12 3.21 3.2
1.12 1.30 1.91 2.11 2.31 2.11 3.22 3.11 3.45 3.5
1.34 1.12 1.65 2.01 2.09 2.09 3.11 3.44 3.55 3.1
1.90 1.13 1.34 2.07 2.08 2.06 3.01 3.02 3.02 3.4
1.12 1.34 1.89 2.03 2.07 2.03 3.32 3.11 3.21 3.1
1.12 1.34 1.89 2.03 2.07 2.03 3.32 3.11 3.21 3.1
1.12 1.34 1.89 2.03 2.07 2.03 3.32 3.11 3.21 3.1