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
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.
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:
Two files are created in the build output folder, <project>_qmltyperegistrations.cpp
and <project>.qmltypes
, if your imports are failing you can look at those to see which types are missing. I found that I needed to do full recompiles sometimes after adding/removing registered types.
Qt examples have been migrated to cmake, so take a look at e.g. Examples/Qt-6.0.0/quick/tableview/gameoflife
to see it in action
There are now pro2cmake.py
and run_pro2cmake.py
files in the Qt sources at Qt/6.0.0/Src/qtbase/util/cmake
. They are mentioned on this Readme page, you can find them here, haven't tried it myself.