QTableView is extremely slow (even for only 3000 rows)

Viewed 20057

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.

9 Answers

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.

I had the exact same issue, with even 100 rows the performance was horrible.

Upon inspection this issue is not really related to options set on the table view itself (like resizing and such), but rather because the model informs and updates the view for each insertion (beginInsertRows/endInsertRows).

With that being said you have 2 options for maximum performance:

  1. Set the model to the view after you populated with data
  2. Set the model anytime, but populate a new list of data and then assign that list to the model

Whichever option you go with, performance is crazy. I went with the second option because I set my model (and proxy model) in the constructor of the widget.

Later on when I want to add data:

// Create a new list of data (temporary)
QList<MyObjects*> NewList;

for (auto it = Result.cbegin(); it != Result.cend(); ++it) 
{
    MyObjects* my = new MyObjects();
    
    // my - set data
    
    NewList.append(my);
}

// Now simply replace current data list
Model->setList(NewList);

This is considering you created your own setList() function inside your custom Model:

void Model::setList(QList<MyObjects*> clist) 
{
    beginResetModel();
    list = clist;
    endResetModel();
}

And voila... you load thousands of records with high performance. Notice beginResetModel() and endResetModel() are the functions that notify the table view.

Enjoy.

Related