Fix Instantiator response to model change
authorAlan Alpert <aalpert@blackberry.com>
Thu, 2 May 2013 00:22:14 +0000 (17:22 -0700)
committerThe Qt Project <gerrit-noreply@qt-project.org>
Wed, 8 May 2013 01:09:47 +0000 (03:09 +0200)
Objects were not being created correctly when the model changed after
componentComplete. After correcting that the model change can lead to
an intermediate count change when the old model is cleared, so a flag
is set to ignore intermediate changes fom the QQmlDelegateModel when
the model changes.

Task-number: QTBUG-30379
Change-Id: I55519c9ee378a1b0569567137ebd378f32a6c85c
Reviewed-by: Caroline Chao <caroline.chao@digia.com>
Reviewed-by: Christopher Adams <chris.adams@jollamobile.com>
Reviewed-by: Gabriel de Dietrich <gabriel.dedietrich@digia.com>

src/qml/types/qqmlinstantiator.cpp
src/qml/types/qqmlinstantiator_p_p.h

index a2a1fa2..234494e 100644 (file)
@@ -52,6 +52,7 @@ QT_BEGIN_NAMESPACE
 
 QQmlInstantiatorPrivate::QQmlInstantiatorPrivate()
     : componentComplete(true)
+    , effectiveReset(false)
     , active(true)
     , async(false)
     , ownModel(false)
@@ -124,7 +125,7 @@ void QQmlInstantiatorPrivate::_q_modelUpdated(const QQmlChangeSet &changeSet, bo
 {
     Q_Q(QQmlInstantiator);
 
-    if (componentComplete)
+    if (!componentComplete || effectiveReset)
         return;
 
     if (reset) {
@@ -162,7 +163,7 @@ void QQmlInstantiatorPrivate::_q_modelUpdated(const QQmlChangeSet &changeSet, bo
             objects = objects.mid(0, index) + movedObjects + objects.mid(index);
         } else for (int i = 0; i < insert.count; ++i) {
             int modelIndex = index + i;
-            QObject* obj = instanceModel->object(i, async);
+            QObject* obj = instanceModel->object(modelIndex, async);
             if (obj)
                 _q_createdItem(modelIndex, obj);
         }
@@ -378,8 +379,11 @@ void QQmlInstantiator::setModel(const QVariant &v)
         if (!d->ownModel)
             d->makeModel();
 
-        if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel *>(d->instanceModel))
+        if (QQmlDelegateModel *dataModel = qobject_cast<QQmlDelegateModel *>(d->instanceModel)) {
+            d->effectiveReset = true;
             dataModel->setModel(v);
+            d->effectiveReset = false;
+        }
     }
 
     if (d->instanceModel != prevModel) {
index 7945929..ac25ce8 100644 (file)
@@ -76,6 +76,7 @@ public:
     void _q_modelUpdated(const QQmlChangeSet &, bool);
 
     bool componentComplete;
+    bool effectiveReset;
     bool active;
     bool async;
     bool ownModel;