Ubuntu – Add image files in Ubuntu QML app with C++ plugins (qrc)


I'm writing an Ubuntu QML (phone) App with C++ plugins using ubuntu-sdk. My app includes a png image and in order to include the image in the binary (so it shows on the phone) I have created a qrc file.

My qrc file looks like this:

<!DOCTYPE RCC><RCC version="1.0">

I've tried what QML app with C++ plugin (cmake) adding resource file suggests and got the Desktop version to work. However, when I run the app on my Aquaris E4.5 the image doesn't show up and I get the following error:

QML QQuickImage: Cannot open:

The image I want to show is stored in the /ProjectName/app/graphics directory. I'm including the qrc file within the backend directory, and added the lines the linked question indicates in the CMakeLists.txt within the backend directory:

qt5_add_resources(RESOURCES modules/ProjectName/myres.qrc)

Where should the image/qrc files be stored, or what other configuration is necessary for packing the image in the binary and launching the app successfully on an Ubuntu Phone?

I'm on Ubuntu 15.10 using the latest ubuntu-sdk version.

Best Answer

  • This is what worked for me.
    My resources.qrc is in {PROJECT_DIR}/resources:

        <qresource prefix="/180">
            <file alias="five">180/five_180.png</file>

    In my backend/CMakeLists.txt I have:

    qt5_add_resources(RESOURCE_ADDED ${CMAKE_SOURCE_DIR}/resources/resources.qrc)
    add_library(Projectnamebackend MODULE
    add_custom_target(Projectnamebackend-qmldir ALL
        COMMAND cp ${CMAKE_CURRENT_SOURCE_DIR}/modules/Projectname/qmldir ${CMAKE_CURRENT_BINARY_DIR}/Projectname

    I can then use the images in a .cpp-file like this:

    m_image = QImage(":/180/one");

    The project has the default example-app structure, given by the IDE.
    I hope this helps.