diff --git a/src/quick/items/qquickitemview.cpp b/src/quick/items/qquickitemview.cpp index c58572de0f..66be3c79f8 100644 --- a/src/quick/items/qquickitemview.cpp +++ b/src/quick/items/qquickitemview.cpp @@ -235,7 +235,9 @@ void QQuickItemView::setModel(const QVariant &m) if (isComponentComplete()) { d->updateSectionCriteria(); d->refill(); - d->currentIndex = -1; + /* Setting currentIndex to -2 ensures that we always enter the "currentIndex changed" + code path in setCurrentIndex, updating bindings depending on currentIndex.*/ + d->currentIndex = -2; setCurrentIndex(d->model->count() > 0 ? 0 : -1); d->updateViewport(); diff --git a/tests/auto/quick/qquicklistview/data/listview-initCurrent.qml b/tests/auto/quick/qquicklistview/data/listview-initCurrent.qml index 8aff649a67..41d5bd3491 100644 --- a/tests/auto/quick/qquicklistview/data/listview-initCurrent.qml +++ b/tests/auto/quick/qquicklistview/data/listview-initCurrent.qml @@ -7,6 +7,7 @@ Rectangle { property bool showHeader: false property bool showFooter: false property int currentItemChangedCount: 0 + property string s: ""+list.currentIndex width: 240 height: 320 diff --git a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp index dac95924b5..3976dbc0f0 100644 --- a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp +++ b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp @@ -2891,7 +2891,11 @@ void tst_QQuickListView::currentIndex() // empty model should reset currentIndex to -1 QaimModel emptyModel; + window->rootObject()->setProperty("currentItemChangedCount", QVariant(0)); + QVERIFY(QQmlProperty(window->rootObject(), "s").read().toString() != QLatin1String("-1")); ctxt->setContextProperty("testModel", &emptyModel); + QCOMPARE(QQmlProperty(window->rootObject(), "s").read().toString(), "-1"); + QCOMPARE(window->rootObject()->property("currentItemChangedCount").toInt(), 1); QCOMPARE(listview->currentIndex(), -1); delete window;