我在QML中设计了一个布局,以了解有关其功能的更多信息,并对设计此类布局的“最佳实践”提出一些问题。这是:
它基本上是一个 ColumnLayout ,由三个 RowLayout 组成,每个 RowLayout ,每个都有一些 Rectangle 。应计算每个Row和Rectangle的大小,例如:
我提出的QML正在发挥作用,现在如下。我对此有一些疑问:
这是我的布局QML代码:
ApplicationWindow {
x: 500
y: 100
width: 250
height: 150
visible: true
ColumnLayout {
anchors.fill: parent
spacing: 0
RowLayout {
spacing: 0
Layout.preferredHeight: 0.4*parent.height
Layout.fillHeight: false
Rectangle {
Layout.fillHeight: true
Layout.fillWidth: true
color: "red"
}
}
RowLayout {
spacing: 0
Layout.preferredHeight: 0.2*parent.height
Layout.fillHeight: false
Rectangle {
Layout.fillHeight: true
Layout.fillWidth: true
color: "darkGreen"
}
Rectangle {
Layout.fillHeight: true
Layout.preferredWidth: 0.8*parent.width
color: "lightGreen"
}
}
RowLayout {
spacing: 0
Layout.preferredHeight: 0.4*parent.height
Layout.fillHeight: false
Rectangle {
Layout.fillHeight: true
Layout.fillWidth: true
color: "darkBlue"
}
Rectangle {
Layout.fillHeight: true
Layout.preferredWidth: 0.2*parent.width
color: "blue"
}
Rectangle {
Layout.fillHeight: true
Layout.preferredWidth: 0.4*parent.width
color: "lightBlue"
}
}
}
}
更新
我的做法似乎比我预期的要严厉:
QML QQuickLayoutAttached:检测到属性“preferredWidth”的绑定循环
如果矩形内的换行文本警告消失。
虽然我在QML中采用流体布局设计的方法有效,但它存在一些严重问题,可能不属于“最佳实践”。
答案 0 :(得分:5)
禁止(并且不必要)尝试从布局内的项目中引用父级的宽度和高度。
当fillWidth
(或fillHeight
)设置为true
时,则会根据指定的preferredWidth
(或preferredHeight
)按比例分配项目空间。
因此,创建布局的正确方法如下。我修改了外观只是为了表明间距和Text
也可以根据需要自由设置。没有绑定循环。
ApplicationWindow {
x: 500
y: 100
width: 250
height: 150
visible: true
ColumnLayout {
anchors.fill: parent
spacing: 5
RowLayout {
spacing: 5
Layout.preferredHeight: 40
Layout.fillHeight: true
Rectangle {
Layout.fillHeight: true
Layout.fillWidth: true
color: "red"
}
}
RowLayout {
spacing: 5
Layout.preferredHeight: 20
Layout.fillHeight: true
Rectangle {
Layout.fillHeight: true
Layout.preferredWidth: 20
Layout.fillWidth: true
color: "darkGreen"
}
Rectangle {
Layout.fillHeight: true
Layout.preferredWidth: 80
Layout.fillWidth: true
color: "lightGreen"
}
}
RowLayout {
spacing: 5
Layout.preferredHeight: 40
Layout.fillHeight: true
Text {
Layout.fillHeight: true
Layout.preferredWidth: 40
Layout.fillWidth: true
color: "darkBlue"
text: "hello world!"
horizontalAlignment: Text.AlignHCenter
verticalAlignment: Text.AlignVCenter
}
Rectangle {
Layout.fillHeight: true
Layout.preferredWidth: 20
Layout.fillWidth: true
color: "blue"
}
Rectangle {
Layout.fillHeight: true
Layout.preferredWidth: 40
Layout.fillWidth: true
color: "lightBlue"
}
}
}
}
答案 1 :(得分:4)
QtQuick.Layout没有对经典锚定系统提供任何真正的改进。我建议避免它们。您可以使用锚点更好地控制布局。
这是完全相同的设计,没有QtQuick.Layout:
ApplicationWindow {
x: 500
y: 100
width: 250
height: 150
visible: true
Column {
anchors.fill: parent
Row {
anchors.left: parent.left
anchors.right: parent.right
height: 0.4 * parent.height
Rectangle {
anchors.top: parent.top
anchors.bottom: parent.bottom
width: parent.width
color: "red"
}
}
Row {
anchors.left: parent.left
anchors.right: parent.right
height: 0.2 * parent.height
Rectangle {
anchors.top: parent.top
anchors.bottom: parent.bottom
width: 0.2 * parent.width
color: "darkGreen"
}
Rectangle {
anchors.top: parent.top
anchors.bottom: parent.bottom
width: 0.8 * parent.width
color: "lightGreen"
}
}
Row {
anchors.left: parent.left
anchors.right: parent.right
height: 0.4 * parent.height
Rectangle {
anchors.top: parent.top
anchors.bottom: parent.bottom
width: 0.4 * parent.width
color: "darkBlue"
}
Rectangle {
anchors.top: parent.top
anchors.bottom: parent.bottom
width: 0.2 * parent.width
color: "blue"
}
Rectangle {
anchors.top: parent.top
anchors.bottom: parent.bottom
width: 0.4 * parent.width
color: "lightBlue"
}
}
}
}
到目前为止,我从未遇到任何没有QtQuick.Layout无法做到的设计。
答案 2 :(得分:2)
虽然其他两个答案都显示了有效的解决方案,但我相信所要提出的问题和两个解决方案都以某种方式错过了使用布局的意义。
基本上,布局是将具有隐式大小(implicitHeight / implicitWidth)的项目组合在一起的。 Layout.preferredWidth / Layout.preferredHeight在某些罕见情况下用于覆盖这些内容,请参见下文。 Qt附带的“ Qt快速布局-基本示例”根本不使用Layout.preferredWidth / Layout.preferredHeight(!),并且外观非常好,而不会用锚点或Layout属性污染整个qml文件。要做到这一点需要一些学习,但是一旦习惯了,布局是一种使用更少的代码更直接地定义用户界面的方法。
最让我困惑的是以下几件事:
考虑到这些问题,我将以以下方式编写示例。我删除了不必要的项目,以更好地说明何时需要Layout.fillwidth / Layout.fillheight,以及我认为什么时候最好使用隐式宽度。
import QtQuick 2.9
import QtQuick.Controls 2.0
import QtQuick.Layouts 1.3
ApplicationWindow {
width: 250
height: 150
ColumnLayout {
spacing: 0
anchors.fill: parent
Rectangle {
implicitHeight: 40
Layout.fillHeight: true
Layout.fillWidth: true
color: "red"
}
RowLayout {
spacing: 0
Layout.preferredHeight: 20
Rectangle {
implicitWidth: 20
Layout.fillHeight: true
Layout.fillWidth: true
color: "darkGreen"
}
Rectangle {
implicitWidth: 80
Layout.fillHeight: true
Layout.fillWidth: true
color: "lightGreen"
}
}
RowLayout {
spacing: 0
Layout.preferredHeight: 40
Rectangle {
implicitWidth: 40
Layout.fillHeight: true
Layout.fillWidth: true
color: "darkBlue"
}
Rectangle {
implicitWidth: 20
Layout.fillHeight: true
Layout.fillWidth: true
color: "blue"
}
Rectangle {
implicitWidth: 40
Layout.fillHeight: true
Layout.fillWidth: true
color: "lightBlue"
}
}
}
}