Lorenzo Aldrighetti
Lorenzo Aldrighetti

Reputation: 90

Set QML_IMPORT_PATH environment variable from CMake

I'm writing an application that imports an external QML Module with Qt 6.5.3 and CMake.

The official Qt documentation says that QML engine searches for identified modules in the default paths: [...] Paths specified by the QML_IMPORT_PATH environment variable

If in my CMakeLists.txt I add set(ENV{QML_IMPORT_PATH} "path/to/my/module"), the engine fails to load it and function QQmlEngine::importPathList() doesn't include the newly added.

The only working solutions are:

I don't want to hardcode the path of my module in my source code or remember to edit the build environment variables.

Am I incorrectly setting the QML_IMPORT_PATH environment variable, or am I missing something?

Below, a minimal reproducible example:

// CMakeLists.txt
cmake_minimum_required(VERSION 3.20)
project(QmlApp LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 REQUIRED COMPONENTS Core Quick)
qt_standard_project_setup(REQUIRES 6.5)

set(ENV{QML_IMPORT_PATH} "C:/Projects/QmlModules/SpecialModule")

qt_add_executable(qml-app
    main.cpp
)

target_link_libraries(qml-app
    PRIVATE Qt6::Core Qt6::Quick
)
// Main.cpp
#include <QGuiApplication>
#include <QQmlApplicationEngine>

int main(int argc, char *argv[])
{
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    qInfo() << engine.importPathList();
    // Prints only the current bin dir, two default Qt dirs and Qt install dir
    return 0;
}

Upvotes: 0

Views: 137

Answers (0)

Related Questions