Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

qt

qt5

qml

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.

like image 947
ManuelSchneid3r Avatar asked Dec 19 '22 19:12

ManuelSchneid3r


1 Answers

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.

like image 137
talamaki Avatar answered Dec 21 '22 09:12

talamaki