Qt Quick Layouts are items that are used to arrange items in a user interface. Since Qt Quick Layouts also resize their items, they are well suited for resizable user interfaces.
The QML types can be imported into your application using the following import statement in your
.qml
文件。
import QtQuick.Layouts 1.2
一些关键特征:
In addition to the above features, GridLayout adds these features:
Since an item can be resized by its layout, the layout needs to know the
minimum
,
preferred
,和
maximum
sizes of all items where
Layout.fillWidth
or
Layout.fillHeight
被设为
true
. For instance, the following will produce a layout with two rectangles lying side-by-side that stretches horizontally. The azure rectangle can be resized from 50x150 to 300x150, and the plum rectangle can be resized from 100x100 to ∞x100.
RowLayout { id: layout anchors.fill: parent spacing: 6 Rectangle { color: 'azure' Layout.fillWidth: true Layout.minimumWidth: 50 Layout.preferredWidth: 100 Layout.maximumWidth: 300 Layout.minimumHeight: 150 Text { anchors.centerIn: parent text: parent.width + 'x' + parent.height } } Rectangle { color: 'plum' Layout.fillWidth: true Layout.minimumWidth: 100 Layout.preferredWidth: 200 Layout.preferredHeight: 100 Text { anchors.centerIn: parent text: parent.width + 'x' + parent.height } } }
Combining each item's constraints will give these implicit constraints to the layout element:
minimum | preferred | maximum | |
---|---|---|---|
implicit constraints (width) | 156 | 306 |
∞ (
Number.POSITIVE_INFINITY
)
|
implicit constraints (heights) | 150 | 150 | 150 |
Thus, the layout cannot be narrower than 156 or be taller or shorter than 150 without breaking any of the constraints of its child items.
For each item, the effective preferred size may come from one of several candidate properties. For determining the effective preferred size, it will query these candidate properties in the following order, and use the first candidate with a valid width or height.
候选特性 | 描述 |
---|---|
Layout.preferredWidth or Layout.preferredHeight | These properties are supposed to be modified by the application if the default implicit size does not give the optimal arrangement. |
implicitWidth or implicitHeight |
These properties are supposed to be supplied by each item to give a meaningful ideal size, for example the size needed to display all the contents of a
Text
type. An implicit width or height of
0
is interpreted as invalid.
|
width and height | If none of the above properties are valid, the layout will resort to the width and height 特性。 |
An item can specify Layout.preferredWidth without having to specify Layout.preferredHeight . In this case, the effective preferred height will be determined from the implicitHeight (or ultimately height ).
注意: Resorting to width or height properties is only provided as a final fallback. If you want to override the preferred size, it is recommended to use Layout.preferredWidth or Layout.preferredHeight . Relying on the width or height properties for specifying the preferred size might give some unexpected behavior. For instance, changing the width or height properties won't trigger a layout rearrangement. Also, when the layout is forced to do a full rebuild it might use the actual width and height, and not the width and height specified in the QML file.
You can just use normal anchoring concepts to ensure that the layout will follow the window resizing.
RowLayout { id: layout anchors.fill: parent
The size constraints of layouts can be used to ensure that the window cannot be resized beyond the layout constraints. You can take the size constraints from the layout and set these constraints on the minimumWidth, minimumHeight, maximumWidth, and maximumHeight of the Window element. The following code ensures that the window cannot be resized beyond the constraints of the layout:
minimumWidth: layout.Layout.minimumWidth minimumHeight: layout.Layout.minimumHeight maximumWidth: 1000 maximumHeight: layout.Layout.maximumHeight
注意: Since layout.Layout.maximumWidth is infinite in this case, we cannot bind that to the maximumWidth property of Window, since that is an integer number. We therefore set a fixed maximum width to 1000.
Finally, you usually want the initial size of the window to be the layout's implicit size:
width: layout.implicitWidth height: layout.implicitHeight