4
votes

I just realized that I can call pretty much any function of an object that is exposed to QML. Now I am curious about Q_INVOKABLE. The Qt5 docs state:

[...] any QML code can access the following members of an instance of a QObject-derived class:

  • Properties

  • Methods (providing they are public slots or flagged with Q_INVOKABLE)

  • Signals

Since Qt5 (In C++) I can invoke any public function of a QObject like a slot, i.e. I do not have to declare them as 'public slot'. Does this mean I can call any method from QML? I cant find anything in the docs.

1
What do you mean by "I can invoke any public function like a slot." ?GrecKo
Please provide a minimal example where you call a non-invokable, non-slot function from QML.Mitch
While trying to provide an example where I call a non-invokable, non-slot function from QML, I realized that its not possible. Wow I dont know how I came to believe that it would work ... :/ Thank youManuelSchneid3r

1 Answers

11
votes

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.

Both Q_INVOKABLE and the slots keyword register your function with Qt meta-system. The difference is that with Q_INVOKABLE you can return values.