Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt 6.2 QStandardItemModel::setRowCount(0) very slow
QtWS25 Last Chance

Qt 6.2 QStandardItemModel::setRowCount(0) very slow

Scheduled Pinned Locked Moved Solved General and Desktop
qstandarditemmoqt6.2
3 Posts 2 Posters 570 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Rory_1
    wrote on 12 Feb 2022, 03:06 last edited by
    #1

    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.

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 12 Feb 2022, 08:03 last edited by
      #2

      You should not use setRowCount() in this case but begin/endResetModel().

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      R 1 Reply Last reply 12 Feb 2022, 16:01
      2
      • C Christian Ehrlicher
        12 Feb 2022, 08:03

        You should not use setRowCount() in this case but begin/endResetModel().

        R Offline
        R Offline
        Rory_1
        wrote on 12 Feb 2022, 16:01 last edited by
        #3

        @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.

        1 Reply Last reply
        1

        3/3

        12 Feb 2022, 16:01

        • Login

        • Login or register to search.
        3 out of 3
        • First post
          3/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved