How to Register your C++ Class as a QML Type
How to Register your C++ Class as a QML Type :
The second possibility to use C++ components in QML is to register the class as a QML type. This allows to create objects (= instances) of your type directly in QML instead of C++. And the best thing is, the concepts with signals, slots and properties we used in the previous example still apply.
When to Use a Context Property and when a QML Object
If thereβs only a single object instance you want to work with in QML you can add the object as a context property. When there can be multiple instances of your class, register it as a QML type and create the objects directly in QML where you need it.
1. For this example, we will create a new type we can use in QML. Letβs start with adding a new C++ Class named SignalClass.

2. Replace the code in signalclass.h with this implementation:
3. To complete the class, add the following code for signalclass.cpp:
Register and Use your C++ QML Type
1. In your main.cpp, first add an include statement for the new class:
The method takes several parameters: The module identifier and version define the required QML import to use the type. The last parameter holds the name of the QML type, which can be different from the actual C++ class name.
3. Add the import which matches the used configuration of qmlRegisterType to your main.qml:
4. For an example usage of our new QML Type, add the following snippet below the first example in main.qml file:
Full main.qml file code

Note: You can find the whole source code on my github.
Last updated
Was this helpful?