Why my QAbstractTableModel table is slower than QStandardItemModel table?



  • The table was using QStandardItemModel before, after I learned that QStandardItemModel is always slower than loading a custom model due to the creation of each individual QStandardItem based on book <<Advanced Qt Programming>> chapter 3.

    So I modified the table to QAbstractTableModel, of course implemented data(), setData(), headerData(), etc. those required functions.

    I can see the QAbstractTableModel table generation is about 100ms faster than QStandardItemModel, but I also notice scrolling up/down is much slower now. Is this normal?

    The data structure I have for QAbstractTableModel is QList<QList<QPair<QVariant, QVariant>>>, QPair<QVariant, QVariant> first QVariant is for display role, second is for sort role. Also the table row color can be different, so I have a QList<QList<QVariant>> data for ForegroundRole.

    The data structure for previous QStandardItemModel was QHash< QPair<int, int>, QPair<QVariant, QVariant> >, QPair<int, int> is cell index row and column, QPair<QVariant, QVariant> is first QVariant is for display role, second is for sort role(the same as QAbstractTableModel). It doesn't need data for ForegroundRole because I can set color for individual cell.

    Any ideas?

    Thanks,



  • Did anyone try both QAbstractTableModel-based table and QStandardItemModel-based table, and find QAbstractTableModel-based table is slower in displaying?



  • I am not sure what you are expecting the world-at-large to contribute. Your code is slower, by whatever definition of slower, for reasons that can be seen by inspecting code only you have access to. You may be doing more work than you need, your data structure could be inefficient, you may not short circuit obvious null QVariant responses as soon as possible, your logic could just be plain broken...

    Generally the reason for using a custom model is to wrap data structures that are better suited to whatever else you need to do with them. "Better suited" might mean more frugal with memory, lazy-loaded (e.g. QSqlQueryModel), faster to write, faster to read, connected to a remote data source, easier to interface to other systems etc. There's no particular reason to expect a given custom model will display faster than a generic one.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.