QML:在布局中设置按钮大小的问题

T.P*_*Poe 3 qt button qml

我在 qml 中有这个应用程序页面:

Image {
    source: "file:///C:/Users/image.jpg"
    fillMode: Image.PreserveAspectCrop

    Rectangle {
        width: parent.width/3
        height: parent.height/3
        anchors.centerIn: parent
        radius: 5

        ColumnLayout {
            width: parent.width * 0.5
            height: parent.height * 0.5
            anchors.centerIn: parent

            //some components here ...

            Button {
                //width: 100   nothing changes in the app
                Layout.alignment: Qt.AlignHCenter | Qt.AlignBottom

                style: ButtonStyle {
                    background: Rectangle {
                        color: "blue"
                        radius: 15
                        //width: 100   nothing changes in the app
                    }
                    label: Text {
                        text: qsTr("Button")
                        color: "white"
                    }
                }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我正在尝试设置按钮的大小(宽度和高度),这取决于父级(布局)的大小,例如width: parent * 0.4. 我可能尝试了代码中宽度语句的所有可能位置,但是当我运行应用程序时,按钮的大小从未改变。我还尝试将大小设置为特定数字,而不是将其绑定到父级,仍然没有。

那么这里可能有什么问题呢?应该在哪里以及如何定义按钮大小,以便将其绑定到其父布局大小?

Chr*_*isM 6

您是否尝试过@folibis 的建议?我猜这与这里的问题类似。在那个例子中,布局内的元素没有响应width和height参数。设置Layout.preferredWidth并Layout.preferredHeight似乎解决了问题。请参阅该答案中提供的文档。

例如,您仍然可以绑定到父级的属性

Layout.preferredWidth: parent.width * 0.4
Run Code Online (Sandbox Code Playgroud)