如何修复缩进并使代码更简单:
import QtQuick 2.15
import QtQuick.Window 2.15
import QtQuick.Layouts 1.12
Window {
id: root
visible: true
width: 640
height: 480
title: qsTr("Hello World")
RowLayout {
id: _rootLayout
anchors.fill: parent
anchors.margins: 10
spacing: 10
ColumnLayout {
id: _1_2_6
Layout.fillHeight: true
Layout.preferredWidth: (_rootLayout.width - _rootLayout.spacing) * 0.66
RowLayout {
id: _1_2
Layout.fillWidth: true
Rectangle {
id: _1
Layout.fillHeight: true
Layout.preferredWidth: (_1_2_6.width - _1_2_6.spacing) * 0.33
color: "green"
Text {
text: "1"
}
}
Rectangle {
id: _2
Layout.fillHeight: true
Layout.preferredWidth: _1_2_6.width - _1_2_6.spacing - _1.width
color: "lightgreen"
Text {
text: "2"
}
}
}
Rectangle {
id: _6
Layout.fillWidth: true
Layout.preferredHeight: (_1_2_6.height - _1_2_6.spacing) * 0.33
color: "grey"
Text {
text: "6"
}
}
}
ColumnLayout {
id: _3_4_5
Layout.fillHeight: true
Layout.preferredWidth: (_rootLayout.width - _rootLayout.spacing) - _1_2_6.width
Rectangle {
id: _3
Layout.fillWidth: true
Layout.fillHeight: true
color: "lightgray"
Text {
text: "3"
}
}
Rectangle {
id: _4
Layout.fillWidth: true
Layout.fillHeight: true
color: "orange"
Text {
text: "4"
}
}
Rectangle {
id: _5
Layout.fillWidth: true
Layout.fillHeight: true
color: "skyblue"
Text {
text: "5"
}
}
}
}
}
如您所见,左列和右列之间的填充比其他地方多。此外,4-5 之间的差距并不总是与差距 2-6(屏幕 2)在高度上匹配。
我也不喜欢这样,如果你_1_2_6有更多的列,你需要减去更多的间距(像这样:)Layout.preferredWidth: (_rootLayout.width - 2 * _rootLayout.spacing) * 0.66。是否可以像 QWidget layout Stretch 那样设置拉伸因子?
最后,Qt Quick Layouts: Polish loop detected. Aborting after two iterations当我水平缩小时,日志中会显示一条“ ”消息。为什么?


感谢@AlexanderChernin 提供使用 Grid* 的建议。
我还意识到
Layout.preferredWidth您可以将其用作拉伸因子,类似于布局拉伸。结果是: