...">

Qt Image from resource file

I am trying to insert an image into my program through a resource file that looks like:

<RCC> <qresource prefix="/"> <file>green.png</file> <file>other files</file> </qresource> </RCC> 

and when I try to load it using QImage or QPixmap, for example:

 QImage *green = new QImage(":/green.png"); if(green->isNull()) qDebug("null"); 

I always see this null message indicating that I am doing something wrong. One solution might be to use an absolute path such as

 "C:\\Users\\user\\Documents\\project\\green.png", 

which works of course, but I would prefer to implement it using a resource file. Could you give me some advice?

+5
source share
2 answers

All this will work if your png files are in the same folder as the .pro, .qrc and .cpp files of your project.

It is usually convenient to place all images in a special "Resources" folder, for example. Then in .qrc the line will look like this:

 <file>Resources/green.png</file> 

And in the .cpp file:

 QImage *green = new QImage(":/Resources/green.png"); 
+3
source

Failed to start qmake after adding resource file?

0
source

All Articles