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 {Red square positioned using direct x and y coordinates}
29
30
31\section1 Anchors
32
33The \c Item type provides the abilitiy to anchor to other \l Item types. There
34are seven anchor lines for each item: \e left, \e right, \e{vertical center},
35\e top, \e bottom, \e baseline and \e{horizontal center}. The three vertical
36anchor lines can be anchored to any of the three vertical anchor lines of
37another item, and the four horizontal anchor lines can be anchored to the
38horizontal anchor lines of another item.
39
40For full details, see \l {Positioning with Anchors} and the documentation of the \l{Item::anchors.top}{anchors property}.
41
42\snippet qmlapp/usecases/layouts.qml import
43\snippet qmlapp/usecases/layouts.qml anchors
44
45\image qmlapp/qml-uses-layouts-anchors.png
46 {Green and orange rectangles positioned using anchors}
47
48
49\section1 Positioners
50
51For the common case of wanting to \e position a set of types in a regular pattern, Qt Quick provides some positioner
52types. Items placed in a positioner are automatically positioned in some way; for example, a \l [QML] Row positions items to be
53horizontally adjacent (forming a row).
54
55For full details see \l {Item Positioners}.
56
57\snippet qmlapp/usecases/layouts.qml import
58\snippet qmlapp/usecases/layouts.qml positioners
59
60\image qmlapp/qml-uses-layouts-positioners.png
61 {Red, green, and blue squares arranged in a row using positioners}
62
63\section1 Layout Types
64
65\e{Layout types} function in a similar way as positioners but allow further refinement or
66restrictions to the layout. Specifically, the layout types allow you to:
67
68\list
69\li set the alignment of text and other items
70\li resize and fill the allotted application areas automatically
71\li set size constraints such as minimum or maximum dimensions
72\li set the spacing between items within the layout
73\endlist
74
75\qml
76 GroupBox {
77 id: gridBox
78 title: "Grid layout"
79 Layout.fillWidth: true
80
81 GridLayout {
82 id: gridLayout
83 rows: 3
84 flow: GridLayout.TopToBottom
85 anchors.fill: parent
86 Label { text: "Line 1" }
87 Label { text: "Line 2" }
88 Label { text: "Line 3" }
89
90 TextField { }
91 TextField { }
92 TextField { }
93
94 TextArea {
95 text: "This widget spans over three rows in the GridLayout.\n"
96 + "All items in the GridLayout are implicitly positioned from top to bottom."
97 Layout.rowSpan: 3
98 Layout.fillHeight: true
99 Layout.fillWidth: true
100 }
101 }
102 }
103\endqml
104The snippet above comes from the \l{Qt Quick Layouts - Basic Example}{Basic Layouts} example. The
105snippet shows the simplicity of adding various fields and items in a layout. The \l [QML] {GridLayout} can
106be resized and its format is customizable through various properties.
107
108For more information about the layout types, visit:
109\list
110\li \l{Qt Quick Layouts Overview}
111\li \l{Qt Quick Layouts - Basic Example}{Basic Layouts} example
112\endlist
113
114\note \l{Qt Quick Layouts} was introduced in Qt 5.1 and requires \l{Qt Quick} 2.1.
115
116*/