Changing model after componentComplete should reset currentIndex
authorMartin Jones <martin.jones@nokia.com>
Mon, 23 Jul 2012 03:19:04 +0000 (13:19 +1000)
committerQt by Nokia <qt-info@nokia.com>
Mon, 23 Jul 2012 04:38:28 +0000 (06:38 +0200)
When the model is changed reset currentIndex back to 0.

Task-number: QTBUG-26604
Change-Id: I1934e083819537d416acd85c75362daff382aa04
Reviewed-by: Bea Lam <bea.lam@nokia.com>
src/quick/doc/src/whatsnew.qdoc
src/quick/items/qquickitemview.cpp
tests/auto/quick/qquickgridview/tst_qquickgridview.cpp
tests/auto/quick/qquicklistview/tst_qquicklistview.cpp

index 954506c..8eff21e 100644 (file)
@@ -299,6 +299,7 @@ the window loses focus.
     \li Delegates in the cache buffer are now created asynchronously.
     \li Setting a \c RightToLeft layout now also reverses the \c preferredHighlightBegin and \c
         preferredHighlightEnd.
+    \li If the model is changed after the component is completed, currentIndex is reset to 0.
     \endlist
 \li ListView only:
     \list
index a9022bd..8f1687f 100644 (file)
@@ -309,18 +309,8 @@ void QQuickItemView::setModel(const QVariant &model)
         if (isComponentComplete()) {
             d->updateSectionCriteria();
             d->refill();
-            if ((d->currentIndex >= d->model->count() || d->currentIndex < 0) && !d->currentIndexCleared) {
-                setCurrentIndex(0);
-            } else {
-                d->moveReason = QQuickItemViewPrivate::SetIndex;
-                d->updateCurrent(d->currentIndex);
-                if (d->highlight && d->currentItem) {
-                    if (d->autoHighlight)
-                        d->resetHighlightPosition();
-                    d->updateTrackedItem();
-                }
-                d->moveReason = QQuickItemViewPrivate::Other;
-            }
+            d->currentIndex = -1;
+            setCurrentIndex(0);
             d->updateViewport();
 
             if (d->transitioner && d->transitioner->populateTransition) {
@@ -328,6 +318,7 @@ void QQuickItemView::setModel(const QVariant &model)
                 d->forceLayoutPolish();
             }
         }
+
         connect(d->model, SIGNAL(modelUpdated(QQuickChangeSet,bool)),
                 this, SLOT(modelUpdated(QQuickChangeSet,bool)));
         emit countChanged();
index 13753ea..110df07 100644 (file)
@@ -1801,16 +1801,16 @@ void tst_QQuickGridView::swapWithFirstItem()
 
 void tst_QQuickGridView::currentIndex()
 {
-    QaimModel model;
+    QaimModel initModel;
     for (int i = 0; i < 60; i++)
-        model.addItem("Item" + QString::number(i), QString::number(i));
+        initModel.addItem("Item" + QString::number(i), QString::number(i));
 
     QQuickView *window = new QQuickView(0);
     window->setGeometry(0,0,240,320);
     window->show();
 
     QQmlContext *ctxt = window->rootContext();
-    ctxt->setContextProperty("testModel", &model);
+    ctxt->setContextProperty("testModel", &initModel);
 
     QString filename(testFile("gridview-initCurrent.qml"));
     window->setSource(QUrl::fromLocalFile(filename));
@@ -1824,16 +1824,27 @@ void tst_QQuickGridView::currentIndex()
     QQuickItem *contentItem = gridview->contentItem();
     QVERIFY(contentItem != 0);
 
-    // current item should be third item
+    // currentIndex is initialized to 35
+    // currentItem should be in view
     QCOMPARE(gridview->currentIndex(), 35);
     QCOMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 35));
     QCOMPARE(gridview->currentItem()->y(), gridview->highlightItem()->y());
     QCOMPARE(gridview->contentY(), 400.0);
 
-    gridview->setCurrentIndex(0);
+    // changing model should reset currentIndex to 0
+    QmlListModel model;
+    for (int i = 0; i < 60; i++)
+        model.addItem("Item" + QString::number(i), QString::number(i));
+    ctxt->setContextProperty("testModel", &model);
+
     QCOMPARE(gridview->currentIndex(), 0);
+    QCOMPARE(gridview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
+
     // confirm that the velocity is updated
+    gridview->setCurrentIndex(35);
     QTRY_VERIFY(gridview->verticalVelocity() != 0.0);
+    gridview->setCurrentIndex(0);
+    QTRY_VERIFY(gridview->verticalVelocity() == 0.0);
 
     // footer should become visible if it is out of view, and then current index moves to the first row
     window->rootObject()->setProperty("showFooter", true);
index 9b0cb2c..50c4a0a 100644 (file)
@@ -2515,15 +2515,15 @@ void tst_QQuickListView::currentIndex_delayedItemCreation_data()
 
 void tst_QQuickListView::currentIndex()
 {
-    QmlListModel model;
+    QmlListModel initModel;
     for (int i = 0; i < 30; i++)
-        model.addItem("Item" + QString::number(i), QString::number(i));
+        initModel.addItem("Item" + QString::number(i), QString::number(i));
 
     QQuickView *window = new QQuickView(0);
     window->setGeometry(0,0,240,320);
 
     QQmlContext *ctxt = window->rootContext();
-    ctxt->setContextProperty("testModel", &model);
+    ctxt->setContextProperty("testModel", &initModel);
     ctxt->setContextProperty("testWrap", QVariant(false));
 
     QString filename(testFile("listview-initCurrent.qml"));
@@ -2537,17 +2537,27 @@ void tst_QQuickListView::currentIndex()
     QTRY_VERIFY(contentItem != 0);
     QTRY_COMPARE(QQuickItemPrivate::get(listview)->polishScheduled, false);
 
-    // current item should be 20th item at startup
-    // and current item should be in view
+    // currentIndex is initialized to 20
+    // currentItem should be in view
     QCOMPARE(listview->currentIndex(), 20);
     QCOMPARE(listview->contentY(), 100.0);
     QCOMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 20));
     QCOMPARE(listview->highlightItem()->y(), listview->currentItem()->y());
 
-    listview->setCurrentIndex(0);
+    // changing model should reset currentIndex to 0
+    QmlListModel model;
+    for (int i = 0; i < 30; i++)
+        model.addItem("Item" + QString::number(i), QString::number(i));
+    ctxt->setContextProperty("testModel", &model);
+
     QCOMPARE(listview->currentIndex(), 0);
+    QCOMPARE(listview->currentItem(), findItem<QQuickItem>(contentItem, "wrapper", 0));
+
     // confirm that the velocity is updated
+    listview->setCurrentIndex(20);
     QTRY_VERIFY(listview->verticalVelocity() != 0.0);
+    listview->setCurrentIndex(0);
+    QTRY_VERIFY(listview->verticalVelocity() == 0.0);
 
     // footer should become visible if it is out of view, and then current index is set to count-1
     window->rootObject()->setProperty("showFooter", true);