Home > Blockchain >  Fail to import QML module using CMake
Fail to import QML module using CMake

Time:11-29

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 ?

CodePudding user response:

I'm currently doing something similar.

I have created a demo app where I import modules. The modules provide QML and C items to the main app. Check the comments in the CMAKE files to find out how this works.

Here is the link:

https://gitlab.com/basic53/DemoApp

Feel free to comment on this.

Another tip: If qt_add_qml_module is not working properly, sometimes it is necessary to remove the whole build folder and update the QML code model. You can check the generated files to see, if your plugin has been created with all its types.

CodePudding user response:

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(":/");
  • Related