从C++导入到qml的全局变量和qml定义变量或许有些混淆,使用下列建议或许可以解决全局变量混淆的问题。
定义qml变量相当于定义JavaScript变量。JavaScript变量命名必须以字母,_
,$
为开头。其他字符可以是字母,_
,$
,数字。
全局变量或外部变量可以使用$
修饰变量。
$message
变量到qml中:...
engine.rootContext()->setContextProperty("$message", "hello world!");
...
...
Component.onCompleted: console.log($message)
...
/* BaseItem.qml */
Item {
property variant $message: "hello world!"
...
}
引用BaseItem.qml的全局变量(外部变量)$message
:
BaseItem {
...
Component.onCompleted: console.log($message)
...
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有