在rowlayout中垂直对齐项目

时间:2017-10-31 16:53:57

标签: qt qml qt5 qt-quick

我正在使用Rowlayout创建一行qml按钮,但无法对齐按钮。我希望在垂直和水平方向上居中对齐。

我尝试了以下内容:

RowLayout
{
    anchors.fill: parent
    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter

    ToolButton {
        //anchors.verticalCenter: parent.verticalCenter
        //anchors.horizontalCenter: parent.horizontalCenter
        Image {
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            source: "../images/search.png"
        }
    }

    ToolButton {
        //anchors.verticalCenter: parent.verticalCenter
        //anchors.horizontalCenter: parent.horizontalCenter
        Image {
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            source: "../images/search.png"
        }
    }
}

这会产生如下图像:

enter image description here

如何让图像从中心对齐?

1 个答案:

答案 0 :(得分:1)

你的意思是这样的:

image

我将Layout.alignment: Qt.AlignRight | Qt.AlignVCenter添加到ToolButtons

RowLayout
{
    anchors.fill: parent
    Layout.alignment: Qt.AlignHCenter | Qt.AlignVCenter

    ToolButton {
        Layout.alignment: Qt.AlignRight | Qt.AlignVCenter
        //anchors.verticalCenter: parent.verticalCenter
        //anchors.horizontalCenter: parent.horizontalCenter
        Image {
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            source: "search.png"
        }
    }

    ToolButton {
        Layout.alignment: Qt.AlignLeft | Qt.AlignVCenter
        //anchors.verticalCenter: parent.verticalCenter
        //anchors.horizontalCenter: parent.horizontalCenter
        Image {
            anchors.verticalCenter: parent.verticalCenter
            anchors.horizontalCenter: parent.horizontalCenter
            source: "search.png"
        }
    }
}
相关问题