拉伸元素以包含所有子元素
QML 如何自动拉伸元素以使其所有子元素都适合它?以及如何指定间距?例如,我想在文本周围有一个矩形。矩形应该有一些内部间距。
如果我写下以下内容,则矩形的大小为 0,0。
Rectangle {
color: "gray"
anchors.centerIn: parent;
Text {
text: "Hello"
}
}
如果我尝试使用 Column
元素来修复它,如 如何使 QML 项目增长以适应内容?,然后我通过整个窗口/父级获得一列,
Column {
anchors.centerIn: parent
Rectangle {
color: "gray"
anchors.fill: parent
}
Text {
anchors.centerIn: parent
text: "Hello"
}
}
编辑:
我还尝试使用 Flow
元素而不是 Column
,但后来我在整个窗口/父窗口中得到了一行。
How is it possible in QML to automatically stretch element so that all its childs fit in it? And how to specify spacing? For example, I would like to have a rectangle around a text. The rectangle should have some internal spacing.
If I write the following then the rectangle has a size of 0,0.
Rectangle {
color: "gray"
anchors.centerIn: parent;
Text {
text: "Hello"
}
}
If I try to fix it by using the Column
element, as suggested in How to make QML items to grow to fit contents?, then I get a column through the whole window/parent,
Column {
anchors.centerIn: parent
Rectangle {
color: "gray"
anchors.fill: parent
}
Text {
anchors.centerIn: parent
text: "Hello"
}
}
Edit:
I have also tried to use the Flow
element instead of Column
, but then I got a row through the whole window/parent.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
childrenRect
< /a> 属性:但是,请注意,在直接子级之一中结合使用
childrenRect
和使用anchors.centerIn:parent
会产生有关绑定循环的警告。You can use the
childrenRect
property for this:However, note that using
childrenRect
in combination with usinganchors.centerIn: parent
in one of the direct children yields a warning about a binding loop.手动设置
width
和height
可以,但有点难看:Setting the
width
andheight
manually works, but is a little ugly:我认为使用
chilrenRect
属性就足够了(正如 Thorbjørn Lindeijer 的建议)。它不会自动考虑子元素的所有各种边距。如果后者发生变化,根矩形不会自动调整其大小。我个人提出了以下解决方案:I don't think using
chilrenRect
property is sufficient (as suggested by Thorbjørn Lindeijer). It doesn't automatically take into account all various margins of the child(ren) element(s). If the latter changes, the root rectangle doesn't automatically adjust its size. I personally came with the following solution: