Important: Please read the Qt Code of Conduct -

Error creating a QQmlComponent (resource not found)

  • Hi everyone.

    I am having several problems porting a quick application from Android to Desktop.
    In this process I must to change from .pro to CMakeLists.txt (I am working on ROS and I need to define a CMakeLists.txt).

    The problem is that I cant find the resources files when I try to compile with cmake. However I have not problems when I use the .pro file.

    My system files:

    |- Project
        |- CMakeLists.txt
        |- src/
            |- main.cpp
            |- ...
        |- resources/
            |- main.qml
            |- qml.qrc
        |- ... 

    The CMakeLists.txt contains:

    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    set(QRC_RESOURCES ${CMAKE_CURRENT_SOURCE_DIR}/resources/qml.qrc)

    In the Andoid version (working properly), i had put in my .pro:

    RESOURCES += resources/qml.qrc

    The .qrc file:

        <qresource prefix="/">

    And finally, in my code:

    QQmlComponent component(&engine, QUrl(QStringLiteral("qrc:/main.qml")));

    The execution error is:

    (qrc:/main.qml: File not found) 

    How do I to find the resouces files with cmake compilation?

    Thank you so much,

  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Did you already check the cmake manual from Qt's documentation ?

  • With cmake, there are three things you do (or at lest I do) to get the resources added.


    So you :

    • create the variable to hold the src file
    • use the qt5_add_resources macro
    • add the files from the qt5_add_resources macro to your executable (or library), as you would other sources.

    Hope this helps.

  • Also, please add the cmake tag to this post; I don't think a responder can.

  • I solved this problem.

    I just added this lines in my CMakeLists.txt

    qt5_add_resources(QT_RESOURCES_CPP resources/qml.qrc)
                     src/my_app.cpp src/my_app.h ${QT_RESOURCES_CPP})

    The problem was that I was adding my reference to QT_RESOURCES_CPP in the target_link_libraries(...) instead of add_executable(...). For some reason this didn't work properly.

    Thank you,