C++ Communication with QML
-
I'm trying to get the QML side to communicate with C++ in a project. I have a class in my project and this class is called
RosQml
and inside this class there is a function calledgetButton()
.
I tried two ways and both times I get the same error.
c++
1->qmlRegisterType<RosQml> ("ROSNODE", 1, 0, "Rosnode");
2->qmlRegisterUncreatableType<RosQml> ("ROSNODE", 1, 0, "Rosnode", "Reference only");
QMLonClicked: { Rosnode.getButton() }
same error in both cases
error =TypeError: Property 'getButton' of object [object Object] is not a function
-
By registering a type, you just make it available to the QML engine. It allows you to do this in QML:
import ROSNODE 1.0 Rosnode { // An instance of C++ class, created in QML! }
If you want to create an instance of your class in C++ and use that same instance in QML ("share" the same object between QML and C++), then you need to use different approach. For example:
- add your object to root context property of your engine
- declare your object to be a QML singleton
- send the object to QML via a signal
- there are probably a few more ways