c++qtcmakeqml

Fail to import QML module using CMake


I'm currently building a minimalist app following this CMake architecture:

-root

--QmlModule

---Component1.qml

---Component2.qml

--App1

---main.cpp

---main.qml

--App2

---main.cpp

---main.qml

I use "qt6_add_qml_module" to create a QML module at "QmlModule" level as a STATIC library.

qt_add_library(myComponentTarget STATIC)
qt6_add_qml_module(myComponentTarget
URI QmlModule
VERSION 1.0
QML_FILES
    Component1.qml
    Component2.qml
RESOURCES
    logo.png)

Then, at App1 (and App2) level, a link to the module is done using "target_link_libraries". "qt6_add_qml_module" does some work behind the scenes in order to expose the module trough an automatically generated plugin named "your_component_URIplugin". More details about this here.

add_executable(App1Exe 
    main.cpp)
qt6_add_qml_module(App1Exe
    URI App1
        VERSION 1.0
    QML_FILES
        main.qml)
target_link_libraries(App1Exe 
    PRIVATE 
    myComponentURIplugin)

At Root level, I overload QML_IMPORT_PATH in order to link to the build folder and add all subdirectories.

set(QML_IMPORT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/qmlModule)
add_subdirectory(QmlModule)
add_subdirectory(App1)
add_subdirectory(App2)

I run CMake without any errors, and open App1/main.qml file. On my import QmlModule, the module can't be found:

module "lupinComponentsplugin" is not installed

How to make my module visible from my Apps ? What step am I missing ?


Solution

  • CMake itself was fine, this was a runtime error and not a link error.

    This issue was raised because the QQmlApplicationEngine wasn't finding path towards my module's QMLDIR. In the end, the only thing missing was an additional import path ":/" in QQmlEngine:

    QQmlApplicationEngine engine;
    engine.addImportPath(":/");