Details
-
Bug
-
Resolution: Done
-
P3: Somewhat important
-
5.4.1
-
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
- is duplicated by
-
QTBUG-70310 QSortFilterProxyModel::setSourceModel reverts to empty static model if old model is deleted
- Closed
- resulted in
-
QTBUG-67948 Crash in QSortFilterProxyModel
- Closed
For Gerrit Dashboard: QTBUG-44962 | ||||||
---|---|---|---|---|---|---|
# | Subject | Branch | Project | Status | CR | V |
211070,4 | QSortFilterProxyModel: Clear persistent indexes on source model change | dev | qt/qtbase | Status: MERGED | +2 | 0 |
230462,2 | QSortFilterProxyModel: don't assert when old model gets destroyed | 5.11 | qt/qtbase | Status: MERGED | +2 | 0 |
230766,1 | QSortFilterProxyModel: don't assert when old model gets destroyed | dev | qt/qtbase | Status: ABANDONED | 0 | 0 |