From 9b25a6f39ee59fe1bb4352a5f2e4ef4a91f1f2c3 Mon Sep 17 00:00:00 2001 From: Caroline Chao Date: Tue, 19 Mar 2013 14:01:54 +0100 Subject: QQuickItemView: Add guard before returning currentItem item And don't check if the currentItem is empty before calling applyPendingChanges(). And an autotest. (Patch from Steffen Imhof) Task-number: QTBUG-30227 Change-Id: Ia16362a6d0dce771f20772929c32e9306ed0e0f9 Reviewed-by: J-P Nurmi Reviewed-by: Alan Alpert --- .../auto/quick/qquicklistview/data/emptymodel.qml | 23 ++++++++++++++++++++++ .../quick/qquicklistview/tst_qquicklistview.cpp | 16 +++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 tests/auto/quick/qquicklistview/data/emptymodel.qml (limited to 'tests/auto/quick') diff --git a/tests/auto/quick/qquicklistview/data/emptymodel.qml b/tests/auto/quick/qquicklistview/data/emptymodel.qml new file mode 100644 index 0000000000..16bcd3f9ae --- /dev/null +++ b/tests/auto/quick/qquicklistview/data/emptymodel.qml @@ -0,0 +1,23 @@ +import QtQuick 2.0 +Rectangle { + ListModel { + id: model + ListElement { name: "hello"} + } + ListView { + id: list + model: model + delegate: Item { + } + } + function remove() { + model.remove(0) + isCurrentItemNull = list.currentItem === null //check no seg fault + } + + function add() { + model.append({name: "hello"}) + isCurrentItemNull = list.currentItem === null + } + property bool isCurrentItemNull +} diff --git a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp index c034fe83c8..3b4a41e203 100644 --- a/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp +++ b/tests/auto/quick/qquicklistview/tst_qquicklistview.cpp @@ -207,6 +207,7 @@ private slots: void parentBinding(); void defaultHighlightMoveDuration(); + void accessEmptyCurrentItem_QTBUG_30227(); private: template void items(const QUrl &source, bool forceLayout); @@ -6814,6 +6815,21 @@ void tst_QQuickListView::defaultHighlightMoveDuration() QCOMPARE(obj->property("highlightMoveDuration").toInt(), -1); } +void tst_QQuickListView::accessEmptyCurrentItem_QTBUG_30227() +{ + QQuickView *window = createView(); + window->setSource(testFileUrl("emptymodel.qml")); + + QQuickListView *listview = window->rootObject()->findChild(); + QTRY_VERIFY(listview != 0); + + QMetaObject::invokeMethod(window->rootObject(), "remove"); + QVERIFY(window->rootObject()->property("isCurrentItemNull").toBool()); + + QMetaObject::invokeMethod(window->rootObject(), "add"); + QVERIFY(!window->rootObject()->property("isCurrentItemNull").toBool()); +} + QTEST_MAIN(tst_QQuickListView) #include "tst_qquicklistview.moc" -- cgit v1.2.3