summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/itemmodels/qitemselectionmodel
diff options
context:
space:
mode:
authorAxel Spoerl <axel.spoerl@qt.io>2023-08-16 20:09:52 +0200
committerAxel Spoerl <axel.spoerl@qt.io>2023-09-01 00:11:51 +0200
commit4f4a8e75ab34003a4a49b89392ae7712415ac788 (patch)
tree24107251d0106e66fb9394e5bd8f95286e5251ce /tests/auto/corelib/itemmodels/qitemselectionmodel
parent381612f7944b202c8b1428f0cc9d1af72f5f7647 (diff)
QItemSelectionModelPrivate: use QObjectPrivate::connect
QItemSelectionModelPrivate::initModel() uses string based connections, to connect/disconnet its QAbstractItemModel. The QObject::destroyed signal is connected to modelDestroyed(), which does not disconnect other signals. QQuickTableView's selection model binds to its QAbstractItemModel. The binding also reacts to QObject::destroyed Eventually, QItemSelectionModel::setModel(nullptr) is called. At this point, only a QOBject is left from the QAbstractItemModel. That leads to warnings about disconnecting string based signals, which belong to QAbstractItemModel. This patch changes the connect syntax to the QObjectPrivate::connect API. Instead of keeping a list of string based connections around, the connections themselves are kept in a list member. Disconnecting happens based on that list. Connections are also disconnected in QAbstractItemModelPrivate::modelDestroyed. An auto test is added in tst_QItemSelectionModel. Fixes: QTBUG-116056 Pick-to: 6.6 6.5 6.2 Change-Id: I57e5c0f0a574f154eb312a282003774dd0613dd6 Reviewed-by: Ahmad Samir <a.samirh78@gmail.com> Reviewed-by: Richard Moe Gustavsen <richard.gustavsen@qt.io>
Diffstat (limited to 'tests/auto/corelib/itemmodels/qitemselectionmodel')
-rw-r--r--tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp
index 2d09b01c26..a14336e119 100644
--- a/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp
+++ b/tests/auto/corelib/itemmodels/qitemselectionmodel/tst_qitemselectionmodel.cpp
@@ -80,7 +80,10 @@ private slots:
void QTBUG93305();
+ void testSignalsDisconnection();
+
private:
+ static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
QAbstractItemModel *model;
QItemSelectionModel *selection;
};
@@ -2917,5 +2920,37 @@ void tst_QItemSelectionModel::QTBUG93305()
QCOMPARE(spy.size(), 4);
}
+static void (*oldMessageHandler)(QtMsgType, const QMessageLogContext&, const QString&);
+static bool signalError = false;
+
+// detect disconnect warning:
+// qt.core.qobject.connect: QObject::disconnect: No such signal
+void tst_QItemSelectionModel::messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
+{
+ Q_ASSERT(oldMessageHandler);
+
+ if (type == QtWarningMsg
+ && QString(context.category) == "qt.core.qobject.connect"
+ && msg.contains("No such")) {
+ signalError = true;
+ }
+
+ return oldMessageHandler(type, context, msg);
+}
+
+void tst_QItemSelectionModel::testSignalsDisconnection()
+{
+ oldMessageHandler = qInstallMessageHandler(messageHandler);
+ auto resetMessageHandler = qScopeGuard([] { qInstallMessageHandler(oldMessageHandler); });
+ auto *newModel = new QStandardItemModel(model);
+ selection->setModel(newModel);
+ QSignalSpy spy(newModel, &QObject::destroyed);
+ delete newModel;
+ QTRY_COMPARE(spy.count(), 1);
+ qDebug() << spy;
+ selection->setModel(nullptr);
+ QVERIFY(!signalError);
+}
+
QTEST_MAIN(tst_QItemSelectionModel)
#include "tst_qitemselectionmodel.moc"