diff options
author | Caroline Chao <caroline.chao@digia.com> | 2013-03-19 14:01:54 +0100 |
---|---|---|
committer | The Qt Project <gerrit-noreply@qt-project.org> | 2013-03-19 19:44:10 +0100 |
commit | 9b25a6f39ee59fe1bb4352a5f2e4ef4a91f1f2c3 (patch) | |
tree | c20b30ae5486a4105b0f2f22bf4fd4c6ce84f3f3 | |
parent | ea8b11259ad68a8ff44708786ecf54e5a8a272d8 (diff) |
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 <jpnurmi@digia.com>
Reviewed-by: Alan Alpert <aalpert@blackberry.com>
-rw-r--r-- | src/quick/items/qquickitemview.cpp | 4 | ||||
-rw-r--r-- | tests/auto/quick/qquicklistview/data/emptymodel.qml | 23 | ||||
-rw-r--r-- | tests/auto/quick/qquicklistview/tst_qquicklistview.cpp | 16 |
3 files changed, 40 insertions, 3 deletions
diff --git a/src/quick/items/qquickitemview.cpp b/src/quick/items/qquickitemview.cpp index b73fb8c5b6..be056d13fd 100644 --- a/src/quick/items/qquickitemview.cpp +++ b/src/quick/items/qquickitemview.cpp @@ -248,10 +248,8 @@ QQuickItemView::~QQuickItemView() QQuickItem *QQuickItemView::currentItem() const { Q_D(const QQuickItemView); - if (!d->currentItem) - return 0; const_cast<QQuickItemViewPrivate*>(d)->applyPendingChanges(); - return d->currentItem->item; + return d->currentItem ? d->currentItem->item : 0; } QVariant QQuickItemView::model() const 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 <class T> 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<QQuickListView*>(); + 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" |