2

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 ?

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

1

I am trying to add common custom QML controls to my project in the same way, i.e. I have my QmlModule folder outside the project. I found the Qt documentation did not go far enough in terms of fully worked examples for what is quite a complex subject. My problem was that it failed to load the plugin library AT RUNTIME, even though the DLL named in the error message was present. I followed Jürgen Lutz's DemoApp and worked out that the critical missing step was

target_link_libraries(${EXECUTABLE_NAME} PRIVATE myComponentTarget)

in my application CMakeLists.txt. Note that this is the library named in the plugin's CMakeLists.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.