Regof
Regof

Reputation: 2857

How do I define an Icon in QT with a compile time predefined image?

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?

Upvotes: 0

Views: 2760

Answers (3)

yungtrizzle
yungtrizzle

Reputation: 83

If your using Qt-Creator, there's a resource editor available for you to use. Simply create a new resource file, add a prefix and add files. Qt-Creator will automatically add it into the project file. After that, select your image from your resource and your good to go

Upvotes: 0

Gunjan
Gunjan

Reputation: 1237

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

Upvotes: 8

Jeremy Friesner
Jeremy Friesner

Reputation: 73081

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",
[...]
"                "};

Upvotes: 7

Related Questions