【发布时间】:2017-11-01 12:52:17
【问题描述】:
我正在使用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"
}
}
}
}
我的按钮总是从行的左侧开始布局。相反,我想让它们相对于工具栏的中心布置。我认为指定这条线 anchors.horizontalCenter: parent.horizontalCenter 应该可以实现这一点,但无论我尝试什么,组件都是从左边界开始布局的。
【问题讨论】:
-
检查
RowLayout。它看起来像Row,但有更多的布局选项。 -
@BenjaminT 也试过了,但无法让它们按照我的意愿对齐组件。
-
好的,您正试图将您的 Row 放置在父级 (
anchors.fill: parent) 中,同时在父级内部居中 (anchors.horizontalCenter: parent.horizontalCenter)。那只是不能一起工作。如果您想将 Row 项目放在其父项的中心,只需删除anchors.fill: parent。您还应该设置行的高度。我想应该是height: parent.height。另外我想你应该设置你的宽度ToolButtons -
@folibis 你想把它写成答案以便我接受吗?它工作得很好。抱歉,由于我的低街信誉,无法支持您的评论。
标签: qt qml qt-quick qtquickcontrols