Qt 拉伸元素以包含所有子元素
在中,如何能够自动拉伸元素,使其所有子元素都适合它?如何指定间距?例如,我希望文本周围有一个矩形。矩形应该有一些内部间距 如果我写以下内容,那么矩形的大小为0,0Qt 拉伸元素以包含所有子元素,qt,qml,Qt,Qml,在中,如何能够自动拉伸元素,使其所有子元素都适合它?如何指定间距?例如,我希望文本周围有一个矩形。矩形应该有一些内部间距 如果我写以下内容,那么矩形的大小为0,0 Rectangle { color: "gray" anchors.centerIn: parent; Text { text: "Hello" } } 如果我尝试使用列元素来修复它,如中所建议的,那么我会在整个窗口/父窗口中获得一列 Column { anchors.cen
Rectangle {
color: "gray"
anchors.centerIn: parent;
Text {
text: "Hello"
}
}
如果我尝试使用列
元素来修复它,如中所建议的,那么我会在整个窗口/父窗口中获得一列
Column {
anchors.centerIn: parent
Rectangle {
color: "gray"
anchors.fill: parent
}
Text {
anchors.centerIn: parent
text: "Hello"
}
}
编辑:
我还尝试使用
流
元素而不是列
,但随后我在整个窗口/父窗口中得到了一行。手动设置宽度
和高度
,但有点难看:
Rectangle {
color: "gray"
width: label.width+20
height: label.height+20
anchors.centerIn: parent
Text {
id: label
anchors.centerIn: parent
text: "Hello"
}
}
您可以将此属性用于:
import QtQuick 2.0
Rectangle {
width: 320
height: 200
Rectangle {
color: "BurlyWood"
anchors.centerIn: parent
width: childrenRect.width + 20
height: childrenRect.height + 20
Text {
id: hello
x: 10
y: 10
text: "Hello"
}
Text {
anchors.left: hello.right
anchors.leftMargin: 10
anchors.top: hello.top
text: "World"
}
}
}
但是,请注意,在其中一个直接子级中使用
childrenRect
并结合使用anchors.centerIn:parent
会产生绑定循环的警告。main.qml:6:ReferenceError:childrenRect未定义
。有什么问题?Qt 5.3,Qt快速2。3@ManuelSchneid3r嗯,我不能重现你的问题。我只是在Qt5.5上试过,在将导入更改为QtQuick 2.3后,使用qmlscene
运行上述代码。工作正常。问题是我在窗口中尝试了这个。这里没有定义childrenRect。啊,事实上,因为窗口
不是项
。所以我想您需要使用一个显式的根项,它是窗口的子项。