Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add and use a resource in QML?

I want to use a QtQuick Image object in a simple Qt project, and include the image in the project. I have added the image to the resource file. I have used debugging to be sure that the resource exists and is compiled into the application as "Resources/myfile.png"

However if I declare a QtQuick image object and set the source to "Resource/myfile.png" the image is not found. Instead I get a message:

QML Image: Cannot open: file:///C:/Users/me/Documents/QtCreator/build-myapp-Desktop_Qt_5_1_1_MinGW_32bit-Debug/qml/myapp/Resources/dial_bg.png

The same occurs if I try to use the C++ approach to accessing the file

source: ":/Resources/dial_bg.png"

This gets me an error at compile time.

myfile.png has not been copied to that location. myfile.png doesn't appear in the project files, although it has been added to the resources file.

I'll be happy with a solution that either gets the image copied to the place where the Image wants to pick it up, or a way for Image to access the 'compiled in' version.

like image 598
DJClayworth Avatar asked Oct 29 '13 18:10

DJClayworth


People also ask

How do I add a resource file to Qt?

In the Location field, specify a location for the file. Select Add to create a . qrc file and to open it in the Qt Resource Editor. To add resources to the file, select Add > Add Files.

How do I use QRC files?

Using a QRC file qrc file in your application you first need to compile it to Python. PyQt5 comes with a command line tool to do this, which takes a . qrc file as input and outputs a Python file containing the compiled data. This can then be imported into your app as for any other Python file or module.

How do you use QML in Qt?

Creating and Running QML Projects For simple UI files such as this one, select File > New File or Project > Application (Qt Quick) > Qt Quick Application - Empty from within Qt Creator. Pressing the green Run button runs the application. You should see the text Hello, World! in the center of a red rectangle.

How do I edit a QML file?

In Projects, double-click a . qml file to open it in the code editor. Then select the Design mode to edit the file in the visual editor.


3 Answers

If you're using Qt Creator you can right click on the resource and Copy Path which in my case gives me:

qrc:/Oscar.PNG

You don't need directly reference the resources directory as suggested by selected answer.

Example:

Example of copying resource path

like image 136
BugHunterUK Avatar answered Oct 20 '22 16:10

BugHunterUK


The way to access such resources in QML is as:

source: "qrc:///Resources/dial_bg.png"
like image 40
DJClayworth Avatar answered Oct 20 '22 16:10

DJClayworth


qml.qrc file

<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
    <file alias="main.qml">qml/main.qml</file>
</qresource>
<qresource prefix="/pics/">
    <file alias="bottom_arrow.png">pics/bottom_arrow.png</file>
</qresource>
</RCC>

In main.cpp file to load a qml file:

...
QString qml = QStringLiteral("qrc:///main.qml");
engine.load(QUrl(qml));
...

In main.qml file to load an image:

...
Image {
  source: "pics/ef-logo.png"
}
..

In QT, it somehow AUTORCC qrc file to code. But if you are using Cmake and visual studio. then in Cmake you need to include this :

set(CMAKE_AUTOMOC ON)                   #must have for QML to work
set(CMAKE_AUTORCC ON)                   #for compiling qrc file to rcc
set(AUTORCC_OPTIONS "src/qml.qrc")

I hope this will help. It may not be the best way, but still

like image 34
Raimondas Klemka Avatar answered Oct 20 '22 14:10

Raimondas Klemka