tags:

views:

94

answers:

2

I have a png file on disk at compile time. I'd like to have it included into the compiled executable. How do I define such an icon in Qt?

+6  A: 

You basically need to use the Qt resource system.
Check out Compiled-In Resources here.

Lets say this this your resource file

<!DOCTYPE RCC><RCC version="1.0">
 <qresource>
     <file>images/copy.png</file>
     <file>images/cut.png</file>
     <file>images/paste.png</file>
 </qresource>
</RCC>

In your source you can now create QIcons by referencing images from the resource

QIcon(":/images/cut.png")

Don't forget to reference the resource file in your .pro

 RESOURCES = application.qrc

This example uses images in Resource file for the icons

Gunjan
perfect, and then - hoe do I define the Icon using that compiled-in resource?
Regof
+1  A: 

As an alternative to Qt's resource system, you can use (your favorite image conversion utility) to convert the .png file to .xpm format, and then add these lines to your .cpp file:

#include "my_converted_image.xpm"
[...]
QPixmap myPixmap((const char **) my_converted_image_xpm);

...where my_converted_image_xpm is the name of the character array declared near the top of the .xpm file. This works because the .xpm image format is actually just C source code declaring a character array that is the bitmap, which QPixmap knows how to parse, e.g.:

/* XPM */
static const char * const my_converted_image_xpm[] = {
"16 16 65 1",
"       c None",
".      c #0F0F04",
[...]
"                "};
Jeremy Friesner