Qt 6.2 QStandardItemModel::setRowCount(0) very slow
-
I have a QStandardItemModell that keeps track of image file information. Every time I select a new folder the datamodel is rebuilt. One of the resets is to call QStandardItemModel::setRowCount(0). The model has three views and a QSortFilterProxyModel.
Here is a performance comparison between Qt versions (time units are milliseconds):
1 row 13 rows 40 rows 50 rows 158 rows Qt 6.2 MSVC2019 0 11 1048 1495 31767 Qt 6.2 Mac M1 0 0 0 0 1 Qt 5.15 MSVC2019 0 0 1 1 3
I've traced the slow preformance to qabstractitemmodel.cpp function QAbstractItemModelPrivate::rowsRemoved.
void QAbstractItemModelPrivate::rowsRemoved(const QModelIndex &parent, int first, int last) { const QList<QPersistentModelIndexData *> persistent_moved = persistent.moved.pop(); const int count = (last - first) + 1; // it is important to only use the delta, because the change could be nested for (auto *data : persistent_moved) { QModelIndex old = data->index; persistent.indexes.erase(persistent.indexes.constFind(old)); data->index = q_func()->index(old.row() - count, old.column(), parent); if (data->index.isValid()) { persistent.insertMultiAtEnd(data->index, data); } else { qWarning() << "QAbstractItemModel::endRemoveRows: Invalid index (" << old.row() - count << ',' << old.column() << ") in model" << q_func(); } } const QList<QPersistentModelIndexData *> persistent_invalidated = persistent.invalidated.pop(); // THIS LOOP IS SLOW IN QT 6.2 RUNNING MSVC2019 for (auto *data : persistent_invalidated) { auto pit = persistent.indexes.constFind(data->index); if (pit != persistent.indexes.cend()) persistent.indexes.erase(pit); data->index = QModelIndex(); } }
I built a simple minimum code to try to replicate the issue, to no avail. So, something in my program is making it difficult to update persistent indexes, but only in Qt 6+ and MSVC2019. Any clues on what could cause this would be very much appreciated.
-
You should not use setRowCount() in this case but begin/endResetModel().
-
@Christian-Ehrlicher said in Qt 6.2 QStandardItemModel::setRowCount(0) very slow:
You should not use setRowCount() in this case but begin/endResetModel().
Thanks very much! That solved the slowdown.