From 4fbcb6a531bf424a8dbbbd0ca2947d9e15118885 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Thu, 5 May 2011 14:18:28 +1000 Subject: Views do not notify count changes properly The views don't have a valid count until both a model and a delegate are provided. But, countChanged() is not called when a delegate is set after the model, so bindings to count were not evaluated. Also test that count is updated for insertion/removal Change-Id: Ic82039a624c02f0bdb2862ac7a6e215df75bb314 Task-number: QTBUG-19037 Reviewed-by: Bea Lam --- src/declarative/graphicsitems/qdeclarativegridview.cpp | 3 +++ src/declarative/graphicsitems/qdeclarativelistview.cpp | 3 +++ src/declarative/graphicsitems/qdeclarativepathview.cpp | 3 +++ tests/auto/declarative/qdeclarativegridview/data/gridview1.qml | 1 + .../declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp | 3 +++ tests/auto/declarative/qdeclarativelistview/data/listviewtest.qml | 1 + .../declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp | 3 +++ tests/auto/declarative/qdeclarativepathview/data/datamodel.qml | 1 + tests/auto/declarative/qdeclarativepathview/data/pathview0.qml | 1 + .../declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp | 4 ++++ 10 files changed, 23 insertions(+) diff --git a/src/declarative/graphicsitems/qdeclarativegridview.cpp b/src/declarative/graphicsitems/qdeclarativegridview.cpp index 05e64b9..460f2c3 100644 --- a/src/declarative/graphicsitems/qdeclarativegridview.cpp +++ b/src/declarative/graphicsitems/qdeclarativegridview.cpp @@ -1499,6 +1499,7 @@ void QDeclarativeGridView::setDelegate(QDeclarativeComponent *delegate) d->ownModel = true; } if (QDeclarativeVisualDataModel *dataModel = qobject_cast(d->model)) { + int oldCount = dataModel->count(); dataModel->setDelegate(delegate); if (isComponentComplete()) { for (int i = 0; i < d->visibleItems.count(); ++i) @@ -1516,6 +1517,8 @@ void QDeclarativeGridView::setDelegate(QDeclarativeComponent *delegate) } d->moveReason = QDeclarativeGridViewPrivate::Other; } + if (oldCount != dataModel->count()) + emit countChanged(); emit delegateChanged(); } } diff --git a/src/declarative/graphicsitems/qdeclarativelistview.cpp b/src/declarative/graphicsitems/qdeclarativelistview.cpp index 2e9822e..cb40384 100644 --- a/src/declarative/graphicsitems/qdeclarativelistview.cpp +++ b/src/declarative/graphicsitems/qdeclarativelistview.cpp @@ -1813,6 +1813,7 @@ void QDeclarativeListView::setDelegate(QDeclarativeComponent *delegate) d->ownModel = true; } if (QDeclarativeVisualDataModel *dataModel = qobject_cast(d->model)) { + int oldCount = dataModel->count(); dataModel->setDelegate(delegate); if (isComponentComplete()) { for (int i = 0; i < d->visibleItems.count(); ++i) @@ -1831,6 +1832,8 @@ void QDeclarativeListView::setDelegate(QDeclarativeComponent *delegate) } d->updateViewport(); } + if (oldCount != dataModel->count()) + emit countChanged(); } emit delegateChanged(); } diff --git a/src/declarative/graphicsitems/qdeclarativepathview.cpp b/src/declarative/graphicsitems/qdeclarativepathview.cpp index aed849b..be2bd60 100644 --- a/src/declarative/graphicsitems/qdeclarativepathview.cpp +++ b/src/declarative/graphicsitems/qdeclarativepathview.cpp @@ -1021,9 +1021,12 @@ void QDeclarativePathView::setDelegate(QDeclarativeComponent *delegate) d->ownModel = true; } if (QDeclarativeVisualDataModel *dataModel = qobject_cast(d->model)) { + int oldCount = dataModel->count(); dataModel->setDelegate(delegate); d->modelCount = dataModel->count(); d->regenerate(); + if (oldCount != dataModel->count()) + emit countChanged(); emit delegateChanged(); } } diff --git a/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml b/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml index caa28d6..a2b0a91 100644 --- a/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml +++ b/tests/auto/declarative/qdeclarativegridview/data/gridview1.qml @@ -2,6 +2,7 @@ import QtQuick 1.1 Rectangle { id: root + property int count: grid.count property bool showHeader: false property bool showFooter: false property int added: -1 diff --git a/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp b/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp index c8e7817..dc79370 100644 --- a/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp +++ b/tests/auto/declarative/qdeclarativegridview/tst_qdeclarativegridview.cpp @@ -222,6 +222,7 @@ void tst_QDeclarativeGridView::items() QTRY_VERIFY(contentItem != 0); QTRY_COMPARE(gridview->count(), model.count()); + QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item for (int i = 0; i < model.count(); ++i) { @@ -305,6 +306,7 @@ void tst_QDeclarativeGridView::inserted() QTRY_VERIFY(contentItem != 0); model.insertItem(1, "Will", "9876"); + QCOMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item @@ -382,6 +384,7 @@ void tst_QDeclarativeGridView::removed() QTRY_VERIFY(contentItem != 0); model.removeItem(1); + QCOMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QDeclarativeText *name = findItem(contentItem, "textName", 1); QTRY_VERIFY(name != 0); diff --git a/tests/auto/declarative/qdeclarativelistview/data/listviewtest.qml b/tests/auto/declarative/qdeclarativelistview/data/listviewtest.qml index 3cd6f42..75ddabb 100644 --- a/tests/auto/declarative/qdeclarativelistview/data/listviewtest.qml +++ b/tests/auto/declarative/qdeclarativelistview/data/listviewtest.qml @@ -6,6 +6,7 @@ Rectangle { height: 320 color: "#ffffff" + property int count: list.count property bool showHeader: false property bool showFooter: false property real hr: list.visibleArea.heightRatio diff --git a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp index 0c96587..8b90030 100644 --- a/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp +++ b/tests/auto/declarative/qdeclarativelistview/tst_qdeclarativelistview.cpp @@ -389,6 +389,7 @@ void tst_QDeclarativeListView::items() QTRY_VERIFY(listview->highlightItem() != 0); QTRY_COMPARE(listview->count(), model.count()); + QTRY_COMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item // current item should be first item @@ -517,6 +518,7 @@ void tst_QDeclarativeListView::inserted() model.insertItem(0, "Foo", "1111"); // zero index, and current item + QCOMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QTRY_COMPARE(contentItem->childItems().count(), model.count()+1); // assumes all are visible, +1 for the (default) highlight item name = findItem(contentItem, "textName", 0); @@ -583,6 +585,7 @@ void tst_QDeclarativeListView::removed(bool animated) QTRY_VERIFY(contentItem != 0); model.removeItem(1); + QCOMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QDeclarativeText *name = findItem(contentItem, "textName", 1); QTRY_VERIFY(name != 0); diff --git a/tests/auto/declarative/qdeclarativepathview/data/datamodel.qml b/tests/auto/declarative/qdeclarativepathview/data/datamodel.qml index 1322025..e2c4e3d 100644 --- a/tests/auto/declarative/qdeclarativepathview/data/datamodel.qml +++ b/tests/auto/declarative/qdeclarativepathview/data/datamodel.qml @@ -2,6 +2,7 @@ import QtQuick 1.0 PathView { id: pathview + property int viewCount: count objectName: "pathview" width: 240; height: 320 pathItemCount: testObject.pathItemCount diff --git a/tests/auto/declarative/qdeclarativepathview/data/pathview0.qml b/tests/auto/declarative/qdeclarativepathview/data/pathview0.qml index 04c7717..28b6fb9 100644 --- a/tests/auto/declarative/qdeclarativepathview/data/pathview0.qml +++ b/tests/auto/declarative/qdeclarativepathview/data/pathview0.qml @@ -2,6 +2,7 @@ import QtQuick 1.0 Rectangle { id: root + property int count: view.count property int currentA: -1 property int currentB: -1 property real delegateWidth: 60 diff --git a/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp b/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp index 46c3519..24b1115 100644 --- a/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp +++ b/tests/auto/declarative/qdeclarativepathview/tst_qdeclarativepathview.cpp @@ -253,6 +253,8 @@ void tst_QDeclarativePathView::items() QDeclarativePathView *pathview = findItem(canvas->rootObject(), "view"); QVERIFY(pathview != 0); + QCOMPARE(pathview->count(), model.count()); + QCOMPARE(canvas->rootObject()->property("count").toInt(), model.count()); QCOMPARE(pathview->childItems().count(), model.count()+1); // assumes all are visible, including highlight for (int i = 0; i < model.count(); ++i) { @@ -400,6 +402,7 @@ void tst_QDeclarativePathView::dataModel() model.insertItem(4, "orange", "10"); QTest::qWait(100); + QCOMPARE(canvas->rootObject()->property("viewCount").toInt(), model.count()); QTRY_COMPARE(findItems(pathview, "wrapper").count(), 14); QVERIFY(pathview->currentIndex() == 0); @@ -409,6 +412,7 @@ void tst_QDeclarativePathView::dataModel() QCOMPARE(text->text(), model.name(4)); model.removeItem(2); + QCOMPARE(canvas->rootObject()->property("viewCount").toInt(), model.count()); text = findItem(pathview, "myText", 2); QVERIFY(text); QCOMPARE(text->text(), model.name(2)); -- cgit v0.12