PDA

View Full Version : QSortFilterProxyModel very slow when moving a lot of rows



gabo
16th February 2022, 13:30
Hi All,

I have a QSortFilterProxyModel connected to a QAbstractItemModel that implements a tree.

I need to move ~8000 rows around which should be fine.

However, for each one of those moves (done with individual pairs of beginMoveRows / endMoveRows) the QSortFilterProxyModel clears all the internal index mappings and then tries to update all persistent indexes right after (that's all in QSortFilterProxyModelPrivate::_q_sourceLayoutChang ed). The problem is that updating the persistent indexes re-creates the mappings again and forces everything to sort, even when dynamic sorting is disabled (this is done in QSortFilterProxyModelPrivate::create_mapping). This is done for each row that is moved, and since I am using QStrings sorting is VERY slow.

I'm considering storing my data already sorted so I avoid all the string comparisons, but before I go that route I wanted to ask. Does the QSortFilterProxyModel need to do all this creating/destroying/sorting of the mappings, is there a way to avoid it? Am I using this wrong?

Thanks a lot!

d_stranz
16th February 2022, 16:37
If you are doing a pairwise move of 8000 rows, then this will be slow, because each move will cause the proxy model to update itself as you have seen.

It seems to me that instead of doing it pairwise, you might simply modify your model "behind the curtain" all at once, and use the QAbstractItemModel::beginResetModel() / QAbstractItemModel::endResetModel() notifications instead. The proxy will do the same work of recreating the mappings, but will do it only once.