不是在item之间分配一个固定的spacing
,而是可以沿着(parent的)width
均匀分布item,从而最大化间距?
我目前做的是:
Row {
width: parent.width
readonly property real childrenWidth: {
var w = 0
for(var child of children) w += child.width
return w
}
spacing: (width - childrenWidth) / Math.max(1, children.length - 1)
MyElement {}
MyElement {}
...
}
有没有更好的办法?
回答1
我认为您正在寻找 RowLayout
RowLayout {
width: parent.width
MyElement { Layout.fillWidth: true }
MyElement { Layout.fillWidth: true }
}
如果需要,您可以声明 minimumWidth
、preferredWidth
和/或 maximumWidth
,请参阅 https://doc.qt.io/qt-5/qml-qtquick-layouts-rowlayout.html。如果没有这些,则使用 MyElement
的 implicitWidth
。