Qt Image from resource file
Did you remember to run qmake after adding the resource file?
First of all, you need to create a .qrc file and add image folder to it
- (image folder must be contained inside of the project folder)
- Right-click on the project file
- Add New
- Qt
- Qt Resource File press Choose and do other steps
- after opening .qrc file you must press Add > Add Prefix > change prefix name if you want
- again Add > Add File > and choose your images
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"));
All this will work if your png files are located in the same folder as .pro, .qrc, and .cpp files of your project.
Usually it is convenient to put all images to special subfolder Resources, for example. Then in .qrc the line will look like:
<file>Resources/green.png</file>
And in .cpp file:
QImage *green = new QImage(":/Resources/green.png");