Qml 均匀调整行中所有子行的大小,以适合其父行

Qml 均匀调整行中所有子行的大小,以适合其父行,qml,sailfish-os,Qml,Sailfish Os,我试图在我的窗口中放置5个按钮,对齐成一行,每个按钮的宽度应该相等,同时占用父窗口中的所有可用空间。以下是我如何尝试(失败): 在本例中,按钮项的大小不会调整为适合其父项。我希望看到每个按钮的宽度为其父按钮宽度的1/5。显然,使用行是不够的 此外,使用: width: parent.width / parent.children.count 在每个按钮中,都会失败,因为parent.children.count未定义。当一切准备就绪时,可以使用Component.onCompleted设置宽度

我试图在我的窗口中放置5个按钮,对齐成一行,每个按钮的宽度应该相等,同时占用父窗口中的所有可用空间。以下是我如何尝试(失败):

在本例中,
按钮
项的大小不会调整为适合其父项。我希望看到每个
按钮
的宽度为其父按钮宽度的1/5。显然,使用
是不够的

此外,使用:

width: parent.width / parent.children.count
在每个按钮中,都会失败,因为
parent.children.count
未定义。当一切准备就绪时,可以使用
Component.onCompleted
设置宽度,但必须有更好的方法


是否有一个
选项可在其子项上强制设置宽度?或者还有什么其他的布局选项?

我个人这样做时,我使用了一个转发器和一个列表模型或一个文本数组,这样你就可以很容易地知道你将拥有多少个
按钮,并正确地调整它们的大小

ListModel {
    id: modelButtons
    ListElement { buttonText: "left" }
    ListElement { buttonText: "right" }
    ListElement { buttonText: "tab" }
    ListElement { buttonText: "home" }
    ListElement { buttonText: "end" }
}

Row {
    id: toolbar
    width: 300
    height: 80

    Repeater {
        id: repeaterButton
        model: modelButtons
        delegate: Button {
            width: toolbar.width / repeaterButton.model.count
            text: buttonText
        }
    }
}
ListModel {
    id: modelButtons
    ListElement { buttonText: "left" }
    ListElement { buttonText: "right" }
    ListElement { buttonText: "tab" }
    ListElement { buttonText: "home" }
    ListElement { buttonText: "end" }
}

Row {
    id: toolbar
    width: 300
    height: 80

    Repeater {
        id: repeaterButton
        model: modelButtons
        delegate: Button {
            width: toolbar.width / repeaterButton.model.count
            text: buttonText
        }
    }
}