PDA

View Full Version : How to load icons from resource file



joseph
27th June 2007, 15:08
hi ,

Could you please explain , how to use resources file .
What i did........
1) created a main window,
2) added QActions on main window . and trying to put icons( .png ) on actions.
3) created My_Resource.qrc, then added imageOne.png , imageTwo.png ...etc from the Application directory.

see the code here, its not showing the "imageOne.png " in the QAction on mainwindow.


MyMainWindow::MyMainWindow(QWidget *parent)
: QMainWindow(parent)
{
QAction* actNew = new QAction( QIcon("imageOne_1.png") , "&New File ..", this);

QMenu *fileMenu = menuBar()->addMenu(QObject::tr("&File"));

fileMenu->addAction( actNew );

}



please help how to load the image from the .qrc file.
How it is different from load image directly from a realative path..??
NB : I am using VC++ editor

thanks

rmagro
27th June 2007, 15:24
copy the image into the application path..(the same path that contains the debug folder)

Then if you didn't change the prefix while you added the image in My_Resource.qrc

replace the line

QAction* actNew = new QAction( QIcon("imageOne_1.png") , "&New File ..", this);

with this


QAction* actNew = new QAction( QIcon(":/imageOne_1.png") , "&New File ..", this);

Please note that the name of the file has to be exact (it is case sentitive)
Even .PNG is different from .png...so pay attention..

at the end ..compile again and let us know ;)

CU

joseph
27th June 2007, 15:34
I have tried this. But it's not working

see my "My_Resource.qrc"




+ /new/prefix
|___Resources/imageOne.png



NB: "Resources" is a directory

Any other way..???
thanks

jacek
27th June 2007, 15:55
Could you post the contents of My_Resource.qrc?

rmagro
27th June 2007, 15:58
create a new .qrc file from the editor

open it

then right clik and choose change prefix...
write "images"

now you will see /images appearing on the top.

copy the file where I told you before, not under Resources,
but at the same level as Resources..

choose (always right click) "Add file", PNG image and IMPORT
pick up the file and when a question is prompted to you choose NO!


Modify the code like this:


QAction* actNew = new QAction( QIcon(":/images/imageOne_1.png") , "&New File ..", this);

CU

joseph
28th June 2007, 08:16
Thank you

I got the solution
What i did...


1) I have changed the prefix in .qrc file to ( Images )
2) Then Added the images.png files from folder ( Images ) to .qrc file.
3) Added the auto-generated "MyResourcefile.cpp" to Generated files in SolutionExplorer ( VC++ Editor )
4) In coding ...


QAction* actNew_1 = new QAction( QIcon(":Images/Resources/glass_numbers_0.png") , "&New File ..", this);


5) In "MyRsourcefile.qrc" which is added in Resource Files ( In Solution Explorer )



+ /Images
|_____Resources/ImageOne.png
|_____Resources/ImageTwo.png
|_____Resources/ImageThree.png


NB: I think the benefit from using Resources files is..........
1) if you are loading image directly from Imgae folder to coding ,and suppose the imageOne.png is missing in folder , UI will not able to locate the image.
2) While using resource file , Even if the Imageone.png is missing from Image-Folder , the UI will get he image as it is added in the resources. Is it right or not ...???

jacek
28th June 2007, 17:04
NB: I think the benefit from using Resources files is..........
1) if you are loading image directly from Imgae folder to coding ,and suppose the imageOne.png is missing in folder , UI will not able to locate the image.
2) While using resource file , Even if the Imageone.png is missing from Image-Folder , the UI will get he image as it is added in the resources. Is it right or not ...???
Yes, if you use resources, you simply won't lose any images, because they're part of the executable file.

riarioriu3
25th June 2012, 07:00
What do you mean by change the prefix ??