Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt Image from resource file

I'm trying to insert an image to my program via resource file, which is like:

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

and when I'm trying to load it using QImage or QPixmap, like:

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

I always see that null message, indicating that I'm doing something wrong. One solution may be using absolute path like

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

which works of course, but I'd prefer implement it using resource file. Could you please give me some advice?

like image 703
didgogns Avatar asked Jul 14 '15 12:07

didgogns


1 Answers

First of all, you need to create a .qrc file and add image folder to it

  1. (image folder must be contained inside of the project folder)
  2. Right-click on the project file
  3. Add New
  4. Qt
  5. Qt Resource File press Choose and do other steps
  6. after opening .qrc file you must press Add > Add Prefix > change prefix name if you want
  7. again Add > Add File > and choose your images
  8. then go to mainwindow.cpp (in my project ) file and call the images as below code

    • in my case the icon folder is Playericons
ui->play->setIcon(QIcon(":/Playericons/icons8-pause-30.png"));
like image 180
Hishan_98 Avatar answered Sep 21 '22 23:09

Hishan_98