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

时间:2018-03-27 12:37:26

标签: 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"
                    }
                }
            }
        }
    }
}

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

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

1 个答案:

答案 0 :(得分:1)

您是否尝试过@folibis的建议?我猜这是问题here的类似问题。在该示例中,布局内的元素未响应widthheight参数。设置Layout.preferredWidthLayout.preferredHeight似乎可以解决问题。请参阅该答案中提供的文档。

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

Layout.preferredWidth: parent.width * 0.4