你能完成你想要什么用 ListView
你只需要调整间隔动态基础上有多少名代表。 这个例子将打破,如果你的各位代表不同大小的(因为这是基于只有在宽度的第一委托)、或者如果代表们累计超过宽度 ListView
.
ListView {
width: 500
orientation: Qt.Horizontal
model: 6
spacing: {
if (count > 0) {
return (width - (itemAtIndex(0).width * count))/(count - 1)
} else {
return 0
}
}
delegate: Rectangle {
implicitHeight: 50
implicitWidth: 50
color: "red"
border.width: 1
}
}
ListView
可能不是最适当的容器,用于这项任务。 我这样说,是因为它具有一个建立在 ScrollView
和其他行为,这听起来像你不需要。 如果所有你需要的是一个简单的行一些相同大小的代表,我同意scopchanov,并相信一个 Repeater
内部 RowLayout
会是最好的选择。 这是一个简单的例子:
RowLayout {
width: 500
Repeater {
model: 6
delegate: Rectangle {
implicitHeight: 50
implicitWidth: 50
color: "tomato"
border.width: 1
Layout.alignment: Qt.AlignHCenter // must be set to align the rectangles within their empty space
}
}
}
你可能会注意到,这将引入空白的左右,如果这些差距是不可接受的,可能需要设置 spacing
在 RowLayout
在相同的方式 ListView
例,而不是。