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
。我还将继续使用抽屉
。谢谢!