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

Persistent child indexes refer to incorrect model when source model changed

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P3: Somewhat important
    • 5.11.0
    • 5.4.1
    • Widgets: Itemviews
    • None
    • Ubuntu, 64-bit.

    Description

      When a new source model is applied to a proxy model, any persistent child indexes of a hierarchical model do not seem to be properly invalidated. They still refer to the original model.

      If a custom model is being implemented, the index method is then given a parent index that is not of the correct model.

      This can be illustrated by overriding QStandardItemModel::index and adding an assert.

      #include <QApplication>
      #include <QSortFilterProxyModel>
      #include <QStandardItemModel>
      
      #include <QMainWindow>
      #include <QTreeView>
      
      
      class ItemModel : public QStandardItemModel
      {
      public:
          QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const Q_DECL_OVERRIDE
          {
              // Would we always expect the parent to belong to the model?
              Q_ASSERT(!parent.isValid() || parent.model() == this);
              return QStandardItemModel::index(row, column, parent);
          }
      };
      
      void fillModel(QAbstractItemModel &model)
      {
          model.insertRow(0);
          model.insertColumn(0);
          QModelIndex index = model.index(0, 0);
          model.setData(index, "Level0");
          {
              model.insertRow(0, index);
              model.insertColumn(0, index);
              index = model.index(0, 0, index);
              model.setData(index, "Level1");
              {
                  model.insertRow(0, index);
                  model.insertColumn(0, index);
                  index = model.index(0, 0, index);
                  model.setData(index, "Level2");
              }
          }
      }
      
      
      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QMainWindow mainWindow;
          mainWindow.resize(200, 200);
          mainWindow.show();
      
          QTreeView treeView(&mainWindow);
          treeView.resize(mainWindow.width(), mainWindow.height());
      
          ItemModel model;
          fillModel(model);
      
          QSortFilterProxyModel proxy;
          proxy.setSourceModel(&model);
      
          treeView.setModel(&proxy);
          treeView.expandAll();           // Required to create 'persistent' indexes.
      
          // Now set a new model: see assert.
          ItemModel model2;
          fillModel(model2);
          proxy.setSourceModel(&model2);
      
          return a.exec();
      }
      

      Attachments

        Issue Links

          For Gerrit Dashboard: QTBUG-44962
          # Subject Branch Project Status CR V

          Activity

            People

              chehrlic Christian Ehrlicher
              chard Richard Hazlewood
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes