diff options
author | J-P Nurmi <jpnurmi@qt.io> | 2017-06-01 10:48:00 +0200 |
---|---|---|
committer | Shawn Rutledge <shawn.rutledge@qt.io> | 2017-06-02 12:48:16 +0000 |
commit | af6655885cf19de69d5f57ac9daee9f6b9935a70 (patch) | |
tree | 1bd1212a370ec56507dce32624ee3a9d4d472569 /tests/auto | |
parent | ac02a71a9cb8e80014218ba7de46f4f914b6e00c (diff) |
QQuickWindow/View: set a QObject-parent on the root item
People are often confused why eg. the objects from:
window->contentItem()->findChildren()
are not included in window->findChildren(). This change connects
the item tree to the window's object tree to make findChild() and
findChildren() produce expected results. The same technique is
already used for QQuickFlickable's contentItem.
[ChangeLog][QtQuick][QQuickWindow] Set the window as the QObject-parent
of the contentItem to ensure consistent behavior for calling
findChildren() on QQuickWindow and QQuickWindow::contentItem.
[ChangeLog][QtQuick][QQuickView] Set the window's contentItem as the
QObject-parent of the rootObject to ensure consistent behavior for
calling findChildren() on QQuickWindow::contentItem and
QQuickView::rootObject.
Change-Id: Idb7834eb5e560088ca849e6ce90e6fa3b3ae3e91
Reviewed-by: Mitch Curtis <mitch.curtis@qt.io>
Diffstat (limited to 'tests/auto')
-rw-r--r-- | tests/auto/quick/qquickview/data/findChild.qml | 9 | ||||
-rw-r--r-- | tests/auto/quick/qquickview/tst_qquickview.cpp | 40 | ||||
-rw-r--r-- | tests/auto/quick/qquickwindow/tst_qquickwindow.cpp | 24 |
3 files changed, 73 insertions, 0 deletions
diff --git a/tests/auto/quick/qquickview/data/findChild.qml b/tests/auto/quick/qquickview/data/findChild.qml new file mode 100644 index 0000000000..8cbc46abe2 --- /dev/null +++ b/tests/auto/quick/qquickview/data/findChild.qml @@ -0,0 +1,9 @@ +import QtQuick 2.0 +Item { + width: 200 + height: 200 + objectName: "rootObject" + Item { + objectName: "rootObjectChild" + } +} diff --git a/tests/auto/quick/qquickview/tst_qquickview.cpp b/tests/auto/quick/qquickview/tst_qquickview.cpp index fa9192edb4..04d21457e6 100644 --- a/tests/auto/quick/qquickview/tst_qquickview.cpp +++ b/tests/auto/quick/qquickview/tst_qquickview.cpp @@ -70,6 +70,7 @@ private slots: void resizemodeitem(); void errors(); void engine(); + void findChild(); }; @@ -265,6 +266,45 @@ void tst_QQuickView::engine() delete view4; } +void tst_QQuickView::findChild() +{ + QQuickView view; + view.setSource(testFileUrl("findChild.qml")); + + // QQuickView + // |_ QQuickWindow::contentItem + // | |_ QQuickView::rootObject: QML Item("rootObject") (findChild.qml) + // | | |_ QML Item("rootObjectChild") (findChild.qml) + // | |_ QObject("contentItemChild") + // |_ QObject("viewChild") + + QObject *viewChild = new QObject(&view); + viewChild->setObjectName("viewChild"); + + QObject *contentItemChild = new QObject(view.contentItem()); + contentItemChild->setObjectName("contentItemChild"); + + QObject *rootObject = view.rootObject(); + QVERIFY(rootObject); + + QObject *rootObjectChild = rootObject->findChild<QObject *>("rootObjectChild"); + QVERIFY(rootObjectChild); + + QCOMPARE(view.findChild<QObject *>("viewChild"), viewChild); + QCOMPARE(view.findChild<QObject *>("contentItemChild"), contentItemChild); + QCOMPARE(view.findChild<QObject *>("rootObject"), rootObject); + QCOMPARE(view.findChild<QObject *>("rootObjectChild"), rootObjectChild); + + QVERIFY(!view.contentItem()->findChild<QObject *>("viewChild")); // sibling + QCOMPARE(view.contentItem()->findChild<QObject *>("contentItemChild"), contentItemChild); + QCOMPARE(view.contentItem()->findChild<QObject *>("rootObject"), rootObject); + QCOMPARE(view.contentItem()->findChild<QObject *>("rootObjectChild"), rootObjectChild); + + QVERIFY(!view.rootObject()->findChild<QObject *>("viewChild")); // ancestor + QVERIFY(!view.rootObject()->findChild<QObject *>("contentItemChild")); // cousin + QVERIFY(!view.rootObject()->findChild<QObject *>("rootObject")); // self +} + QTEST_MAIN(tst_QQuickView) #include "tst_qquickview.moc" diff --git a/tests/auto/quick/qquickwindow/tst_qquickwindow.cpp b/tests/auto/quick/qquickwindow/tst_qquickwindow.cpp index 1d6547c5be..daa5e53730 100644 --- a/tests/auto/quick/qquickwindow/tst_qquickwindow.cpp +++ b/tests/auto/quick/qquickwindow/tst_qquickwindow.cpp @@ -375,6 +375,8 @@ private slots: void testDragEventPropertyPropagation(); + void findChild(); + private: QTouchDevice *touchDevice; QTouchDevice *touchDeviceWithVelocity; @@ -2831,6 +2833,28 @@ void tst_qquickwindow::testDragEventPropertyPropagation() } } +void tst_qquickwindow::findChild() +{ + QQuickWindow window; + + // QQuickWindow + // |_ QQuickWindow::contentItem + // | |_ QObject("contentItemChild") + // |_ QObject("viewChild") + + QObject *windowChild = new QObject(&window); + windowChild->setObjectName("windowChild"); + + QObject *contentItemChild = new QObject(window.contentItem()); + contentItemChild->setObjectName("contentItemChild"); + + QCOMPARE(window.findChild<QObject *>("windowChild"), windowChild); + QCOMPARE(window.findChild<QObject *>("contentItemChild"), contentItemChild); + + QVERIFY(!window.contentItem()->findChild<QObject *>("viewChild")); // sibling + QCOMPARE(window.contentItem()->findChild<QObject *>("contentItemChild"), contentItemChild); +} + QTEST_MAIN(tst_qquickwindow) #include "tst_qquickwindow.moc" |