runtime error: module is not installed when using qt_add_qml_module()
-
I'm following various examples and documentation trying to learn how to use qt_add_qml_module() to create a simple module mymodule using cmake and have my windows QML application import and use that module.
I get the following error message at run-time:
qrc:/main.qml:4:1: module "modules.mymodule" is not installed
I think I am missing some fundamental understanding or step.
The file modules/mymodule/CMakeLists.txt is:cmake_minimum_required(VERSION 3.16) qt_add_qml_module(mymodule URI modules.mymodule VERSION 1.0 QML_FILES MyRect.qml SHARED)
The file modules/mymodule/MyRect.qml is:
import QtQuick import QtQuick.Controls.Basic import QtQuick.Layouts Item { id: root implicitWidth: 50 implicitHeight: 50 Rectangle { color: "blue" anchors.fill: parent } }
The main.qml file imports this module and tries to display a MyRect:
import QtQuick import modules.mymodule ApplicationWindow { id: root visible: true width: 1000 height: 800 MyRect { } ...
The CMakeLists.txt for the main application includes the following:
qt_add_executable(mytestapp main.cpp) set_target_properties(mytestapp PROPERTIES WIN32_EXECUTABLE ON) target_link_libraries(mytestapp PRIVATE Qt6::Core Qt6::Gui Qt6::Qml Qt6::Quick) qt_add_qml_module(mytestapp URI main VERSION 1.0 QML_FILES main.qml NO_RESOURCE_TARGET_PATH NO_PLUGIN IMPORTS modules.mymodules) find_program(WINDEPLOYQT_EXE windeployqt.exe) add_custom_command( TARGET mytestapp POST_BUILD COMMAND ${WINDEPLOYQT_EXE} --no-translations $<TARGET_FILE:mytestapp > --qmldir ${CMAKE_CURRENT_SOURCE_DIR} COMMENT "Running windeployqt for target mytestapp " )
And the main.cpp is:
int main(int argc, char **argv) { QGuiApplication app(argc, argv); app.setApplicationName(QFileInfo(app.applicationFilePath()).baseName()); QQmlApplicationEngine engine; engine.load(QUrl("qrc:/main.qml")); return app.exec(); }
I have been able to work-around the error if I change mymodule to be built as a STATIC library instead of SHARED, and also explicitly add mymodule to the target_link_libraries() of the main applicaiton. I don't understand why using STATIC works, but using SHARED doesn't. When using a SHARED library for mymodule, I can see the .dll file mymodule.dll being copied to the application build directory, but the error says "module "modules.mymodule" is not installed". What does 'installed' mean in this context?