【发布时间】:2015-10-20 04:28:54
【问题描述】:
我在 QML 中使用 Column 和 Row 类型对齐 Items。有没有办法给每个Item 提供不同的间距。大致如下:
喜欢:
项目1
间距:10
项目2
间距:20
项目3
间距:40
项目4
这是我的代码:
ApplicationWindow {
visible: true
width: 640
height: 480
title: qsTr("Hello World")
Rectangle{
id: rect
anchors.fill: parent
Column{
id: column
anchors.centerIn: parent
spacing: 10
Row{
id: row1
anchors.horizontalCenter: parent.horizontalCenter
Rectangle{
width: 300
height: 100
color: "lightgreen"
}
}
Row{
id: row2
anchors.horizontalCenter: parent.horizontalCenter
Rectangle{
width: 100
height: 50
color: "lightblue"
}
}
Row{
id: row3
anchors.horizontalCenter: parent.horizontalCenter
Rectangle{
width: 50
height: 50
color: "green"
}
}
}
}
}
【问题讨论】:
-
改用ColumntLayout。它具有Layout.topMargin 附加属性来设置边距(在您的情况下为顶部)以及底部、右侧和左侧。
-
@folibis 写下答案。这当然是解决问题的好方法。
-
我同意@BaCaRoZzo 的观点。
-
@folibis 我在很多地方都在关注这个,所以我不想更改为 ColomnLayout。为此我不能使用 topmargin
-
您可以使用“spacer”
Items 来获得更细粒度的间距。不太实用,但很管用。