Create linked qml module
-
wrote 6 days ago last edited by Andrew Strokov 5 Dec 2025, 09:15
I create qml module DataModel
qt_add_library(DataModel SHARED) qt_add_qml_module(DataModel URI DataModel VERSION 1.0 PLUGIN_TARGET DataModel TYPEINFO plugins.qmltypes SOURCES data_model.h data_model.cpp)
DataModel class is QML_SINGLETON
Can i create a module like this?
In qt guide https://doc.qt.io/qt-6/qtqml-writing-a-module.html
they recommends create qt_add_library with STATIC flag -
wrote 6 days ago last edited by
Yes, you can create a QML module like this using qt_add_library(DataModel SHARED) — it is valid and supported by Qt, especially when your module is meant to be used as a shared QML plugin across multiple applications.
The Qt guide recommends using STATIC for modules that are internal to a specific application because:
Static libraries avoid deployment issues with plugins (like qmldir, plugin paths, etc.).
They simplify application packaging since everything is built into the app binary.
However, in your case:
You're defining a QML_SINGLETON, which is typically registered via a plugin.
You're using PLUGIN_TARGET, which indicates you're building a QML plugin.
Shared builds make sense when the module is intended to be reused and dynamically loaded by the QML engine.
-
Yes, you can create a QML module like this using qt_add_library(DataModel SHARED) — it is valid and supported by Qt, especially when your module is meant to be used as a shared QML plugin across multiple applications.
The Qt guide recommends using STATIC for modules that are internal to a specific application because:
Static libraries avoid deployment issues with plugins (like qmldir, plugin paths, etc.).
They simplify application packaging since everything is built into the app binary.
However, in your case:
You're defining a QML_SINGLETON, which is typically registered via a plugin.
You're using PLUGIN_TARGET, which indicates you're building a QML plugin.
Shared builds make sense when the module is intended to be reused and dynamically loaded by the QML engine.
wrote 6 days ago last edited by@SuhasKrishanamurthy Thank you! By the way, using qt_add_qml_module together with qt_add_library or qt_add_executable means embedding the qml module?
and if you use qt_add_qml_module separately, will a plugin be created? -
wrote 6 days ago last edited by
Yes — if you combine qt_add_qml_module() with a STATIC library or an executable target without a PLUGIN_TARGET, the QML types get embedded in the binary. No plugin is generated.
And if you define qt_add_qml_module() with a PLUGIN_TARGET, and the library is built as SHARED, Qt will generate a plugin that the QML engine can dynamically load.
-
Yes — if you combine qt_add_qml_module() with a STATIC library or an executable target without a PLUGIN_TARGET, the QML types get embedded in the binary. No plugin is generated.
And if you define qt_add_qml_module() with a PLUGIN_TARGET, and the library is built as SHARED, Qt will generate a plugin that the QML engine can dynamically load.
wrote 6 days ago last edited by@SuhasKrishanamurthy Thanks! :)
-
1/5