From 290dc6caf65aeecaceb5a67b55cb33f0b059f98a Mon Sep 17 00:00:00 2001 From: Nils Jeisecke Date: Tue, 6 Aug 2013 13:58:15 +0200 Subject: Fix currentIndex in Qml itemView when assigning an empty model When assigning an empty model to e.g. a ListView after component initialization has been completed, currentIndex is now correctly set to -1. Change-Id: I540c034944009ccb8894bf84f400658ef9f0371f Task-number: QTBUG-32838 Reviewed-by: Alan Alpert --- dist/changes-5.1.2 | 25 ++++++++++++++++++++++ src/quick/items/qquickitemview.cpp | 2 +- .../quick/qquicklistview/tst_qquicklistview.cpp | 5 +++++ 3 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 dist/changes-5.1.2 diff --git a/dist/changes-5.1.2 b/dist/changes-5.1.2 new file mode 100644 index 0000000000..2ea62a4f6b --- /dev/null +++ b/dist/changes-5.1.2 @@ -0,0 +1,25 @@ +Qt 5.1.2 is a bug-fix release. It maintains both forward and backward +compatibility (source and binary) with Qt 5.1.0 and 5.1.1. + +For more details, refer to the online documentation included in this +distribution. The documentation is also available online: + + http://qt-project.org/doc/qt-5.1/ + +Some of the changes listed in this file include issue tracking numbers +corresponding to tasks in the Qt Bug Tracker: + + http://bugreports.qt-project.org/ + +Each of these identifiers can be entered in the bug tracker to obtain more +information about a particular change. + +**************************************************************************** +* Important Behavior Changes * +**************************************************************************** + + - QTBUG-32838: Setting the model property of ListView, GridView or PathView to + an empty model will now correctly reset currentIndex to -1. In prior versions + currentIndex was set to 0 regardless of the actual number of rows in the model + when the model property was changed after the view's initialization. + diff --git a/src/quick/items/qquickitemview.cpp b/src/quick/items/qquickitemview.cpp index f8f622a1b9..e19e780438 100644 --- a/src/quick/items/qquickitemview.cpp +++ b/src/quick/items/qquickitemview.cpp @@ -326,7 +326,7 @@ void QQuickItemView::setModel(const QVariant &model) d->updateSectionCriteria(); d->refill(); d->currentIndex = -1; - setCurrentIndex(0); + setCurrentIndex(d->model->count() > 0 ? 0 : -1); d->updateViewport(); if (d->transitioner && d->transitioner->populateTransition) { diff --git a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp index 2268f0754c..cd8cc53b40 100644 --- a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp +++ b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp @@ -2605,6 +2605,11 @@ void tst_QQuickListView::currentIndex() listview->setContentY(200); QTRY_VERIFY(!delegateVisible(listview->currentItem())); + // empty model should reset currentIndex to -1 + QaimModel emptyModel; + ctxt->setContextProperty("testModel", &emptyModel); + QCOMPARE(listview->currentIndex(), -1); + delete window; } -- cgit v1.2.3