如何在 ColumnLayout 中居中元素

Mic*_*ski 7 qt qtquick2

如何将元素居中ColumnLayout

这是我的qml代码:

ApplicationWindow {
    id: root
    visible: true
    width: 640
    height: 640
    title: qsTr("Your Booking")
    GridLayout{
        anchors.fill: parent
        columns: 2
        flow: GridLayout.TopToBottom
        Rectangle{
            id: appBar
            Layout.columnSpan: 2
            width: root.width
            height: root.height/10
            color: "red"
        }
        ColumnLayout{
            spacing:5
            id: columnData
            height: root.height - appBar.height
            width: root.width/2

            ComboBox{
                 anchors.horizontalCenter: parent.horizontalCenter
            }
            ComboBox{

            }
            ComboBox{
            }
            ComboBox{
            }
            ComboBox{
            }
        }
        ColumnLayout{

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

我想在 ColumnLayout 中居中组合框。

在此处输入图片说明

小智 8

您应该避免同时使用锚点和布局。将它们混合在同一级别会导致布局故障或一些意外结果(但是,在布局中的项目内使用锚点是可以的)。

要对齐布局中的项目,您可以使用附加属性:Layout.alignment,例如:

 Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter
Run Code Online (Sandbox Code Playgroud)

此语句将使您的项目完全位于布局的中心。

  • [`Qt.AlignCenter`](https://doc.qt.io/qt-5/qt.html#AlignmentFlag-enum) 是`Qt.AlignHCenter | Qt.AlignVCenter` (2认同)