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
layouts.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\page qtquick-usecase-layouts.html
5\meta {keywords} {qmltopic}
6\title Positioners and layouts
7\keyword Use Case - Positioners and Layouts In QML
8\brief Example of how to create layouts for visual components in a QML application
9\ingroup explanations-programminglanguages
10
11There are several ways to position items in QML.
12
13Below is a brief overview. For more details, see \l {Important Concepts In Qt Quick - Positioning}.
14
15\section1 Manual Positioning
16
17Items can be placed at specific x,y coordinates on the screen by setting their x,y properties. This will
18setup their position relative to the top left corner of their parent, according to the
19\l {Concepts - Visual Coordinates in Qt Quick}{visual coordinate system} rules.
20
21Combined with using \l{Property Binding}{bindings} instead of constant values for these properties, relative positioning is also easily
22accomplished by setting the x and y coordinates to the appropriate bindings.
23
24\snippet qmlapp/usecases/layouts.qml import
25\snippet qmlapp/usecases/layouts.qml direct
26
27\image qmlapp/qml-uses-layouts-direct.png
28
29
30\section1 Anchors
31
32The \c Item type provides the abilitiy to anchor to other \l Item types. There
33are seven anchor lines for each item: \e left, \e right, \e{vertical center},
34\e top, \e bottom, \e baseline and \e{horizontal center}. The three vertical
35anchor lines can be anchored to any of the three vertical anchor lines of
36another item, and the four horizontal anchor lines can be anchored to the
37horizontal anchor lines of another item.
38
39For full details, see \l {Positioning with Anchors} and the documentation of the \l{Item::anchors.top}{anchors property}.
40
41\snippet qmlapp/usecases/layouts.qml import
42\snippet qmlapp/usecases/layouts.qml anchors
43
44\image qmlapp/qml-uses-layouts-anchors.png
45
46
47\section1 Positioners
48
49For the common case of wanting to \e position a set of types in a regular pattern, Qt Quick provides some positioner
50types. Items placed in a positioner are automatically positioned in some way; for example, a \l [QML] Row positions items to be
51horizontally adjacent (forming a row).
52
53For full details see \l {Item Positioners}.
54
55\snippet qmlapp/usecases/layouts.qml import
56\snippet qmlapp/usecases/layouts.qml positioners
57
58\image qmlapp/qml-uses-layouts-positioners.png
59
60\section1 Layout Types
61
62\e{Layout types} function in a similar way as positioners but allow further refinement or
63restrictions to the layout. Specifically, the layout types allow you to:
64
65\list
66\li set the alignment of text and other items
67\li resize and fill the allotted application areas automatically
68\li set size constraints such as minimum or maximum dimensions
69\li set the spacing between items within the layout
70\endlist
71
72\qml
73 GroupBox {
74 id: gridBox
75 title: "Grid layout"
76 Layout.fillWidth: true
77
78 GridLayout {
79 id: gridLayout
80 rows: 3
81 flow: GridLayout.TopToBottom
82 anchors.fill: parent
83 Label { text: "Line 1" }
84 Label { text: "Line 2" }
85 Label { text: "Line 3" }
86
87 TextField { }
88 TextField { }
89 TextField { }
90
91 TextArea {
92 text: "This widget spans over three rows in the GridLayout.\n"
93 + "All items in the GridLayout are implicitly positioned from top to bottom."
94 Layout.rowSpan: 3
95 Layout.fillHeight: true
96 Layout.fillWidth: true
97 }
98 }
99 }
100\endqml
101The snippet above comes from the \l{Qt Quick Layouts - Basic Example}{Basic Layouts} example. The
102snippet shows the simplicity of adding various fields and items in a layout. The \l [QML] {GridLayout} can
103be resized and its format is customizable through various properties.
104
105For more information about the layout types, visit:
106\list
107\li \l{Qt Quick Layouts Overview}
108\li \l{Qt Quick Layouts - Basic Example}{Basic Layouts} example
109\endlist
110
111\note \l{Qt Quick Layouts} was introduced in Qt 5.1 and requires \l{Qt Quick} 2.1.
112
113*/