From cdf868033bbd7bf5a996c67fa56f8ac15e755115 Mon Sep 17 00:00:00 2001 From: Andrew den Exter Date: Fri, 7 Oct 2011 13:06:26 +1000 Subject: [PATCH] Fix ListView components being unable to access context properties. Where avaialable use the components creation context instead of the views context as the root context for the new item. Task-number: QTBUG-21865 Change-Id: I07e564548de57d58413dc0d7cd151bd8a90886e7 Reviewed-on: http://codereview.qt-project.org/6199 Reviewed-by: Qt Sanity Bot Reviewed-by: Bea Lam --- src/declarative/items/qsgitemview.cpp | 4 +++- src/declarative/items/qsglistview.cpp | 4 +++- src/declarative/items/qsgvisualdatamodel.cpp | 4 +++- .../declarative/qsggridview/data/ComponentView.qml | 14 +++++++++++++ .../qsggridview/data/creationContext.qml | 5 +++++ .../declarative/qsggridview/tst_qsggridview.cpp | 24 +++++++++++++++++++++- .../declarative/qsglistview/data/ComponentView.qml | 16 +++++++++++++++ .../qsglistview/data/creationContext.qml | 5 +++++ .../declarative/qsglistview/tst_qsglistview.cpp | 24 +++++++++++++++++++++- .../declarative/qsgpathview/data/ComponentView.qml | 17 +++++++++++++++ .../qsgpathview/data/creationContext.qml | 5 +++++ .../declarative/qsgpathview/tst_qsgpathview.cpp | 16 +++++++++++++++ 12 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 tests/auto/declarative/qsggridview/data/ComponentView.qml create mode 100644 tests/auto/declarative/qsggridview/data/creationContext.qml create mode 100644 tests/auto/declarative/qsglistview/data/ComponentView.qml create mode 100644 tests/auto/declarative/qsglistview/data/creationContext.qml create mode 100644 tests/auto/declarative/qsgpathview/data/ComponentView.qml create mode 100644 tests/auto/declarative/qsgpathview/data/creationContext.qml diff --git a/src/declarative/items/qsgitemview.cpp b/src/declarative/items/qsgitemview.cpp index b34ed82..9415f9e 100644 --- a/src/declarative/items/qsgitemview.cpp +++ b/src/declarative/items/qsgitemview.cpp @@ -1624,7 +1624,9 @@ QSGItem *QSGItemViewPrivate::createComponentItem(QDeclarativeComponent *componen QSGItem *item = 0; if (component) { - QDeclarativeContext *context = new QDeclarativeContext(qmlContext(q)); + QDeclarativeContext *creationContext = component->creationContext(); + QDeclarativeContext *context = new QDeclarativeContext( + creationContext ? creationContext : qmlContext(q)); QObject *nobj = component->create(context); if (nobj) { QDeclarative_setParent_noEvent(context, nobj); diff --git a/src/declarative/items/qsglistview.cpp b/src/declarative/items/qsglistview.cpp index 7f9ce3a..beeeda4 100644 --- a/src/declarative/items/qsglistview.cpp +++ b/src/declarative/items/qsglistview.cpp @@ -832,7 +832,9 @@ QSGItem * QSGListViewPrivate::getSectionItem(const QString §ion) QDeclarativeContext *context = QDeclarativeEngine::contextForObject(sectionItem)->parentContext(); context->setContextProperty(QLatin1String("section"), section); } else { - QDeclarativeContext *context = new QDeclarativeContext(qmlContext(q)); + QDeclarativeContext *creationContext = sectionCriteria->delegate()->creationContext(); + QDeclarativeContext *context = new QDeclarativeContext( + creationContext ? creationContext : qmlContext(q)); context->setContextProperty(QLatin1String("section"), section); QObject *nobj = sectionCriteria->delegate()->beginCreate(context); if (nobj) { diff --git a/src/declarative/items/qsgvisualdatamodel.cpp b/src/declarative/items/qsgvisualdatamodel.cpp index f5c86b8..e0d353a 100644 --- a/src/declarative/items/qsgvisualdatamodel.cpp +++ b/src/declarative/items/qsgvisualdatamodel.cpp @@ -939,7 +939,9 @@ QObject *QSGVisualDataModelPrivate::object(Compositor::Group group, int index, b if (!cacheItem->object) { QObject *data = m_adaptorModel->data(it.modelIndex()); - QDeclarativeContext *rootContext = new QDeclarativeContext(m_context); + QDeclarativeContext *creationContext = m_delegate->creationContext(); + QDeclarativeContext *rootContext = new QDeclarativeContext( + creationContext ? creationContext : m_context.data()); QDeclarativeContext *ctxt = rootContext; if (m_adaptorModel->flags() & QSGVisualAdaptorModel::ProxiedObject) { if (QSGVisualAdaptorModelProxyInterface *proxy = qobject_cast(data)) { diff --git a/tests/auto/declarative/qsggridview/data/ComponentView.qml b/tests/auto/declarative/qsggridview/data/ComponentView.qml new file mode 100644 index 0000000..12ab6c9 --- /dev/null +++ b/tests/auto/declarative/qsggridview/data/ComponentView.qml @@ -0,0 +1,14 @@ +import QtQuick 2.0 + +GridView { + id: view + + property string title + + width: 100; height: 100; + + model: 1 + delegate: Text { objectName: "listItem"; text: view.title } + header: Text { objectName: "header"; text: view.title } + footer: Text { objectName: "footer"; text: view.title } +} diff --git a/tests/auto/declarative/qsggridview/data/creationContext.qml b/tests/auto/declarative/qsggridview/data/creationContext.qml new file mode 100644 index 0000000..79a6827 --- /dev/null +++ b/tests/auto/declarative/qsggridview/data/creationContext.qml @@ -0,0 +1,5 @@ +import QtQuick 2.0 + +ComponentView { + title: "Hello!" +} diff --git a/tests/auto/declarative/qsggridview/tst_qsggridview.cpp b/tests/auto/declarative/qsggridview/tst_qsggridview.cpp index 24471e9..a5b7c5b 100644 --- a/tests/auto/declarative/qsggridview/tst_qsggridview.cpp +++ b/tests/auto/declarative/qsggridview/tst_qsggridview.cpp @@ -64,7 +64,7 @@ class tst_QSGGridView : public QObject public: tst_QSGGridView(); -private slots: +//private slots: void initTestCase(); void cleanupTestCase(); void items(); @@ -107,6 +107,8 @@ private slots: void testQtQuick11Attributes_data(); void columnCount(); void margins(); +private slots: + void creationContext(); private: QSGView *createView(); @@ -2926,6 +2928,26 @@ void tst_QSGGridView::margins() } } +void tst_QSGGridView::creationContext() +{ + QSGView canvas; + canvas.setGeometry(0,0,240,320); + canvas.setSource(QUrl::fromLocalFile(SRCDIR "/data/creationContext.qml")); + qApp->processEvents(); + + QSGItem *rootItem = qobject_cast(canvas.rootObject()); + QVERIFY(rootItem); + QVERIFY(rootItem->property("count").toInt() > 0); + + QSGItem *item; + QVERIFY(item = rootItem->findChild("listItem")); + QCOMPARE(item->property("text").toString(), QString("Hello!")); + QVERIFY(item = rootItem->findChild("header")); + QCOMPARE(item->property("text").toString(), QString("Hello!")); + QVERIFY(item = rootItem->findChild("footer")); + QCOMPARE(item->property("text").toString(), QString("Hello!")); +} + QSGView *tst_QSGGridView::createView() { QSGView *canvas = new QSGView(0); diff --git a/tests/auto/declarative/qsglistview/data/ComponentView.qml b/tests/auto/declarative/qsglistview/data/ComponentView.qml new file mode 100644 index 0000000..3e87be8 --- /dev/null +++ b/tests/auto/declarative/qsglistview/data/ComponentView.qml @@ -0,0 +1,16 @@ +import QtQuick 2.0 + +ListView { + id: view + + property string title + + width: 100; height: 100; + + model: 1 + delegate: Text { objectName: "listItem"; text: view.title } + header: Text { objectName: "header"; text: view.title } + footer: Text { objectName: "footer"; text: view.title } + section.delegate: Text { objectName: "section"; text: view.title } + section.property: "modelData" +} diff --git a/tests/auto/declarative/qsglistview/data/creationContext.qml b/tests/auto/declarative/qsglistview/data/creationContext.qml new file mode 100644 index 0000000..79a6827 --- /dev/null +++ b/tests/auto/declarative/qsglistview/data/creationContext.qml @@ -0,0 +1,5 @@ +import QtQuick 2.0 + +ComponentView { + title: "Hello!" +} diff --git a/tests/auto/declarative/qsglistview/tst_qsglistview.cpp b/tests/auto/declarative/qsglistview/tst_qsglistview.cpp index 6e8b247..cca6cd3 100644 --- a/tests/auto/declarative/qsglistview/tst_qsglistview.cpp +++ b/tests/auto/declarative/qsglistview/tst_qsglistview.cpp @@ -134,6 +134,7 @@ private slots: void rightToLeft(); void test_mirroring(); void margins(); + void creationContext(); private: template void items(); @@ -3712,6 +3713,28 @@ void tst_QSGListView::qAbstractItemModel_clear() clear(); } +void tst_QSGListView::creationContext() +{ + QSGView canvas; + canvas.setGeometry(0,0,240,320); + canvas.setSource(QUrl::fromLocalFile(SRCDIR "/data/creationContext.qml")); + qApp->processEvents(); + + QSGItem *rootItem = qobject_cast(canvas.rootObject()); + QVERIFY(rootItem); + QVERIFY(rootItem->property("count").toInt() > 0); + + QSGItem *item; + QVERIFY(item = rootItem->findChild("listItem")); + QCOMPARE(item->property("text").toString(), QString("Hello!")); + QVERIFY(item = rootItem->findChild("header")); + QCOMPARE(item->property("text").toString(), QString("Hello!")); + QVERIFY(item = rootItem->findChild("footer")); + QCOMPARE(item->property("text").toString(), QString("Hello!")); + QVERIFY(item = rootItem->findChild("section")); + QCOMPARE(item->property("text").toString(), QString("Hello!")); +} + QSGView *tst_QSGListView::createView() { QSGView *canvas = new QSGView(0); @@ -3794,7 +3817,6 @@ void tst_QSGListView::dumpTree(QSGItem *parent, int depth) } } - QTEST_MAIN(tst_QSGListView) #include "tst_qsglistview.moc" diff --git a/tests/auto/declarative/qsgpathview/data/ComponentView.qml b/tests/auto/declarative/qsgpathview/data/ComponentView.qml new file mode 100644 index 0000000..b61033d --- /dev/null +++ b/tests/auto/declarative/qsgpathview/data/ComponentView.qml @@ -0,0 +1,17 @@ +import QtQuick 2.0 + +PathView { + id: view + + property string title + + width: 100; height: 100; + + model: 1 + delegate: Text { objectName: "listItem"; text: view.title } + + path: Path { + startX: 25; startY: 25; + PathLine { x: 75; y: 75 } + } +} diff --git a/tests/auto/declarative/qsgpathview/data/creationContext.qml b/tests/auto/declarative/qsgpathview/data/creationContext.qml new file mode 100644 index 0000000..79a6827 --- /dev/null +++ b/tests/auto/declarative/qsgpathview/data/creationContext.qml @@ -0,0 +1,5 @@ +import QtQuick 2.0 + +ComponentView { + title: "Hello!" +} diff --git a/tests/auto/declarative/qsgpathview/tst_qsgpathview.cpp b/tests/auto/declarative/qsgpathview/tst_qsgpathview.cpp index 1a7368e..6dd2a57 100644 --- a/tests/auto/declarative/qsgpathview/tst_qsgpathview.cpp +++ b/tests/auto/declarative/qsgpathview/tst_qsgpathview.cpp @@ -111,6 +111,7 @@ private slots: void treeModel(); void changePreferredHighlight(); void missingPercent(); + void creationContext(); private: QSGView *createView(); @@ -1066,6 +1067,21 @@ void tst_QSGPathView::changePreferredHighlight() delete canvas; } +void tst_QSGPathView::creationContext() +{ + QSGView canvas; + canvas.setGeometry(0,0,240,320); + canvas.setSource(QUrl::fromLocalFile(SRCDIR "/data/creationContext.qml")); + + QSGItem *rootItem = qobject_cast(canvas.rootObject()); + QVERIFY(rootItem); + QVERIFY(rootItem->property("count").toInt() > 0); + + QSGItem *item; + QVERIFY(item = findItem(rootItem, "listItem", 0)); + QCOMPARE(item->property("text").toString(), QString("Hello!")); +} + QSGView *tst_QSGPathView::createView() { QSGView *canvas = new QSGView(0); -- 2.7.4