1、属性别名使用注意点
正常使用:
Rectangle{
property alias buttonText: textItem.text
width: 100; height: 30; color: "yellow"
Text{ id: textItem }
}
注意点一:属性别名在整个组件初始化完毕之后才可以使用
id: root
property alias buttonText: textItem.text
//下面的代码会报错,因为代码执行到这里,buttonText还是一个未定义的值
property alias buttonText2: root.buttonText
Component.onCompleted: buttonText = "some text"
注意点二:属性别名可以与现有属性同名,但会覆盖现有属性
Rectangle{
id: coloredrectangle
property alias color: bluerectangle.color
color: "red"
Rectangle{
id: bluerectangle
color: '#1234ff'
}
}
属性别名在QML中的使用技巧
本文探讨了QML中属性别名的正确使用方法,包括在组件初始化后的应用,以及如何避免在初始化前引用未定义的属性。同时,文章还讨论了属性别名与现有属性同名时的覆盖行为,提供了实例代码帮助理解。
1724

被折叠的 条评论
为什么被折叠?



