qt - QML 行“填充”/自动间距?

不是在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 }
}

如果需要,您可以声明 minimumWidthpreferredWidth 和/或 maximumWidth,请参阅 https://doc.qt.io/qt-5/qml-qtquick-layouts-rowlayout.html。如果没有这些,则使用 MyElementimplicitWidth

相似文章

随机推荐

最新文章