Zeb
Zeb

Reputation: 47

How to use an image in QML

I am very new to Qt and trying

import QtQuick 2.5
Image {
    id: root
    source: "Background.png"
}

in Qt Quick Application and getting the following error

qrc:/main.qml:3:1: QML Image: Cannot open: qrc:/Background.png

In response to similar but older questions it is suggested that we need to put the image in the Resource folder, but Resources folder is just a pseudo folder, not visible in folder tree.
So how may I put the image in Resources folder or how may I use it otherwise?

Upvotes: 0

Views: 2833

Answers (2)

You can use source: "file:///C:/Users/your_user/Images/Image.png" If don't have the image added to your qrc resource you can use the absolute image path but you will have an issue if you deploy the application and the image path changes.

Upvotes: 0

jpnurmi
jpnurmi

Reputation: 5836

Take a look at the Qt resource system. In essence, you'll create a .qrc file where you list your resources, and add it to the RESOURCES variable in the application .pro file. You can do all this in Qt Creator as well, and the default Qt Quick Application templates already have resources setup, so all you need is to add your background.png there.

Upvotes: 1

Related Questions