1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \page qml-anchor-layout.html
|
---|
30 | \target anchor-layout
|
---|
31 | \title Anchor-based Layout in QML
|
---|
32 |
|
---|
33 | \section1 Overview
|
---|
34 |
|
---|
35 | In addition to the more traditional \l Grid, \l Row, and \l Column,
|
---|
36 | QML also provides a way to layout items using the concept of \e anchors.
|
---|
37 | Each item can be thought of as having a set of 7 invisible "anchor lines":
|
---|
38 | \l {Item::anchors.left}{left}, \l {Item::anchors.horizontalCenter}{horizontalCenter},
|
---|
39 | \l {Item::anchors.right}{right}, \l {Item::anchors.top}{top},
|
---|
40 | \l {Item::anchors.verticalCenter}{verticalCenter}, \l {Item::anchors.baseline}{baseline},
|
---|
41 | and \l {Item::anchors.bottom}{bottom}.
|
---|
42 |
|
---|
43 | \image edges_qml.png
|
---|
44 |
|
---|
45 | The baseline (not pictured above) corresponds to the imaginary line on which
|
---|
46 | text would sit. For items with no text it is the same as \e top.
|
---|
47 |
|
---|
48 | The QML anchoring system allows you to define relationships between the anchor lines of different items. For example, you can write:
|
---|
49 |
|
---|
50 | \code
|
---|
51 | Rectangle { id: rect1; ... }
|
---|
52 | Rectangle { id: rect2; anchors.left: rect1.right; ... }
|
---|
53 | \endcode
|
---|
54 |
|
---|
55 | In this case, the left edge of \e rect2 is bound to the right edge of \e rect1, producing the following:
|
---|
56 |
|
---|
57 | \image edge1.png
|
---|
58 |
|
---|
59 |
|
---|
60 | You can specify multiple anchors. For example:
|
---|
61 |
|
---|
62 | \code
|
---|
63 | Rectangle { id: rect1; ... }
|
---|
64 | Rectangle { id: rect2; anchors.left: rect1.right; anchors.top: rect1.bottom; ... }
|
---|
65 | \endcode
|
---|
66 |
|
---|
67 | \image edge3.png
|
---|
68 |
|
---|
69 | By specifying multiple horizontal or vertical anchors you can control the size of an item. Below,
|
---|
70 | \e rect2 is anchored to the right of \e rect1 and the left of \e rect3. If either of the blue
|
---|
71 | rectangles are moved, \e rect2 will stretch and shrink as necessary:
|
---|
72 |
|
---|
73 | \code
|
---|
74 | Rectangle { id: rect1; x: 0; ... }
|
---|
75 | Rectangle { id: rect2; anchors.left: rect1.right; anchors.right: rect3.left; ... }
|
---|
76 | Rectangle { id: rect3; x: 150; ... }
|
---|
77 | \endcode
|
---|
78 |
|
---|
79 | \image edge4.png
|
---|
80 |
|
---|
81 |
|
---|
82 | \section1 Anchor Margins and Offsets
|
---|
83 |
|
---|
84 | The anchoring system also allows \e margins and \e offsets to be specified for an item's anchors.
|
---|
85 | Margins specify the amount of empty space to leave to the outside of an item's anchor, while
|
---|
86 | offsets allow positioning to be manipulated using the center anchor lines. An item can
|
---|
87 | specify its anchor margins individually through \l {Item::anchors.leftMargin}{leftMargin},
|
---|
88 | \l {Item::anchors.rightMargin}{rightMargin}, \l {Item::anchors.topMargin}{topMargin} and
|
---|
89 | \l {Item::anchors.bottomMargin}{bottomMargin}, or use \l {Item::}{anchors.margins} to
|
---|
90 | specify the same margin value for all four edges. Anchor offsets are specified using
|
---|
91 | \l {Item::anchors.horizontalCenterOffset}{horizontalCenterOffset},
|
---|
92 | \l {Item::anchors.verticalCenterOffset}{verticalCenterOffset} and
|
---|
93 | \l {Item::anchors.baselineOffset}{baselineOffset}.
|
---|
94 |
|
---|
95 | \image margins_qml.png
|
---|
96 |
|
---|
97 | The following example specifies a left margin:
|
---|
98 |
|
---|
99 | \code
|
---|
100 | Rectangle { id: rect1; ... }
|
---|
101 | Rectangle { id: rect2; anchors.left: rect1.right; anchors.leftMargin: 5; ... }
|
---|
102 | \endcode
|
---|
103 |
|
---|
104 | In this case, a margin of 5 pixels is reserved to the left of \e rect2, producing the following:
|
---|
105 |
|
---|
106 | \image edge2.png
|
---|
107 |
|
---|
108 | \note Anchor margins only apply to anchors; they are \e not a generic means of applying margins to an \l Item.
|
---|
109 | If an anchor margin is specified for an edge but the item is not anchored to any item on that
|
---|
110 | edge, the margin is not applied.
|
---|
111 |
|
---|
112 |
|
---|
113 | \section1 Restrictions
|
---|
114 |
|
---|
115 | For performance reasons, you can only anchor an item to its siblings and direct parent. For example,
|
---|
116 | the following anchor is invalid and would produce a warning:
|
---|
117 |
|
---|
118 | \badcode
|
---|
119 | Item {
|
---|
120 | id: group1
|
---|
121 | Rectangle { id: rect1; ... }
|
---|
122 | }
|
---|
123 | Item {
|
---|
124 | id: group2
|
---|
125 | Rectangle { id: rect2; anchors.left: rect1.right; ... } // invalid anchor!
|
---|
126 | }
|
---|
127 | \endcode
|
---|
128 |
|
---|
129 | Also, anchor-based layouts cannot be mixed with absolute positioning. If an item specifies its
|
---|
130 | \l {Item::}{x} position and also sets \l {Item::}{anchors.left},
|
---|
131 | or anchors its left and right edges but additionally sets a \l {Item::}{width}, the
|
---|
132 | result is undefined, as it would not be clear whether the item should use anchoring or absolute
|
---|
133 | positioning. The same can be said for setting an item's \l {Item::}{y} and \l {Item::}{height}
|
---|
134 | with \l {Item::}{anchors.top} and \l {Item::}{anchors.bottom}, or setting \l {Item::}{anchors.fill}
|
---|
135 | as well as \l {Item::}{width} or \l {Item::}{height}. If you wish to change from using
|
---|
136 | anchor-based to absolute positioning, you can clear an anchor value by setting it to \c undefined.
|
---|
137 |
|
---|
138 | */
|
---|