c++qtcmakeqmlqt5

How to use QML_ELEMENT with cmake


The doc shows I can use QML_ELEMENT macro to create QML types from C++ by adding some variables in qmake's .pro file. But I'm using cmake


Solution

  • Update (Qt 6.2+)

    As of Qt 6.2, qt_add_qml_module is a single command for building qml modules that should take care of virtually everything, replacing amongst others the old qt6_qml_type_registration command.

    Old answer (Qt 6.0/6.1)

    Now that Qt 6.0 is out this is supported, albeit poorly documented. What you need now is:

    set_target_properties(foo PROPERTIES
        QT_QML_MODULE_VERSION 1.0
        QT_QML_MODULE_URI     Foo
    )
    
    qt6_qml_type_registration(foo)
    

    you can then do in qml:

    import Foo
    

    and you'll have access to types that have QML_ELEMENT and friends. Notes: