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_AUTOMOC ON)
    set(CMAKE_INCLUDE_CURRENT_DIR ON)
    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:

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

    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,
    jonyAL


  • 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.

    SET(PROJECT_RESOURCES
        ../resources/resources.qrc
    )
    qt5_add_resources(PROJECT_RESOURCES_CXX ${PROJECT_RESOURCES})
    
    add_executable(${PROJECT_NAME} ${${PROJECT_NAME}_SOURCE} ${PROJECT_RESOURCES_CXX})
    
    

    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)
    
    add_executable(my_app
                     src/my_app.cpp src/my_app.h ${QT_RESOURCES_CPP})
    
    target_link_libraries(my_app
                            lib1
                            lib2
                            ...)
    

    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,
    Jonatan


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.