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>
\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
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) {
d->forceLayoutPolish();
}
}
+
connect(d->model, SIGNAL(modelUpdated(QQuickChangeSet,bool)),
this, SLOT(modelUpdated(QQuickChangeSet,bool)));
emit countChanged();
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));
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);
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"));
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);