Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-90030

Persistent index handling in QAbstractItemModel is wrong

    XMLWordPrintable

    Details

    • Commits:
      6ec3fa2842b5c4714dc9a3953b2721ef70dd957b (qt/qtbase/dev) 3081731b02f070cd97837fade5dc110e10cd60c6 (qt/qtbase/6.0) 90ff2d083ec6d1274359cea12136ea9f0e1f9e00 (qt/qtbase/6.1) 2af9e7da094724e0be238db583f038b4ae3861cc (qt/tqtc-qtbase/5.15)

      Description

      Unless I'm missing something, when using e.g. the built-in beginRemoveRows/endRemoveRows and beginMoveRows/endMoveRows functionality, the logic for updates to persistent model indices appears to be incorrect.

      Here's a simple example to demonstrate, making use of QTreeWidget to minimize the possibility it's my mistake:

      QTreeWidget * tree = new QTreeWidget;
      
      // Top level node:
      QTreeWidgetItem * toplevel = new QTreeWidgetItem({"toplevel"});
      tree->addTopLevelItem(toplevel);
      QModelIndex firstIndex = tree->model()->index(0, 0);
      
      // Add two children:
      QTreeWidgetItem * child1 = new QTreeWidgetItem({"child1"}),
                      * child2 = new QTreeWidgetItem({"child2"});
      toplevel->addChildren({child1, child2});
      tree->expandAll();
      
      // Select child2
      tree->selectionModel()->select(tree->model()->index(1, 0, firstIndex),
                                     QItemSelectionModel::Select);
      
      // Remove child1:
      tree->model()->removeRows(0, 1, firstIndex);
      

      This produces an error:

      QAbstractItemModel::endRemoveRows:  Invalid index ( -1 , 0 ) in model QTreeModel(...)

      ... and then the selection is lost. A similar issue occurs with moveRows, which is where I ran into it (and presumably also with columns).

      The bug doesn't appear to happen if we remove nodes at the top level.

       

        Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

          Activity

            People

            Assignee:
            dfaure_kdab David Faure
            Reporter:
            carlturner Carl Turner
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:

                Gerrit Reviews

                There are no open Gerrit changes