diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-09-21 21:24:26 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2023-09-22 06:14:02 +0000 |
commit | 372eaedc5b8c771c46acc4c96e91bbade4ca3624 (patch) | |
tree | 4aaa1ee548ec9583a8e3da950fa55b6f614168c9 | |
parent | acee3c71a5296f5aeeb67104f58d960021d7b69f (diff) |
Amends 4f4a8e75ab34003a4a49b89392ae7712415ac788, after which
QItemSelectionModel printed a warning when destroying the model.
We reset the selection model in response to the model getting destroyed,
and since the model is already set to be nullptr at this point the
select() function complains about changing the selection with no model
set being a no-op.
Fix this by not calling reset() when the model gets destroyed - the
stored selection and currentIndex are already reset at this point -
and instead only call reset() when a new model is set in initModel.
Fixes: QTBUG-117200
Change-Id: I12fc6b3fb2f2ff2a34b46988d5f58151123f9976
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
(cherry picked from commit dc126de22ea4d38736ae57c08c0b271f3c53fad9)
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
-rw-r--r-- | src/corelib/itemmodels/qitemselectionmodel.cpp | 6 | ||||
-rw-r--r-- | tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp | 17 |
2 files changed, 20 insertions, 3 deletions
diff --git a/src/corelib/itemmodels/qitemselectionmodel.cpp b/src/corelib/itemmodels/qitemselectionmodel.cpp index ad36ecfbf6..67868b19e1 100644 --- a/src/corelib/itemmodels/qitemselectionmodel.cpp +++ b/src/corelib/itemmodels/qitemselectionmodel.cpp @@ -555,8 +555,10 @@ void QItemSelectionModelPrivate::initModel(QAbstractItemModel *m) if (oldModel == m) return; - if (oldModel) + if (oldModel) { + q->reset(); disconnectModel(); + } // Caller has to call notify(), unless calling during construction (the common case). model.setValueBypassingBindings(m); @@ -593,12 +595,10 @@ void QItemSelectionModelPrivate::initModel(QAbstractItemModel *m) void QItemSelectionModelPrivate::disconnectModel() { - Q_Q(QItemSelectionModel); for (auto &connection : connections) { QObject::disconnect(connection); connection = QMetaObject::Connection(); } - q->reset(); } /*! diff --git a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp index a14336e119..eeb72f5301 100644 --- a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp +++ b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp @@ -81,6 +81,7 @@ private slots: void QTBUG93305(); void testSignalsDisconnection(); + void destroyModel(); private: static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg); @@ -2952,5 +2953,21 @@ void tst_QItemSelectionModel::testSignalsDisconnection() QVERIFY(!signalError); } +void tst_QItemSelectionModel::destroyModel() +{ + auto itemModel = std::make_unique<QStandardItemModel>(5, 5); + auto selectionModel = std::make_unique<QItemSelectionModel>(); + selectionModel->setModel(itemModel.get()); + selectionModel->select(itemModel->index(0, 0), QItemSelectionModel::Select); + QVERIFY(!selectionModel->selection().isEmpty()); + selectionModel->setCurrentIndex(itemModel->index(1, 0), QItemSelectionModel::Select); + QVERIFY(selectionModel->currentIndex().isValid()); + + QTest::failOnWarning(QRegularExpression(".*")); + itemModel.reset(); + QVERIFY(!selectionModel->currentIndex().isValid()); + QVERIFY(selectionModel->selection().isEmpty()); +} + QTEST_MAIN(tst_QItemSelectionModel) #include "tst_qitemselectionmodel.moc" |