}
}
foreach (const QDeclarativeChangeSet::Insert &i, changeSet.inserts()) {
- itemCount += i.count;
if (moveId == -1) {
- if (newCurrentIndex >= i.index) {
+ if (itemCount && newCurrentIndex >= i.index) {
newCurrentIndex += i.count;
currentChanged = true;
} else if (newCurrentIndex < 0) {
newCurrentIndex = 0;
currentChanged = true;
+ } else if (newCurrentIndex == 0 && !itemCount) {
+ // this is the first item, set the initial current index
+ currentChanged = true;
}
} else if (moveId == i.moveId) {
newCurrentIndex = i.index + moveOffset;
}
+ itemCount += i.count;
}
}
void swapWithFirstItem();
void itemList();
+ void currentIndex_delayedItemCreation();
+ void currentIndex_delayedItemCreation_data();
void currentIndex();
void noCurrentIndex();
void enforceRange();
delete canvas;
}
+void tst_QSGListView::currentIndex_delayedItemCreation()
+{
+ QFETCH(bool, setCurrentToZero);
+
+ QSGView *canvas = createView();
+
+ TestModel model;
+
+ // test currentIndexChanged() is emitted even if currentIndex = 0 on start up
+ // (since the currentItem will have changed and that shares the same index)
+ canvas->rootContext()->setContextProperty("setCurrentToZero", setCurrentToZero);
+
+ canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/fillModelOnComponentCompleted.qml"));
+ qApp->processEvents();
+
+ QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");
+ QTRY_VERIFY(listview != 0);
+
+ QSGItem *contentItem = listview->contentItem();
+ QTRY_VERIFY(contentItem != 0);
+
+ QSignalSpy spy(listview, SIGNAL(currentIndexChanged()));
+ QCOMPARE(listview->currentIndex(), 0);
+ QTRY_COMPARE(spy.count(), 1);
+
+ delete canvas;
+}
+
+void tst_QSGListView::currentIndex_delayedItemCreation_data()
+{
+ QTest::addColumn<bool>("setCurrentToZero");
+
+ QTest::newRow("set to 0") << true;
+ QTest::newRow("don't set to 0") << false;
+}
+
void tst_QSGListView::currentIndex()
{
TestModel model;
TestModel model;
- canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/header1.qml"));
+ canvas->rootContext()->setContextProperty("setCurrentToZero", false);
+ canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/fillModelOnComponentCompleted.qml"));
qApp->processEvents();
QSGListView *listview = findItem<QSGListView>(canvas->rootObject(), "list");