【发布时间】:2021-10-13 14:02:09
【问题描述】:
每个人都知道如何通过这些元素的背景属性为按钮、弹出窗口等设置背景。但我想知道,我怎样才能自己为自己的自定义元素创建这样的属性?我找到了一种方法,但它看起来很丑陋,我似乎找不到定义所述属性的 Button、Popup 等的 qml 代码。所以我玩了一下,想出了这样使用 Bindings 的想法:
Item {
id: root
property Item background: Rectangle {
color: "red"
}
Binding {
target: root.background
property: "parent"
value: root
}
Binding {
target: root.background
property: "anchors.fill"
value: root
when: root.background.parent == root
delayed: true
}
}
如前所述,如果您需要声明孩子的许多属性,这看起来很烦人。那么,Qt 是如何做到的,或者正确的做法是什么?
【问题讨论】:
-
如果您要查找 Button 的
background属性的定义位置,它位于 C++ 代码中。 Button 派生自 AbstractButton,AbstractButton 派生自 Control。您可以找到声明为here 的控件的background属性。 -
我实际上是在寻找 qml 声明,如果这样的事情存在,因为我对 qml 的执行方式感兴趣。不过还是谢谢你!
-
正如我所说,在 QQC2 源代码中,该属性是用 C++ 定义的,而不是 qml。