从 C++ 访问 QML 对象的属性没有 Q_PROPERTY 定义
我知道,可以定义具有自定义属性的 QObject 并在 QML 环境中公开该对象。但这样,对于每个新属性,我都需要重新编译 C++ 代码。
是否可以进行从 C++/Qt 到 QML 对象的动态绑定? 比如:
//C++ code:
updateProperty("myQmlObject.any_property", "Hello World");
谢谢!
已解决:
_view->rootContext()->setContextProperty( "cppmessage" , "Hello from C++" );
其中:view是一个QDeclarativeView,并且cppmessage在QML中使用,没有事先声明,例如:“text:cppmessage”
此链接很有用寻找解决方案: http:// xizhizhu.blogspot.com/2010/10/hybrid-application-using-qml-and-qt-c.html
I know, it's possible to define a QObject with custom properties and expose this object in QML environment. But this way, for each new property I'd need to recompile C++ code.
Is it possible, to make a dynamic binding from C++/Qt to QML-objects?
Something like:
//C++ code:
updateProperty("myQmlObject.any_property", "Hello World");
Thank you!
SOLVED:
_view->rootContext()->setContextProperty( "cppmessage" , "Hello from C++" );
WHERE: view is a QDeclarativeView, and cppmessage is used in QML without prior declaration like: "text: cppmessage"
This link was usefull for finding the solution: http://xizhizhu.blogspot.com/2010/10/hybrid-application-using-qml-and-qt-c.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,这是可以做到的。 链接
编辑:1
这里列出了 @Valentin 建议的另一种方法
链接
Yes, This can be done. Link
Edit:1
Another way to do it , as suggested by @Valentin is listed here
link