Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
visualparent.qdoc
Go to the documentation of this file.
1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5\page qtquick-visualcanvas-visualparent.html
6\title Concepts - Visual Parent in Qt Quick
7\brief Description of the concept of visual parent in Qt Quick
8
9\section1 Visual Parent
10
11When creating visual scenes with Qt Quick, it is important to understand the concept of the \e {visual parent}.
12
13The concept of the visual parent in Qt Quick is separate from, but related to, the concept of the \e {object parent}
14within the QObject parent hierarchy. All QML objects have an \e {object parent}, which is determined by the
15\l{qml-object-declarations}{object hierarchy} in which the object is declared. When working with the \c QtQuick
16module, the \l Item type is the base type for all visual items provided by this module, and it provides the
17concept of an additional \e {visual parent}, as defined by an item's \l {Item::}{parent} property. Every item
18has a visual parent; if an item's \l {Item::}{parent} property value is \c null, the item will not be rendered
19in the scene.
20
21Any object assigned to an item's \l{Item::}{data} property becomes a child of the item within its QObject hierarchy, for
22memory management purposes. Additionally, if an object added to the data property is of the \l Item type, it is also
23assigned to the \l Item::children property and becomes a child of the item within the visual scene hierarchy.
24(Most Qt Quick hierarchy crawling algorithms, especially the rendering algorithms, only consider the visual parent
25hierarchy.)
26
27For convenience, the Item \l {Item::}{data} property is its \l{Default Properties}{default property}. This means
28that any child item declared within an \l Item object without being assigned to a specific property is automatically
29assigned to the \l {Item::}{data} property and becomes a child of the item as described above. So, the two code
30blocks below produce the same result, and you will almost always see the form shown below left, rather than the
31explicit \c data assignment shown below right:
32
33\table
34\row
35\li
36\code
37import QtQuick 2.0
38
39Item {
40 width: 100; height: 100
41
42 Rectangle { width: 50;
43 height: 50;
44 color: "red" }
45}
46\endcode
47
48\li
49\code
50import QtQuick 2.0
51
52Item {
53 width: 100; height: 100
54
55 data: [
56 Rectangle { width: 50;
57 height: 50;
58 color: "red" }
59 ]
60}
61\endcode
62\endtable
63
64An item's visual parent can be changed at any time by setting its \l {Item::}{parent} property. Thus, an item's
65visual parent may not necessarily be the same as its object parent.
66
67When an item becomes the child of another item:
68
69\list
70\li The child's \l{Item::parent}{parent} refers to its parent item
71\li The parent's \l{Item::children}{children} and \l{Item::childrenRect.x}{childrenRect} properties takes that
72 child into account
73\endlist
74
75Declaring an item as a child of another does not automatically mean that the child item will be appropriately
76positioned or sized to fit within its parent. Some QML types may have in-built behaviors that affect the positioning
77of child items — for example, a \l Row object automatically re-positions its children into a horizontal formation —
78but these are behaviors enforced by the types' own specific implementations. Additionally, a parent item will not
79automatically clip its children to visually contain them within the parent's visual bounds, unless its \l{Item::}{clip}
80property is set to true.
81
82The visual parent of an item may come under consideration in particular circumstances, as described in the sections
83below.
84
85
86\section2 Item Coordinates
87As item coordinates are relative to the visual parent, they can be affected by changes to the visual hierarchy. See
88the \l{qtquick-visualcanvas-coordinates.html}{Visual Coordinates} concept page for more detail.
89
90\section2 Stacking Order
91
92Qt Quick items use a recursive drawing algorithm for determining which items are drawn on top in case of collisions.
93In general items are drawn on top of their parent items, in the order they were created (or specified in the QML file).
94So in the following example, the blue rectangle will be drawn on top of the green rectangle:
95
96\snippet qml/visualparent.qml 0
97\image visual-parent-example.png
98
99Because the algorithm recurses through the visual item hierarchy, any children of the green rectangle will also be drawn beneath
100the blue rectangle and beneath any of the blue rectangle's children.
101
102Stacking order can be influenced with the \l Item::z property. Z values below 0 will stack below the parent, and if z
103values are assigned then siblings will stack in z-order (with creation order used to break ties). Z values only affect
104stacking compared to siblings and the parent item. If you have an item which is obscured by a subtree rooted above its
105parent item, no z value on that item will increase its stacking order to stack above that subtree. To stack that item
106above the other subtree you'll have to alter z values farther up in the hierarchy, or re-arrange the visual item
107hierarchy.
108
109\snippet qml/visualparent2.qml 0
110\image visual-parent-example2.png
111
112In the above example, the red rectangle has a high z value, but is still stacked below the blue rectangle. This is
113because it is a child of the green rectangle, and the green rectangle is a sibling of the blue rectangle. The z value
114of the green rectangle is lower than that of the blue rectangle, so the green rectangle and all children will be
115stacked beneath the blue rectangle.
116
117\section2 Canvas Ownership
118
119The definition of what is rendered in a Qt Quick scene is the visual item tree rooted at QQuickWindow::contentItem.
120Therefore to add an Item to a specific Qt Quick scene for rendering it needs to become a visual hierarchy child of an
121Item already in the visual item hierarchy, such as QQuickWindow::contentItem.
122*/