'Different results when using CMake and QMake C++ QT

If I do the following steps for QMake and CMake I get two different results:

  1. create new project
  2. add Image to main window
  3. add ressource file to project (on qmake it gets automatically added to .pro file, but on cmake I add it manually as an executable)
  4. add test image to ressoure file
  5. change image source to "/test.png".

Here is the source code of both of the projects:

Both - main.qml:

import QtQuick

Window {
    width: 640
    height: 480
    visible: true
    color: "#c03434"
    title: qsTr("Hello World")

    Image {
        id: image
        anchors.fill: parent
        source: "/test.png"
        fillMode: Image.PreserveAspectFit
    }
}

Both - QRessource:

<RCC>
    <qresource prefix="/">
        <file>test.png</file>
        <file>main.qml</file>
    </qresource>
</RCC>

CMakeList.txt:

cmake_minimum_required(VERSION 3.16)

project(untitled10 VERSION 0.1 LANGUAGES CXX)

set(CMAKE_AUTOMOC ON)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(Qt6 6.2 COMPONENTS Quick REQUIRED)

qt_add_executable(appuntitled10
    main.cpp
    rc.qrc
)

qt_add_qml_module(appuntitled10
    URI untitled10
    VERSION 1.0
    QML_FILES main.qml 
)

set_target_properties(appuntitled10 PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

target_compile_definitions(appuntitled10
    PRIVATE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:QT_QML_DEBUG>)
target_link_libraries(appuntitled10
    PRIVATE Qt6::Quick)

QMake.pro:

QT += quick

SOURCES += \
        main.cpp

resources.files = main.qml 
resources.prefix = /$${TARGET}
RESOURCES += resources \
    rc.qrc

# Additional import path used to resolve QML modules in Qt Creator's code model
QML_IMPORT_PATH =

# Additional import path used to resolve QML modules just for Qt Quick Designer
QML_DESIGNER_IMPORT_PATH =

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

I used the same image and it was both times in the project folder. It's a QT6 quick project.

The qmake version runs just fine, but the cmake version says "QML Image: Cannot open: qrc:/test.png". I tried to add main.qml to the ressource file, but it wont help. Also if I change "/test.png" to ":/test.png", there is no error, but also no image shown.

I'm really frustrated having this problem, on such a so simple task. Any help or thoughts are appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source