我在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
。我可能在代码中尝试了宽度语句的每个可能位置,但是当我运行应用程序时,按钮的大小永远不会改变。我还尝试将大小设置为特定数字,而不是将其绑定到父级,仍然没有。
那么这可能是什么问题?应该在哪里以及如何定义按钮大小,以便将其绑定到其父布局大小?
答案 0 :(得分:1)
您是否尝试过@folibis的建议?我猜这是问题here的类似问题。在该示例中,布局内的元素未响应width
和height
参数。设置Layout.preferredWidth
和Layout.preferredHeight
似乎可以解决问题。请参阅该答案中提供的文档。
您仍然可以绑定到父级的属性,例如
Layout.preferredWidth: parent.width * 0.4