cmakeqmlqt-creatorqt6ubuntu-22.04

Qt Creator doesn't understand a valid QML module import


I have a CMake (3.22.1) project and I have added my QML (Qt SDK 6.5.5) module like this:

qt_add_qml_module(MyQmlModule
    URI MyQmlModule
    VERSION 1.0
    RESOURCE_PREFIX /
    QML_FILES ${QML_SOURCE_FILES}
)

While the project builds and works just fine, Qt Creator (13.0.0, 14.0.0) doesn't understand the import and it annoys me:

enter image description here

I need this import, because otherwise my singleton QML objects don't work:

set_source_files_properties(MySingleton.qml PROPERTIES QT_QML_SINGLETON_TYPE TRUE)

The error message says that QML_IMPORT_PATH should be in CMakeCache.txt, but not sure if that's related to this particular case as the project actually works.

Is there a way to make Qt Creator to understand my import or is it just a bug?


Solution

  • Placing these two lines in your main CMakeLists would probably tackle the problem:

    set(QT_QML_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/qml" CACHE PATH "Install path for QML" FORCE)
    set(QML_IMPORT_PATH ${CMAKE_BINARY_DIR}/qml CACHE STRING "Extra QML Import Paths" FORCE)
    

    As it seems like a cash error otherwise the project would not have run in the first place. Set the path for QMLs, store it as a cash, and then import it from the specified path.