From 22c68738bf2a83eb228b412fbd251bcdf8e7056e Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Mon, 23 Jul 2012 13:19:04 +1000 Subject: [PATCH] Changing model after componentComplete should reset currentIndex When the model is changed reset currentIndex back to 0. Task-number: QTBUG-26604 Change-Id: I1934e083819537d416acd85c75362daff382aa04 Reviewed-by: Bea Lam --- src/quick/doc/src/whatsnew.qdoc | 1 + src/quick/items/qquickitemview.cpp | 15 ++---------- .../quick/qquickgridview/tst_qquickgridview.cpp | 21 ++++++++++++++---- .../quick/qquicklistview/tst_qquicklistview.cpp | 22 ++++++++++++++----- 4 files changed, 36 insertions(+), 23 deletions(-) diff --git a/src/quick/doc/src/whatsnew.qdoc b/src/quick/doc/src/whatsnew.qdoc index 954506c..8eff21e 100644 --- a/src/quick/doc/src/whatsnew.qdoc +++ b/src/quick/doc/src/whatsnew.qdoc @@ -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 diff --git a/src/quick/items/qquickitemview.cpp b/src/quick/items/qquickitemview.cpp index a9022bd..8f1687f 100644 --- a/src/quick/items/qquickitemview.cpp +++ b/src/quick/items/qquickitemview.cpp @@ -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(); diff --git a/tests/auto/quick/qquickgridview/tst_qquickgridview.cpp b/tests/auto/quick/qquickgridview/tst_qquickgridview.cpp index 13753ea..110df07 100644 --- a/tests/auto/quick/qquickgridview/tst_qquickgridview.cpp +++ b/tests/auto/quick/qquickgridview/tst_qquickgridview.cpp @@ -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(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(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); diff --git a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp index 9b0cb2c..50c4a0a 100644 --- a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp +++ b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp @@ -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(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(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); -- 1.7.2.5