Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Qt GridLayout-ColumnLayout中的项目居中_Qt_Qml_Qtquick2_Qt Quick - Fatal编程技术网

Qt GridLayout-ColumnLayout中的项目居中

Qt GridLayout-ColumnLayout中的项目居中,qt,qml,qtquick2,qt-quick,Qt,Qml,Qtquick2,Qt Quick,我有一个GridLayout,有两列。一个是矩形,第二个是列布局。这两个项目都使用布局。*进行位置和对齐 红色区域应该是可以打开/关闭的侧栏。右边是内容 我遇到的问题是ColumnLayout中的项目从屏幕的垂直中心开始。如您所见,绿色的矩形不是从顶部开始的 我可以在绿色的矩形上使用anchors.top:parent.top,但我不想混合布局。*和锚定。* 我还尝试了在几个组件上使用Layout.alignment:Qt.AlignTop,但没有骰子。代码如下: import QtQuick

我有一个
GridLayout
,有两列。一个是
矩形
,第二个是
列布局
。这两个项目都使用
布局。*
进行位置和对齐

红色区域应该是可以打开/关闭的侧栏。右边是内容

我遇到的问题是
ColumnLayout
中的项目从屏幕的垂直中心开始。如您所见,绿色的
矩形
不是从顶部开始的

我可以在绿色的
矩形上使用
anchors.top:parent.top
,但我不想混合
布局。*
锚定。*


我还尝试了在几个组件上使用
Layout.alignment:Qt.AlignTop
,但没有骰子。代码如下:

import QtQuick 2.7
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.0

ApplicationWindow {
    width: 400
    height: 400
    visible: true

    GridLayout {
        anchors.fill: parent
        columns: 2
        columnSpacing: 0
        rowSpacing: 0

        Rectangle {
            id: sideBar
            Layout.preferredWidth: 240
            Layout.fillHeight: true
            color: "red"
            state: "opened"

            states: [
                State {
                    name: "opened"
                    PropertyChanges { target: sideBar; Layout.preferredWidth: 240 }
                },
                State {
                    name: "closed"
                    PropertyChanges { target: sideBar; Layout.preferredWidth: 0 }
                }
            ]
        }

        ColumnLayout {
            Layout.preferredWidth: parent.width - sideBar.width
            Layout.preferredHeight: parent.height
            spacing: 0

            Rectangle {
                color: "green"
                Layout.alignment: Qt.AlignHCenter
                Layout.preferredWidth: 200
                Layout.preferredHeight: 40

            }

            MouseArea {
                anchors.fill: parent
                onClicked: {
                    sideBar.state == "opened" ? sideBar.state = "closed" : sideBar.state = "opened";
                }
            }
        }
    }
}

更改
矩形的对齐方式将修复它:

Rectangle {
    color: "green"
    Layout.alignment: Qt.AlignTop
    Layout.preferredWidth: 200
    Layout.preferredHeight: 40

}
我还建议在侧边栏中使用:

import QtQuick 2.7
import QtQuick.Layouts 1.1
import QtQuick.Controls 2.0

ApplicationWindow {
    width: 400
    height: 400
    visible: true

    Drawer {
        id: sideBar
        width: 240
        height: parent.height
        background: Rectangle {
            color: "red"
        }
    }

    Rectangle {
        color: "green"
        anchors.fill: parent

        MouseArea {
            anchors.fill: parent
            onClicked: sideBar.open()
        }
    }
}

通过删除矩形中的“Layout.alignment:Qt.AlignHCenter”进行检查,也请发布可编译的示例。我无法编译您的示例。很抱歉@nayab,我忘了包含
ApplicationWindow
Layout.alignment:Qt.AlignTop | Qt.AlignHCenter
。我还将继续使用
抽屉
。谢谢!