Update parent indexes first with changePersistentIndex.
authorStephen Kelly <stephen.kelly@kdab.com>
Sun, 15 Apr 2012 21:03:40 +0000 (23:03 +0200)
committerQt by Nokia <qt-info@nokia.com>
Mon, 16 Apr 2012 22:47:49 +0000 (00:47 +0200)
Otherwise, the order of updating of the indexes will cause
inconsistent results because it will rely on ordering within a
QHash (which is indeterminate).

Task-number: QTBUG-25325

Change-Id: I7d99578c8ee2954b8562dc5aff7dc32e74d41fb5
Reviewed-by: Lars Knoll <lars.knoll@nokia.com>
tests/auto/corelib/itemmodels/qabstractitemmodel/qabstractitemmodel.pro
tests/auto/other/modeltest/dynamictreemodel.cpp

index 8bfe662..9e59251 100644 (file)
@@ -6,4 +6,3 @@ mtdir = ../../../other/modeltest
 INCLUDEPATH += $$PWD/$${mtdir}
 SOURCES = tst_qabstractitemmodel.cpp $${mtdir}/dynamictreemodel.cpp $${mtdir}/modeltest.cpp
 HEADERS = $${mtdir}/dynamictreemodel.h $${mtdir}/modeltest.h
-CONFIG += insignificant_test # QTBUG-25325
index 325fc19..ab783d0 100644 (file)
@@ -372,7 +372,17 @@ void ModelChangeChildrenLayoutsCommand::doCommand()
         }
     }
 
-    foreach (const QModelIndex &idx, m_model->persistentIndexList()) {
+    // If we're changing one of the parent indexes, we need to ensure that we do that before
+    // changing any children of that parent. The reason is that we're keeping parent1 and parent2
+    // around as QPersistentModelIndex instances, and we query idx.parent() in the loop.
+    QModelIndexList persistent = m_model->persistentIndexList();
+    foreach (const QModelIndex &parent, parents) {
+        int idx = persistent.indexOf(parent);
+        if (idx != -1)
+            persistent.move(idx, 0);
+    }
+
+    foreach (const QModelIndex &idx, persistent) {
         if (idx.parent() == parent1) {
             if (idx.row() == rowSize1 - 1) {
                 m_model->changePersistentIndex(idx, m_model->createIndex(0, idx.column(), idx.internalPointer()));