Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use QML_ELEMENT with cmake

Tags:

c++

cmake

qt

qt5

qml

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

like image 738
너를 속였다 Avatar asked Aug 20 '20 16:08

너를 속였다


1 Answers

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:

  • 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.
like image 99
Adversus Avatar answered Sep 21 '22 09:09

Adversus