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
qtquicklayouts-overview.qdoc
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page qtquicklayouts-overview.html
6 \title Qt Quick Layouts Overview
7 \brief A set of APIs for arranging QML items in a user interface.
8
9 Use Qt Quick Layouts to arrange items in a user interface. Qt Quick Layouts
10 resize their items, which makes them well suited for resizable user
11 interfaces.
12
13 \section1 Key features
14
15 Some of the key features of Qt Quick Layouts are:
16
17 \list
18 \li \l{Layout::alignment}{Align} items with the
19 \l{Layout::alignment}{Layout.alignment} property.
20 \li Specify \l{Layout::fillWidth}{resizable items} with the
21 \l{Layout::fillWidth}{Layout.fillWidth} and
22 \l{Layout::fillHeight}{Layout.fillHeight} properties.
23 \li Set \l{Size constraints}{size constraints} with the
24 \l{Layout::minimumWidth}{Layout.minimumWidth},
25 \l{Layout::preferredWidth}{Layout.preferredWidth}, and
26 \l{Layout::maximumWidth}{Layout.maximumWidth} properties -- "Width"
27 can be replaced with "Height" for specifying similar constraints to
28 the height.
29 \li You can specify \l {RowLayout::spacing}{spacing} with
30 \l{RowLayout::spacing}{spacing},
31 \l{GridLayout::rowSpacing}{rowSpacing},
32 or \l{GridLayout::columnSpacing}{columnSpacing}.
33 \li Stretch items both horizontally and vertically with
34 \l{Layout::horizontalStretchFactor}{stretch factors}.
35 \endlist
36
37 In addition, GridLayout adds these features:
38 \list
39 \li \l{Layout::row}{Grid coordinates}, controlled by the
40 \l{Layout::row}{Layout.row} and \l{Layout::column}{Layout.column}
41 properties.
42 \li \l{GridLayout::flow}{Automatic grid coordinates} used together with
43 the \l{GridLayout::flow}{flow}, \l{GridLayout::rows}{rows}, and
44 \l{GridLayout::columns}{columns} properties.
45 \li \l{Layout::columnSpan}{Spans} across rows or columns, that you can
46 specify with the \l{Layout::rowSpan}{Layout.rowSpan} and
47 \l{Layout::columnSpan}{Layout.columnSpan} properties.
48 \endlist
49
50 \section1 Getting started
51
52 To get started using Qt Quick Layouts, import the QML types into your
53 application with the following import statement in your \c {.qml} file:
54
55 \qml
56 import QtQuick.Layouts
57 \endqml
58
59 The next step is to create a \l {A simple layout}{simple layout}. You can
60 also study the \l {Qt Quick Layouts - Basic Example}.
61
62 \section2 A simple layout
63
64 The intention of using a layout is to rearrange its children whenever the
65 layout changes size. This means the application must ensure that the layout
66 gets resized. In the following snippet, the \e RowLayout ensures that by
67 specifying \c{anchors.fill: parent}. However, you can also achieve this by
68 other means, such as specifying the \l{Item::width}{width} and
69 \l{Item::height}{height} properties. In the same snippet, the \e {orange
70 Rectangle} has a fixed size of \e 100 by \e 150 pixels, and the \e {plum
71 Rectangle} will expand to occupy all the space it gets allocated.
72
73 \snippet qml/layout-simple.qml 1
74 \target simple-layout-snippet
75
76 Layouts are responsible for their children's geometry.
77 This includes properties such as \l{Item::width}{width},
78 \l{Item::height}{height}, \l{Item::x}{x}, \l{Item::y}{y},
79 \l{Item::anchors}{anchors}), etc.
80
81 \important Don't specify properties that influence the geometry of child
82 items in your application. Setting these properties on a child item causes
83 a conflict of interest, and the result is undefined. This also applies when
84 the child item is a layout. Therefore, only layouts with no parent layout
85 can have \c{anchors.fill: parent}.
86
87 \section3 Spacing
88 As seen in the \l {simple-layout-snippet}{previous snippet}, the \e spacing
89 for the \e RowLayout is set to \e 6. This ensures that all items in the
90 layout have 6 pixels of spacing between them:
91
92 \snippet qml/layout-simple.qml spacing
93
94 If you omit specifying a spacing value, the layout will use a default of
95 \e 5 pixels. The spacing, as well as the \e implicitWidth of any children,
96 contributes to the \e implicitWidth of the layout. This is important to
97 keep in mind if you rely on default behavior, as it may impact your layout
98 design. For example, the two \e {ColumnLayout}s both set
99 \e {Layout.fillWidth: true} in the following snippet. It's natural to think
100 that they would both get the same width. However, because of the default 5
101 pixel spacing between the items in the inner \e RowLayout, the
102 \e implicitWidth of the first \e ColumnLayout becomes larger, leaving less
103 room for the second one. For example:
104
105 \snippet qml/layout_with_default_spacing.qml layout-with-default-spacing
106
107 This snippet will produce a layout that looks like this:
108
109 \image layout-with-default-spacing.png "A QML layout with default spacing"
110
111 To ensure equal size of these two columns, you can either
112 \list a
113 \li set the spacing of the \e RowLayout to \c 0, or
114 \li set \e preferredWidth to equal values on both \e {ColumnLayout}s.
115 \endlist
116
117 \section2 Specifying preferred size
118 For each item, the effective preferred size may come from one of several
119 candidate properties. For determining the effective preferred size, an item
120 queries these candidate properties in the following order, and will use the
121 first candidate with a valid width or height.
122
123 \table
124 \header
125 \li Candidate properties
126 \li Description
127 \row
128 \li \l{Layout::preferredWidth}{Layout.preferredWidth} or
129 \l{Layout::preferredHeight}{Layout.preferredHeight}
130 \li These properties are supposed to be modified by the application if
131 the default implicit size does not give the optimal arrangement.
132 \row
133 \li \l{Item::implicitWidth}{implicitWidth} or
134 \l{Item::implicitHeight}{implicitHeight}.
135 \li These properties are supposed to be supplied by each item to give a
136 meaningful ideal size. For example, the size needed to display all
137 the contents of a \l Text type. An implicit width or height of \c 0
138 is interpreted as invalid.
139 \endtable
140
141 An item can specify \l{Layout::preferredWidth}{Layout.preferredWidth}
142 without having to specify
143 \l{Layout::preferredHeight}{Layout.preferredHeight}. In such cases, the
144 effective preferred height is determined from the
145 \l{Item::implicitHeight}{implicitHeight}.
146
147 \note If you don't specify neither preferredWidth nor implicitWidth, the
148 Layout will query \l {Item::}{width} as an ultimate value for the effective
149 preferred width. However, you shouldn't rely on \l {Item::}{width} as a
150 source for the effective preferred width, as that may cause unexpected
151 behavior. For instance, changing the \l{Item::}{width} or
152 \l{Item::height}{height} properties won't trigger a layout rearrangement,
153 or the layout might use the actual width and height -- not the width and
154 height specified in your QML file -- when forced to do a full rebuild.
155
156 \section2 Size constraints
157
158 Since an item can be resized by its layout, the layout needs to know the
159 \l{Layout::minimumWidth}{minimum}, \l{Layout::preferredWidth}{preferred},
160 and \l{Layout::maximumWidth}{maximum} sizes of all items where
161 \l{Layout::fillWidth}{Layout.fillWidth} or
162 \l{Layout::fillHeight}{Layout.fillHeight} is set to \e true.
163
164 The \l{Layout::preferredWidth}{preferred} width and height is the \e actual
165 width and height of an item, if the layout is not bound to a specific size
166 itself. If the layout is set to a specific size, it distributes additional
167 space based on the ratio of preferred sizes of its items, while taking
168 minimum and maximum sizes into account. The preferred and implicit sizes
169 act as ratios and weights when all items set \e fillWidth and
170 \e fillHeight.
171
172 For instance, the following produces a layout with two rectangles lying
173 side-by-side that stretches horizontally. The \e {orange Rectangle} can be
174 resized from 50x150 to 300x150, and the \e {plum Rectangle} can be resized
175 from 100x100 to ∞x100. As long as the minimum and maximum width of each
176 item isn't exceeded, the plum rectangle will have twice the width of the
177 orange one.
178
179 \snippet qml/windowconstraints.qml rowlayout
180
181 \image rowlayout-minimum.png "RowLayout at its minimum"
182
183 Combining each item's constraints gives these implicit constraints to the
184 layout element:
185
186 \table
187 \header
188 \li
189 \li minimum
190 \li preferred
191 \li maximum
192 \row
193 \li implicit constraints (width)
194 \li 156
195 \li 306
196 \li ∞ (\c Number.POSITIVE_INFINITY)
197 \row
198 \li implicit constraints (heights)
199 \li 150
200 \li 150
201 \li 150
202 \endtable
203
204 Thus, the layout cannot be narrower than 156, nor can it be taller or
205 shorter than 150, without breaking any of the constraints of its child items.
206
207 \section1 Connecting windows and layouts
208 You can use normal anchoring concepts to ensure that your layout follows
209 the window resizing.
210
211 \snippet qml/windowconstraints.qml anchoring
212
213 You can rely on the size constraints of layouts to ensure that the window
214 cannot be resized beyond the layout constraints. You can take the size
215 constraints from the layout and set these constraints on the \e minimumWidth,
216 \e minimumHeight, \e maximumWidth, and \e maximumHeight of the
217 \l [Qml]{Window} element. The following code ensures that the window cannot
218 be resized beyond the constraints of the layout:
219
220 \snippet qml/windowconstraints.qml bindconstraints
221
222 \note Because \e {layout.Layout.maximumWidth} is infinite in this case, we
223 cannot bind that to the \e maximumWidth property of Window, as that is an
224 integer number. Therefore, the maximum width is set to a fixed value of
225 \c 1000.
226
227 Finally, set the initial size of the window to be the layout's implicit
228 size:
229
230 \snippet qml/windowconstraints.qml binddefaultsize
231
232 \section1 Spanning and stretching Items
233
234 Use \l {Layout::columnSpan}{spans} in a GridLayout to make child items
235 occupy more than one cell. For example, you may have a GridLayout with six
236 cells across two rows. The top row contains the \e {Item}s item1, item2,
237 and item3. The bottom row contains the \e Item item4, which specifies
238 \e {columnSpan: 3} and \e {alignment: Qt.AlignHCenter}. This places item4
239 in the middle of the three cells that make up the bottom row. The following
240 snippet serves as an example:
241
242 \snippet qml/gridlayout_with_span.qml gridlayout-with-span
243
244 The size of rows and columns are given implicitly by their contents.
245 For example, a Button may impact the width of the column it's in, or the
246 height of the row it's in. This means GridLayout doesn't have uniform
247 distribution. Because of this, you can't use a span to stretch a layout.
248 To manipulate the stretch of an item or layout, use
249 \l {Layout::horizontalStretchFactor}{stretchFactor}s and/or size hints
250 instead.
251
252 \note When setting implicit or preferred sizes, don't bind the respective
253 properties to the width or height of the layout itself or items it depends
254 on for its size calculations, as this can cause cyclic dependencies that
255 are hard to track down.
256*/