From d3a0bd57c7c3cecdeeb4ad9af3cf6bb4ee39964b Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 24 Feb 2010 17:48:20 +1000 Subject: Don't crash if the currentIndex is set while creating a delegate. Task-number: QTBUG-8456 --- .../graphicsitems/qdeclarativegridview.cpp | 4 ++- .../graphicsitems/qdeclarativelistview.cpp | 2 ++ .../qdeclarativegridview/data/setindex.qml | 33 ++++++++++++++++++++++ .../tst_qdeclarativegridview.cpp | 15 ++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/auto/declarative/qdeclarativegridview/data/setindex.qml diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp index aae5571..2ef6305 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview.cpp +++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp @@ -321,7 +321,7 @@ FxGridItem *QDeclarativeGridViewPrivate::createItem(int modelIndex) listItem->item->setParent(q->viewport()); unrequestedItems.remove(listItem->item); } - requestedIndex = 0; + requestedIndex = -1; return listItem; } @@ -888,6 +888,8 @@ int QDeclarativeGridView::currentIndex() const void QDeclarativeGridView::setCurrentIndex(int index) { Q_D(QDeclarativeGridView); + if (d->requestedIndex >= 0) // currently creating item + return; if (isComponentComplete() && d->isValid() && index != d->currentIndex && index < d->model->count() && index >= 0) { d->moveReason = QDeclarativeGridViewPrivate::SetIndex; cancelFlick(); diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp index d471749..77e3a15 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview.cpp +++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp @@ -1580,6 +1580,8 @@ int QDeclarativeListView::currentIndex() const void QDeclarativeListView::setCurrentIndex(int index) { Q_D(QDeclarativeListView); + if (d->requestedIndex >= 0) // currently creating item + return; if (isComponentComplete() && d->isValid() && index != d->currentIndex && index < d->model->count() && index >= 0) { d->moveReason = QDeclarativeListViewPrivate::SetIndex; cancelFlick(); diff --git a/tests/auto/declarative/qdeclarativegridview/data/setindex.qml b/tests/auto/declarative/qdeclarativegridview/data/setindex.qml new file mode 100644 index 0000000..908b365 --- /dev/null +++ b/tests/auto/declarative/qdeclarativegridview/data/setindex.qml @@ -0,0 +1,33 @@ +import Qt 4.6 + +Rectangle { + width: 200 + height: 200 + Component { + id: appDelegate + + Item { + id : wrapper + Script { + function startupFunction() + { + if (index == 5) view.currentIndex = index; + + } + } + Component.onCompleted: startupFunction(); + width: 30; height: 30 + Text { text: index } + } + } + + GridView { + id: view + objectName: "grid" + anchors.fill: parent + cellWidth: 30; cellHeight: 30 + model: 35 + delegate: appDelegate + focus: true + } +} diff --git a/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp b/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp index 9a7f517..9c7468d 100644 --- a/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp +++ b/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp @@ -66,6 +66,7 @@ private slots: void defaultValues(); void properties(); void positionViewAtIndex(); + void QTBUG_8456(); private: QDeclarativeView *createView(); @@ -657,6 +658,7 @@ void tst_QDeclarativeGridView::currentIndex() gridview->setFlow(QDeclarativeGridView::TopToBottom); + QEXPECT_FAIL("", "QTBUG-8475", Abort); QTest::keyClick(canvas, Qt::Key_Right); QCOMPARE(gridview->currentIndex(), 5); @@ -882,6 +884,19 @@ void tst_QDeclarativeGridView::positionViewAtIndex() delete canvas; } +void tst_QDeclarativeGridView::QTBUG_8456() +{ + QDeclarativeView *canvas = createView(); + + canvas->setSource(QUrl::fromLocalFile(SRCDIR "/data/setindex.qml")); + qApp->processEvents(); + + QDeclarativeGridView *gridview = findItem(canvas->rootObject(), "grid"); + QVERIFY(gridview != 0); + + QCOMPARE(gridview->currentIndex(), 0); +} + QDeclarativeView *tst_QDeclarativeGridView::createView() { QDeclarativeView *canvas = new QDeclarativeView(0); -- cgit v0.12