Load the QML plugin automatically when application starts
-
Mac Qt 6.4
Created the plugin using qt_add_qml_module. I have set QML_IMPORT_PATH = /Users/dheeru/libs.
This fine.
I would like the plugin in /Users/dheeru/libs/com/pthinks/libMyModulePlugin.dylib(Mac) to be automatically loaded.
Is there any environment variable to set such that above library is loaded by QmlApplication Engine ?I don't want write a code in main.cpp to load the plugin using QPluginLoader.
Is this possible ?
-
The plugin needs to be specified in the qmldir file.
Here is a small example of how it works.
Plumbing withQPluginLoader
inmain.cpp
is not necessary. -
Thank you @Axel-Spoerl. QML file(MyPthinkSMain.qml) to load is present in the plugin itself. Code snippet
QPluginLoader loader("/Users/dheeru/libs/India/Bengaluruu/PthinkS/QtExperts/libPthinkSUtilplugin.dylib"); loader.load(); const QUrl urlu"qrc:/India/Bengaluruu/PthinkS/QtExperts/MyPthinkSMain.qml"_qs); engine.load(url);
The file qrc:/India/Bengaluruu/PthinkS/QtExperts/MyPthinkSMain.qml comes from the plugin libPthinkSUtilplugin.dylib itself. If I comment loader.load(), application does not show up window.
As I understand plugin will be loaded once engine see the import statement in qml. Hope this is what you are describing. In my use case qml file itself comes from the plugin built using qt_add_qml_module.