PDA

View Full Version : need faster QTableView performance



phenoboy
19th February 2015, 14:02
What kind of possibilities there are to get faster QTableView performance when adding big amount of rows. I want to add say, 10 000 rows in "batch" and then display it to user. It seems too slow for me. I want to know what is the fastest way to add "batch" data of large amounts.

I'm using QTableView and setting QStandardItemModel as model.


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

QStandardItemModel model(50, 3);
for (int i = 0; i < model.rowCount(); ++i) {
for (int j = 0; j < model.columnCount(); ++j) {
QStandardItem *item = new QStandardItem(QString("%1, %2").arg(i).arg(j));
model.setItem(i, j, item);
}
}

QTableView view;
view.setModel(&model);
view.setVerticalScrollBar(new WideScrollBar(&view));
view.show();

return app.exec();

wysota
19th February 2015, 15:47
Use a custom model instead of QStandardItemModel. As a last resort disable updates of the view or temporarily block signals from the model.

phenoboy
20th February 2015, 06:35
Ok, I guess that I would need to implement my own class for data storage and re-implement my own function something like 'setAllData(MyData*) that sets all the data at once ?

wysota
20th February 2015, 08:16
More likely you should have a function for appending a batch of data at once. This doesn't have to be "all" data.

StrikeByte
20th February 2015, 16:00
For a custom table model you can implement insertrows
call beginInsertRows(parentIndex, first, last);
add all the rows you need
call endInsertRows()

this should only update once after finishing inserting rows.

phenoboy
20th February 2015, 21:24
More likely you should have a function for appending a batch of data at once. This doesn't have to be "all" data.


For a custom table model you can implement insertrows
call beginInsertRows(parentIndex, first, last);
add all the rows you need
call endInsertRows()

this should only update once after finishing inserting rows.

Thanks, what do you suggest for data implementation class (QVector, QList, etc..) if I only need new function in my derived QAbstractItemModel to fill data at once ?

wysota
20th February 2015, 21:50
Thanks, what do you suggest for data implementation class (QVector, QList, etc..) if I only need new function in my derived QAbstractItemModel to fill data at once ?

It really depends on your data and how your model is to behave. QList is usually a good first approach as the model is linear anyway.

jefftee
20th February 2015, 23:32
I don't have a ton of experience writing models, but the first one I wrote used QList... Worked fine until I started using larger result sets and added the ability to sort by any column.

I rewrote the model to use QVector and QSortFilterProxyModel which greatly improved the speed for sorting operations. While I never did benchmark QList vs QVector, I suspect the biggest improvement was due to the QSortFilterProxyModel implementation, not necessarily the difference between QList and QVector, but I never cared enough to benchmark the difference.

If you also have sorting requirements, you might want to implement QSortFilterProxyModel.

Good luck.

phenoboy
21st February 2015, 07:04
I don't have a ton of experience writing models, but the first one I wrote used QList... Worked fine until I started using larger result sets and added the ability to sort by any column.

I rewrote the model to use QVector and QSortFilterProxyModel which greatly improved the speed for sorting operations. While I never did benchmark QList vs QVector, I suspect the biggest improvement was due to the QSortFilterProxyModel implementation, not necessarily the difference between QList and QVector, but I never cared enough to benchmark the difference.

If you also have sorting requirements, you might want to implement QSortFilterProxyModel.

Good luck.

Thanks, I use QSortFilterProxyModel in my current implementation and it is a must to have. Basically I would be fine with QStandardItemModel but I only need one additional function to make large amounts of rows much faster. I am thinking how to design my own model so that I could use QStandardItemModel as backend to represent data and use QStandardItem classes as cell items. They're nice as they give you basic features, checkboxes, foreground and background colors, etc..

wysota
21st February 2015, 08:07
While I never did benchmark QList vs QVector, I suspect the biggest improvement was due to the QSortFilterProxyModel implementation, not necessarily the difference between QList and QVector, but I never cared enough to benchmark the difference.

The only practical difference between those two classes is that the vector only grows at the end while QList can grow from both ends which makes it faster to insert elements at the beginning or in the middle of the sequence.