[Interest] Problems with qt_add_qml_module

Ulf Hermann ulf.hermann at qt.io
Tue Oct 24 16:11:06 CEST 2023


> Most of the context would be injected into the QML runtime from C++ via 
> calls to `setContextProperty()`, as well as by defining additional QML 
> modules from C++ (via `qmlRegister...()`).

This is where your architecture fails. By using setContextProperty() and 
procedurally registering types, you

a, still make your types depend on the context (due to context 
properties). You still won't be able to use them in standalone tools.

b, prevent QML tooling such as qmllint, qmlcachegen, qmlls to do 
anything useful with your QML code since they cannot see through your 
procedural type registrations or your context properties.

Instead of this:

1. Use singletons or object properties instead of context properties. 
There are also initial properties you can pass to QQmlComponent and 
QQmlApplicationEngine if that helps.

2. Use QML_ELEMENT and friends as well as qt_add_qml_module in CMake to 
define your types declaratively so that tooling can see them.

This way you obtain truly re-usable QML modules you can test in 
isolation. You also won't need to implement your own plugins or fight 
with the linker since qmltyperegistrar will generate the registration 
code and the hacky symbol references for you. This is the whole point of 
qt_add_qml_module. If you don't want qmltyperegistrar to do its work, 
there is little point in using qt_add_qml_module at all.


More information about the Interest mailing list