QTableView is extremely slow (even for only 3000 r

2019-04-05 00:20发布

I have a table with 3000 rows and 8 columns. I use the QTableView. To insert items I do:

QStandardItem* vSItem = new QStandardItem();
vSItem->setText("Blabla");
mModel->setItem(row, column, vSItem);

where mModel is QStandardItemModel. Everything is fine if I have not to many rows, but when I am trying to visualize big data (about 3000 rows), then it is extremely slow (20 seconds on Win 7 64-bit (8 core machine with 8 GB of RAM!!!)). Is there anything I can do to improve performance?

Thanks in advance.

标签: qt qtableview
8条回答
迷人小祖宗
2楼-- · 2019-04-05 00:59

For this quantity of data, you'd be better with a custom model - then you'd have the control of when you inform the view of updates, for example. The 'standard' items scale to hundreds, and probably thousands, due to modern hardware being fast, but they're explicitly documented as not being intended for datasets of this size.

查看更多
forever°为你锁心
3楼-- · 2019-04-05 01:15

Watch out for setSectionResizeMode(). This had enormous performance implications for me. It causes row and column size recalculations with every modification (i.e. every setData()/setText() call). This wasn't noticeable until I reached 1000+ rows. Consider using resizeSections() instead which seems to be a one time adjustment.

查看更多
ゆ 、 Hurt°
4楼-- · 2019-04-05 01:17

I am using 80000 rows and had a similar problem adding huge amounts of items to a table.

My solution was to let it allocate the memory in advanced by telling it how many rows it will need.

I was using a Qtableview and model, so:

self.model.setRowCount(80000)

I'm sure you can match this up with your code

查看更多
冷血范
5楼-- · 2019-04-05 01:18

Good call on the autoresize on contents for your columns or rows.

I have a function that added a column to the table each time a client connected to my server application. As the number of columns in the table got large, the insertion time seemed to take longer and longer.

I was doing a ui->messageLog->resizeRowsToContents(); each time. I changed this to only auto resize the row that was being added ui->messageLog->resizeRowToContents(0);, and the slowness went away.

查看更多
时光不老,我们不散
6楼-- · 2019-04-05 01:20

I found a solution: the problem was that I assigned the model to the tableview already in the constructor. So everytime I inserted the item in the model, tableview was informed and probably updated. Now I assign the model to the tableview only after I filled my model with data. This is not an elegant solution but it works. Is there maybe a way to temporarily disable the model from tableview or something that says to the tableview to not to care about changes in the model?

查看更多
▲ chillily
7楼-- · 2019-04-05 01:20

Do you have an autoresize on contents for your columns or rows ? It can be a killer in performance sometimes !

Have a look here : QHeaderView::ResizeToContents

Hope it helps !

查看更多
登录 后发表回答