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

标签 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 .我可能尝试了代码中宽度语句的所有可能位置,但是当我运行应用程序时,按钮的大小永远不会改变。我还尝试将大小设置为特定数字,而不是将其绑定(bind)到父级,仍然没有。

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

最佳答案

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

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

Layout.preferredWidth: parent.width * 0.4

关于qt - QML:在布局中设置按钮大小的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49513050/

相关文章:

jquery - 正/负最大值输入

java - 从 Jtable 更新数据库行 : Error

jQuery UI - 我无法设置复选框按钮的样式

qt - 按 QML ListView 中的角色对 QAbstractListModel 派生模型进行排序

qt - 应用镜像变换来翻转/反射 QML 控件

c++ - QTreeWidget::currentItem 没有选择时返回什么?

qt - QComboBox Selected Item 背景不反射(reflect)

QTCreator 使用 INSTALLS 将文件复制到输出目录

c++ - 如何正确关闭我创建的 QProcess?

qt - 错误: Failed to parse qmlimportscanner output