summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp
diff options
context:
space:
mode:
authorStephen Kelly <steveire@gmail.com>2016-12-20 00:44:12 +0000
committerStephen Kelly <steveire@gmail.com>2016-12-21 01:37:50 +0000
commitbaad82d242a4d8c1af6c87faaa7f25584183fd53 (patch)
treef0330b51b906160732d331c14e14e5193151a0a9 /tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp
parent0874861bcc70313c343aba5e5566ed30b69eed1c (diff)
QIPM: Persist model indexes after emitting layoutChange, not before
Callers can persist a QModelIndex which was not persisted before in a slot connected to the signal, and such a persisted index must be updated in the course of the layoutChange. Store the indexes to persist after emitting the signal. Task-number: QTBUG-32981 Change-Id: Ibee4c0d84817d72603a03fe5b22fdeefeac0695e Reviewed-by: David Faure <david.faure@kdab.com>
Diffstat (limited to 'tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp')
-rw-r--r--tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp76
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp b/tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp
index e946f3104a..564b8547b1 100644
--- a/tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp
+++ b/tests/auto/corelib/itemmodels/qidentityproxymodel/tst_qidentityproxymodel.cpp
@@ -68,6 +68,8 @@ private slots:
void itemData();
+ void persistIndexOnLayoutChange();
+
protected:
void verifyIdentity(QAbstractItemModel *model, const QModelIndex &parent = QModelIndex());
@@ -377,5 +379,79 @@ void tst_QIdentityProxyModel::itemData()
QCOMPARE(proxy.itemData(topIndex).value(Qt::DisplayRole).toString(), QStringLiteral("Monday_appended"));
}
+void dump(QAbstractItemModel* model, QString const& indent = " - ", QModelIndex const& parent = {})
+{
+ for (auto row = 0; row < model->rowCount(parent); ++row)
+ {
+ auto idx = model->index(row, 0, parent);
+ qDebug() << (indent + idx.data().toString());
+ dump(model, indent + "- ", idx);
+ }
+}
+
+void tst_QIdentityProxyModel::persistIndexOnLayoutChange()
+{
+ DynamicTreeModel model;
+
+ QList<int> ancestors;
+ for (auto i = 0; i < 3; ++i)
+ {
+ Q_UNUSED(i);
+ ModelInsertCommand insertCommand(&model);
+ insertCommand.setAncestorRowNumbers(ancestors);
+ insertCommand.setStartRow(0);
+ insertCommand.setEndRow(0);
+ insertCommand.doCommand();
+ ancestors.push_back(0);
+ }
+ ModelInsertCommand insertCommand(&model);
+ insertCommand.setAncestorRowNumbers(ancestors);
+ insertCommand.setStartRow(0);
+ insertCommand.setEndRow(1);
+ insertCommand.doCommand();
+
+ // dump(&model);
+ // " - 1"
+ // " - - 2"
+ // " - - - 3"
+ // " - - - - 4"
+ // " - - - - 5"
+
+ QIdentityProxyModel proxy;
+ proxy.setSourceModel(&model);
+
+ QPersistentModelIndex persistentIndex;
+
+ QPersistentModelIndex sourcePersistentIndex = model.match(model.index(0, 0), Qt::DisplayRole, "5", 1, Qt::MatchRecursive).first();
+
+ QCOMPARE(sourcePersistentIndex.data().toString(), QStringLiteral("5"));
+
+ bool gotLayoutAboutToBeChanged = false;
+ bool gotLayoutChanged = false;
+
+ QObject::connect(&proxy, &QAbstractItemModel::layoutAboutToBeChanged, &proxy, [&proxy, &persistentIndex, &gotLayoutAboutToBeChanged]
+ {
+ gotLayoutAboutToBeChanged = true;
+ persistentIndex = proxy.match(proxy.index(0, 0), Qt::DisplayRole, "5", 1, Qt::MatchRecursive).first();
+ });
+
+ QObject::connect(&proxy, &QAbstractItemModel::layoutChanged, &proxy, [&proxy, &persistentIndex, &sourcePersistentIndex, &gotLayoutChanged]
+ {
+ gotLayoutChanged = true;
+ QCOMPARE(QModelIndex(persistentIndex), proxy.mapFromSource(sourcePersistentIndex));
+ });
+
+ ModelChangeChildrenLayoutsCommand layoutChangeCommand(&model, 0);
+
+ layoutChangeCommand.setAncestorRowNumbers(QList<int>{0, 0, 0});
+ layoutChangeCommand.setSecondAncestorRowNumbers(QList<int>{0, 0});
+
+ layoutChangeCommand.doCommand();
+
+ QVERIFY(gotLayoutAboutToBeChanged);
+ QVERIFY(gotLayoutChanged);
+ QVERIFY(persistentIndex.isValid());
+}
+
QTEST_MAIN(tst_QIdentityProxyModel)
#include "tst_qidentityproxymodel.moc"