Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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} property.
20
\li Specify \l{Layout.fillWidth}{resizable items} with the
21
\l{Layout.fillWidth} and
22
\l{Layout.fillHeight} properties.
23
\li Set \l{Size constraints}{size constraints} with the
24
\l{Layout.minimumWidth},
25
\l{Layout.preferredWidth}, and
26
\l{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} with
30
\l{RowLayout::}{spacing},
31
\l{GridLayout::}{rowSpacing},
32
or \l{GridLayout::}{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} and \l{Layout.column}
41
properties.
42
\li \l{GridLayout.flow}{Automatic grid coordinates} used together with
43
the \l{GridLayout::}{flow}, \l{GridLayout::}{rows}, and
44
\l{GridLayout::}{columns} properties.
45
\li \l{Layout.columnSpan}{Spans} across rows or columns, that you can
46
specify with the \l{Layout.rowSpan} and
47
\l{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} and
69
\l{Item::}{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},
78
\l{Item::}{height}, \l{Item::}{x}, \l{Item::}{y},
79
\l{Item::}{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} or \l{Layout.preferredHeight}
129
\li These properties are supposed to be modified by the application if
130
the default implicit size does not give the optimal arrangement.
131
\row
132
\li \l{Item::}{implicitWidth} or \l{Item::}{implicitHeight}.
133
\li These properties are supposed to be supplied by each item to give a
134
meaningful ideal size. For example, the size needed to display all
135
the contents of a \l Text type. An implicit width or height of \c 0
136
is interpreted as invalid.
137
\endtable
138
139
An item can specify \l{Layout.preferredWidth} without having to specify
140
\l{Layout.preferredHeight}. In such cases, the effective preferred height
141
is determined from the \l{Item::}{implicitHeight}.
142
143
\note If you don't specify neither preferredWidth nor implicitWidth, the
144
Layout will query \l {Item::}{width} as an ultimate value for the effective
145
preferred width. However, you shouldn't rely on \l {Item::}{width} as a
146
source for the effective preferred width, as that may cause unexpected
147
behavior. For instance, changing the \l{Item::}{width} or
148
\l{Item::}{height} properties won't trigger a layout rearrangement,
149
or the layout might use the actual width and height -- not the width and
150
height specified in your QML file -- when forced to do a full rebuild.
151
152
\section2 Size constraints
153
154
Since an item can be resized by its layout, the layout needs to know the
155
\l{Layout.minimumWidth}{minimum}, \l{Layout::preferredWidth}{preferred},
156
and \l{Layout.maximumWidth}{maximum} sizes of all items where
157
\l{Layout.fillWidth} or \l{Layout.fillHeight} is set to \e true.
158
159
The \l{Layout.preferredWidth}{preferred} width and height is the \e actual
160
width and height of an item, if the layout is not bound to a specific size
161
itself. If the layout is set to a specific size, it distributes additional
162
space based on the ratio of preferred sizes of its items, while taking
163
minimum and maximum sizes into account. The preferred and implicit sizes
164
act as ratios and weights when all items set \e fillWidth and
165
\e fillHeight.
166
167
For instance, the following produces a layout with two rectangles lying
168
side-by-side that stretches horizontally. The \e {orange Rectangle} can be
169
resized from 50x150 to 300x150, and the \e {plum Rectangle} can be resized
170
from 100x100 to ∞x100. As long as the minimum and maximum width of each
171
item isn't exceeded, the plum rectangle will have twice the width of the
172
orange one.
173
174
\snippet qml/windowconstraints.qml rowlayout
175
176
\image rowlayout-minimum.png "RowLayout at its minimum"
177
178
Combining each item's constraints gives these implicit constraints to the
179
layout element:
180
181
\table
182
\header
183
\li
184
\li minimum
185
\li preferred
186
\li maximum
187
\row
188
\li implicit constraints (width)
189
\li 156
190
\li 306
191
\li ∞ (\c Number.POSITIVE_INFINITY)
192
\row
193
\li implicit constraints (heights)
194
\li 150
195
\li 150
196
\li 150
197
\endtable
198
199
Thus, the layout cannot be narrower than 156, nor can it be taller or
200
shorter than 150, without breaking any of the constraints of its child items.
201
202
\section1 Connecting windows and layouts
203
You can use normal anchoring concepts to ensure that your layout follows
204
the window resizing.
205
206
\snippet qml/windowconstraints.qml anchoring
207
208
You can rely on the size constraints of layouts to ensure that the window
209
cannot be resized beyond the layout constraints. You can take the size
210
constraints from the layout and set these constraints on the \e minimumWidth,
211
\e minimumHeight, \e maximumWidth, and \e maximumHeight of the
212
\l [Qml]{Window} element. The following code ensures that the window cannot
213
be resized beyond the constraints of the layout:
214
215
\snippet qml/windowconstraints.qml bindconstraints
216
217
\note Because \e {layout.Layout.maximumWidth} is infinite in this case, we
218
cannot bind that to the \e maximumWidth property of Window, as that is an
219
integer number. Therefore, the maximum width is set to a fixed value of
220
\c 1000.
221
222
Finally, set the initial size of the window to be the layout's implicit
223
size:
224
225
\snippet qml/windowconstraints.qml binddefaultsize
226
227
\section1 Spanning and stretching Items
228
229
Use \l {Layout.columnSpan}{spans} in a GridLayout to make child items
230
occupy more than one cell. For example, you may have a GridLayout with six
231
cells across two rows. The top row contains the \e {Item}s item1, item2,
232
and item3. The bottom row contains the \e Item item4, which specifies
233
\e {columnSpan: 3} and \e {alignment: Qt.AlignHCenter}. This places item4
234
in the middle of the three cells that make up the bottom row. The following
235
snippet serves as an example:
236
237
\snippet qml/gridlayout_with_span.qml gridlayout-with-span
238
239
The size of rows and columns are given implicitly by their contents.
240
For example, a Button may impact the width of the column it's in, or the
241
height of the row it's in. This means GridLayout doesn't have uniform
242
distribution. Because of this, you can't use a span to stretch a layout.
243
To manipulate the stretch of an item or layout, use
244
\l {Layout.horizontalStretchFactor}{stretchFactor}s and/or size hints
245
instead.
246
247
\note When setting implicit or preferred sizes, don't bind the respective
248
properties to the width or height of the layout itself or items it depends
249
on for its size calculations, as this can cause cyclic dependencies that
250
are hard to track down.
251
*/
qtdeclarative
src
quick
doc
src
concepts
layouts
qtquicklayouts-overview.qdoc
Generated on
for Qt by
1.16.1