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
topic.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-positioning-topic.html
6\title Important Concepts In Qt Quick - Positioning
7\brief Overview of positioning concepts
8\ingroup explanations-programminglanguages
9
10Visual items in QML can be positioned in a variety of ways. The most important
11positioning-related concept is that of anchoring, a form of relative
12positioning where items can be anchored (or attached) to each other at certain
13boundaries. Other positioning concepts include absolute positioning,
14positioning with coordinate bindings, positioners, and layouts.
15
16
17\section1 Manual Positioning
18
19Items can be positioned manually. If the user-interface is going to be
20static, manual positioning provides the most efficient form of positioning.
21
22In any user-interface, the visual types exist at a particular location in
23the screen coordinates at any instant in time. While fluidly animated and
24dynamic user-interfaces are a major focus of Qt Quick, statically-positioned
25user interfaces are still a viable option. What's more, if the position of
26those types does not change, it can often be more performant to specify
27the position manually than to use the more dynamic positioning methods
28documented in proceeding sections.
29
30In Qt Quick, every visual object is positioned within the
31\l{Concepts - Visual Coordinates in Qt Quick}{coordinate system}
32provided by the Qt Quick visual canvas. As described in that document, the
33x and y coordinates of a visual object are relative to those of its visual
34parent, with the top-left corner having the coordinate (0, 0).
35
36Thus, the following example will display two rectangles positioned manually:
37
38\table
39 \header
40 \li Example Code
41 \li Resultant Layout
42
43 \row
44 \li
45 \qml
46 import QtQuick 2.0
47
48 Item {
49 width: 200
50 height: 200
51
52 Rectangle {
53 x: 50
54 y: 50
55 width: 100
56 height: 100
57 color: "green"
58 }
59
60 Rectangle {
61 x: 100
62 y: 100
63 width: 50
64 height: 50
65 color: "yellow"
66 }
67 }
68 \endqml
69 \li
70 \image manual-layout.png
71\endtable
72
73\section1 Positioning With Bindings
74
75Items may also be positioned by assigning binding expressions to the properties
76associated with their location in the visual canvas. This type of positioning
77is the most highly dynamic, however some performance cost is associated with
78positioning items in this manner.
79
80The position and dimensions of a visual object can also be set through property
81bindings. This has the advantage that the values will automatically be updated
82as the dependencies of the bindings change. For example, the width of one
83Rectangle might depend on the width of the Rectangle next to it.
84
85While bindings provide a very flexible and intuitive way of creating dynamic
86layouts, it should be noted that there is some performance cost associated with
87them, and where possible, pristine Anchor layouts should be preferred.
88
89
90\section1 Anchors
91
92Anchors allow an item to be placed either adjacent to or inside of another,
93by attaching one or more of the item's anchor-points (boundaries) to an
94anchor-point of the other. These anchors will remain even if the dimensions
95or location of one of the items changes, allowing for highly dynamic
96user-interfaces.
97
98A visual object can be thought of as having various anchor-points (or more
99correctly, anchor-lines). Other items can be anchored to those points, which
100means that as any object changes, the other objects which are anchored to it
101will adjust automatically to maintain the anchoring.
102
103Qt Quick provides anchors as a top-level concept. See the documentation about
104\l{qtquick-positioning-anchors.html}{positioning with anchors}
105for in-depth information on the topic.
106
107It is important to note that anchor-based layouts are generally far more
108performant than binding-based layouts, if pristine. A "pristine" anchor layout
109is one which uses only anchors (with object nesting) to determine the
110positioning, whereas a "contaminated" anchor layout is one which uses both
111anchoring and bindings (either on position-related [x,y] properties or on
112dimension-related [width,height] properties) to determine the position.
113
114\section1 Positioners
115
116Qt Quick also provides some built-in positioner items. For many use cases, the best
117positioner to use is a simple grid, row, or column, and Qt Quick provides items which
118will position children in these formations in the most efficient manner possible.
119See the documentation on \l{qtquick-positioning-layouts.html}{item positioners types}
120for more information about utilizing pre-defined positioners.
121
122\section1 Layouts
123
124From Qt 5.1, the module \l {Qt Quick Layouts} can also be used to arrange Qt Quick
125items in a user interface. Unlike positioners, the types in Qt Quick Layouts manage
126both the positions and sizes of items in a declarative interface. They are well
127suited for resizable user interfaces.
128
129\section1 Right-To-Left Support
130
131The directionality of the written form of a language often has a great impact
132on how the visual types of a user-interface should be positioned. Qt Quick
133supports right-to-left positioning of types through the predefined-layouts
134as well as right-to-left text layouts.
135
136Please see the documentation about
137\l{qtquick-positioning-righttoleft.html}
138{right-to-left support in Qt Quick} for in-depth information on the topic.
139
140
141*/