【发布时间】:2022-01-23 14:49:36
【问题描述】:
我正在 QML 中创建一个组件,工作正常,但我希望 TEXT 字段位于矩形的中心。为此,我已相应地锚定它,但锚不起作用。我不知道为什么。
Item {
id: root
property int main_rect_height: 32
property int elem_txt_size: 14
property string elem_border_color: "red"
property int elem_width: parent.width/6.5
Rectangle {
id: clients_rect
width: root.parent.width-27
height: main_rect_height
color: "transparent"
border.color: "black"
Rectangle {
id: username_rect
width: elem_width
height: parent.height
color: "transparent"
border.color: elem_border_color
Text {
id: username_txt
text: "USERNAME"
font.pixelSize: elem_txt_size
anchors {
fill: parent
centerIn: parent // THIS DOESN'T WORK!
}
}
}
}
【问题讨论】:
-
删除“填充:父”
-
谢谢,它成功了。但是为什么当我填写父母时它不起作用?