Unknown CMake command "qtquick_compiler_add_resources"

Viewed 1221

I want to use QtQuickCompiler for my project, but as soon as I call qtquick_compiler_add_resources I get the following error message:

Unknown CMake command "qtquick_compiler_add_resources". 

I use Qt6 and CMake 3.18.2. I have read a lot in the documentation, however everything here should be correct. That's my CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)

project(ProtoPaste LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC OFF)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_PREFIX_PATH "C:/Qt/6.0.0/msvc2019_64/lib/cmake")
set(APP_ICON_RESOURCE_WINDOWS "${CMAKE_CURRENT_SOURCE_DIR}/src/Resource.rc")
option(CLIP_TESTS OFF)
option(CLIP_EXAMPLES OFF)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Core Quick QuickControls2 Widgets Qt6QuickCompiler REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Quick QuickControls2 Widgets REQUIRED)

qtquick_compiler_add_resources(RESOURCES
    src/qml.qrc
    src/images.qrc
)

set(PROJECT_SOURCES
        src/main.cpp
        src/availableElement.cpp
        src/selectedElement.cpp
        src/availableElementsModel.cpp
        src/selectedElementsModel.cpp
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(ProtoPaste WIN32
        ${PROJECT_SOURCES}
        ${RESOURCES}
        ${APP_ICON_RESOURCE_WINDOWS}
    )
else()
    add_executable(ProtoPaste WIN32
        ${PROJECT_SOURCES}
        ${RESOURCES}
        ${APP_ICON_RESOURCE_WINDOWS}
    )
endif()

add_subdirectory(dependencies)

target_include_directories(ProtoPaste PRIVATE ${clip_SOURCE_DIR})
target_compile_definitions(ProtoPaste PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(ProtoPaste
  PRIVATE Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Quick Qt${QT_VERSION_MAJOR}::QuickControls2 Qt${QT_VERSION_MAJOR}::Widgets clip)

What am I doing wrong?

1 Answers

tl;dr

Use qt_add_qml_module instead

Full answer:

With Qt6 it seems qtquick_compiler_add_resources has been removed. The new CMake function within Qt6QmlMacros.cmake is qt_add_qml_module. The function you are looking for had previously been available in Qt5QuickCompilerConfig.cmake. The only mention of that function I could find in Qt6 is in the source file:

declarative/tools/qmlcachegen/Qt6QuickCompilerConfig.cmake.in

The function declared there simply contains this warning:

function(qtquick_compiler_add_resources)
    message(WARNING "Use qt_add_qml_module instead of qtquick_compiler_add_resources."
                    "QML and JavaScript files are automatically compiled when creating a QML"
                    " module.")
endfunction()

Asking Qt support about this I was directed to this blog: https://www.qt.io/blog/introduction-to-the-qml-cmake-api which is very helpful and suggests the reasons for this change.

This new function defaults to compiling the qml into the specified location in the resource file system. It is up to the user to disable compiling.

For those of us supporting CMake-based projects that must work with older Qt versions (e.g. Qt5.6) as well as Qt6.x this means splitting apart qrc files that had previously contained both pure resources (e.g. svg/png) and qml files. The resources can be compiled with qt_add_resources and the qml files can be added with qt_add_qml_module

EDIT: looking a bit more closely, it appears resources could also be compiled in to the qml module via qt_add_qml_module.

Related