From 722798a359761a1eb635d18547b076615f192508 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Tue, 2 Apr 2013 17:52:54 +0200 Subject: Don't call virtual methods after the source model is destroyed. Calling clear_mapping causes the persistent indexes to be queried, and mapped using map_to_source, so that they can be restored later. That is not the appropriate response to the source model being deleted because there won't be anything to restore. Simply clear the stored mapping information instead so that the source model actually exists when mapToSource is called by the framework. Change-Id: I99692ee7aa9c6714aec45c68fe4a2d62be189d60 Reviewed-by: Olivier Goffart --- .../tst_qsortfilterproxymodel.cpp | 35 ++++++++++++++++++++++ 1 file changed, 35 insertions(+) (limited to 'tests/auto/corelib/itemmodels') diff --git a/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp b/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp index 4578bcdab6..923b9a3a07 100644 --- a/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp +++ b/tests/auto/corelib/itemmodels/qsortfilterproxymodel/tst_qsortfilterproxymodel.cpp @@ -148,6 +148,7 @@ private slots: void chainedProxyModelRoleNames(); + void noMapAfterSourceDelete(); protected: void buildHierarchy(const QStringList &data, QAbstractItemModel *model); void checkHierarchy(const QStringList &data, const QAbstractItemModel *model); @@ -3809,5 +3810,39 @@ void tst_QSortFilterProxyModel::chainedProxyModelRoleNames() QVERIFY(proxy2.roleNames().value(Qt::UserRole + 1) == "custom"); } +class SourceAssertion : public QSortFilterProxyModel +{ + Q_OBJECT +public: + explicit SourceAssertion(QObject *parent = 0) + : QSortFilterProxyModel(parent) + { + + } + + QModelIndex mapToSource(const QModelIndex &proxyIndex) const + { + Q_ASSERT(sourceModel()); + return QSortFilterProxyModel::mapToSource(proxyIndex); + } +}; + +void tst_QSortFilterProxyModel::noMapAfterSourceDelete() +{ + SourceAssertion proxy; + QStringListModel *model = new QStringListModel(QStringList() << "Foo" << "Bar"); + + proxy.setSourceModel(model); + + // Create mappings + QPersistentModelIndex persistent = proxy.index(0, 0); + + QVERIFY(persistent.isValid()); + + delete model; + + QVERIFY(!persistent.isValid()); +} + QTEST_MAIN(tst_QSortFilterProxyModel) #include "tst_qsortfilterproxymodel.moc" -- cgit v1.2.3 From 96e3c2bcbfedc8b5cb8fc099229a02a1fa335c21 Mon Sep 17 00:00:00 2001 From: Volker Krause Date: Mon, 8 Apr 2013 14:14:32 +0200 Subject: Don't bypass overwritten [set]data() methods in the proxy. By calling itemData() of the source model directly, the result cannot contain data provided by the proxy model itself. The base class implementation however will call data() on the proxy instead. Change-Id: Ib0ef5f5621457adbfa4bd896a756dfcb98d0ae54 Reviewed-by: Stephen Kelly --- .../tst_qidentityproxymodel.cpp | 26 ++++++++++++++++++++++ 1 file changed, 26 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 f750b7a9d4..ea0a14c18c 100644 --- a/tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp +++ b/tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp @@ -79,6 +79,8 @@ private slots: void reset(); void dataChanged(); + void itemData(); + protected: void verifyIdentity(QAbstractItemModel *model, const QModelIndex &parent = QModelIndex()); @@ -364,5 +366,29 @@ void tst_QIdentityProxyModel::dataChanged() m_proxy->setSourceModel(0); } +class AppendStringProxy : public QIdentityProxyModel +{ +public: + QVariant data(const QModelIndex &index, int role) const + { + const QVariant result = sourceModel()->data(index, role); + if (role != Qt::DisplayRole) + return result; + return result.toString() + "_appended"; + } +}; + +void tst_QIdentityProxyModel::itemData() +{ + QStringListModel model(QStringList() << "Monday" << "Tuesday" << "Wednesday"); + AppendStringProxy proxy; + proxy.setSourceModel(&model); + + const QModelIndex topIndex = proxy.index(0, 0); + QCOMPARE(topIndex.data(Qt::DisplayRole).toString(), QStringLiteral("Monday_appended")); + QCOMPARE(proxy.data(topIndex, Qt::DisplayRole).toString(), QStringLiteral("Monday_appended")); + QCOMPARE(proxy.itemData(topIndex).value(Qt::DisplayRole).toString(), QStringLiteral("Monday_appended")); +} + QTEST_MAIN(tst_QIdentityProxyModel) #include "tst_qidentityproxymodel.moc" -- cgit v1.2.3