From 675b4f63feab7c81c75e49f6dea82a39dd18f489 Mon Sep 17 00:00:00 2001 From: Ahmad Samir Date: Wed, 6 Sep 2023 20:51:15 +0300 Subject: QIdentityProxyModel: add setHandleSourceLayoutChanges(bool) Some sub-classes have special handling of source model layout changes (abbreviated as SMLC from here on out), they relied on disconnecting the connections to the _q_*layout* slots in the private class using the SLOT macro. This isn't possible any more after recent changes (and the method were renamed to remove _q_ prefix). Sub-classes resorting to using private API is a clear sign some functionality is missing from the public API, so a cleaner solution for this issue is adding this setter which enables sub-classes to tell QIdentityProxyModel to leave handling of the SMLC to them. Thanks to David Faure for the idea/solution. [ChangeLog][QtCore][QIdentityProxyModel] Added setHandleSourceLayoutChanges(bool) method to allow sub-classes to indicate to QIdentityProxyModel that they will handle source model layout changes on their own. Also added a getter, isHandleSourceLayoutChanges(). Change-Id: I1de79dd693ce32a6e2df9a7c81dd4abdc5f00248 Reviewed-by: Axel Spoerl --- .../tst_qidentityproxymodel.cpp | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'tests/auto/corelib/itemmodels') diff --git a/tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp b/tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp index b1de23d619..467910ef48 100644 --- a/tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp +++ b/tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp @@ -27,6 +27,16 @@ public: const QModelIndex idx = index(0, 0, QModelIndex()); Q_EMIT dataChanged(idx, idx, QList() << 1); } + + // Workaround QObject::isSignalConnected() being a protected method + bool isConnected(const QMetaMethod &m) const { return isSignalConnected(m); } +}; + +class IdentityProxyModel : public QIdentityProxyModel +{ +public: + // The name has to be different than the method from the base class + void setHandleSLC(bool b) { setHandleSourceLayoutChanges(b); } }; class tst_QIdentityProxyModel : public QObject @@ -53,6 +63,9 @@ private slots: void persistIndexOnLayoutChange(); void createPersistentOnLayoutAboutToBeChanged(); + + void testSetHandleLayoutChanges(); + protected: void verifyIdentity(QAbstractItemModel *model, const QModelIndex &parent = QModelIndex()); @@ -513,5 +526,27 @@ void tst_QIdentityProxyModel::createPersistentOnLayoutAboutToBeChanged() // QTBU QCOMPARE(layoutChangedSpy.size(), 1); } +void tst_QIdentityProxyModel::testSetHandleLayoutChanges() +{ + const std::array layoutSignals = { + QMetaMethod::fromSignal(&QAbstractItemModel::layoutChanged), + QMetaMethod::fromSignal(&QAbstractItemModel::layoutAboutToBeChanged), + }; + + DataChangedModel model; + IdentityProxyModel proxy; + proxy.setSourceModel(&model); + for (const auto &m : layoutSignals) + QVERIFY(model.isConnected(m)); // Connected by default + + proxy.setSourceModel(nullptr); + + // Disable handling (connecting to layotu signals) of source model layout changes + proxy.setHandleSLC(false); + proxy.setSourceModel(&model); + for (const auto &m : layoutSignals) + QVERIFY(!model.isConnected(m)); +} + QTEST_MAIN(tst_QIdentityProxyModel) #include "tst_qidentityproxymodel.moc" -- cgit v1.2.3