qml使用行在中心对齐组件

use*_*140 5 qt qml qt-quick qtquickcontrols

Row习惯于在Rectangle自定义工具栏实现上布局一些按钮。问题是无论我做什么,组件始终从左对齐。我希望它们与行的中心对齐并向外流向边缘。该代码如下所示:

Rectangle {
        id: toolbar
        color: "green"
        anchors.left: parent.left
        anchors.right: parent.right
        anchors.bottom: parent.bottom
        height: 100

        Row
        {
            anchors.fill: parent                
            anchors.horizontalCenter: parent.horizontalCenter
            spacing: 60

            ToolButton {
                height: parent.height
                Image {
                    anchors.verticalCenter: parent.verticalCenter
                    source: "../images/image.png"
                }
            }

            ToolButton {
                height: parent.height
                Image {
                    anchors.verticalCenter: parent.verticalCenter
                    source: "../images/image.png"
                }
            }
        }
 }
Run Code Online (Sandbox Code Playgroud)

我的按钮总是从行的左侧开始布置。相反,我希望它们相对于工具栏的中心进行布局。我认为指定该行anchors.horizontalCenter: parent.horizontalCenter应该可以实现,但是无论我如何尝试,组件都应从左侧边界布局。

小智 0

你已经设置anchors.fill: parent了,Row所以它当然会填充它的父级。相反,您应该删除它,并仅height: parent.height在行上设置。