简体   繁体   English

在Qt5中,Q_INVOKABLE是否需要从QML调用公共QObject函数?

[英]Is Q_INVOKABLE needed to invoke a public QObject function from QML at all in Qt5?

I just realized that I can call pretty much any function of an object that is exposed to QML. 我刚刚意识到我几乎可以调用暴露给QML的对象的任何函数。 Now I am curious about Q_INVOKABLE. 现在我对Q_INVOKABLE感到好奇。 The Qt5 docs state: Qt5 文档声明:

[...] any QML code can access the following members of an instance of a QObject-derived class: [...]任何QML代码都可以访问QObject派生类的实例的以下成员:

  • Properties 属性

  • Methods (providing they are public slots or flagged with Q_INVOKABLE) 方法(提供它们是公共插槽或标记为Q_INVOKABLE)

  • Signals 信号

Since Qt5 (In C++) I can invoke any public function of a QObject like a slot, ie I do not have to declare them as 'public slot'. 从Qt5 (在C ++中)开始,我可以像插槽一样调用QObject的任何公共函数,即我不必将它们声明为“公共插槽”。 Does this mean I can call any method from QML? 这是否意味着我可以从QML调用任何方法? I cant find anything in the docs. 我无法在文档中找到任何内容。

Yes, you have to mark your function of a QObject with Q_INVOKABLE unless it's a public slot in order to be able to call it from QML. 是的,您必须使用Q_INVOKABLE标记QObject的功能,除非它是public slot ,以便能够从QML调用它。

Both Q_INVOKABLE and the slots keyword register your function with Qt meta-system . Q_INVOKABLE和slots关键字都使用Qt meta-system注册您的函数。 The difference is that with Q_INVOKABLE you can return values . 不同之处在于使用Q_INVOKABLE 可以返回值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM