Showing posts with label Icon. Show all posts
Showing posts with label Icon. Show all posts

Sunday, December 9, 2012

Windows application icon in Qt Creator

In an earlier post I described how to add icons to a Qt application using Qt Creator. With the procedure described there, the application shows the icon in the window title bar and on the taskbar. However, when you browse to the folder containing the application in windows, the executable will not be shown with the icon. The same is true when you create a shortcut to the executable (windows will also say that the executable does not contain an icon).

As described here, there is a simple windows specific method to add an application icon that will change the appearance of the executable when browsing under windows (and when creating a shortcut to the executable). You must first create a windows specific resource file which has the extension .rc e.g. myapp.rc. This simple text file then needs to contain the (single) line:

IDI_ICON1 ICON DISCARDABLE "images/myapp.ico"

The final step is then to add this resource file in the Qt project (.pro) file:

RC_FILE = myapp.rc

This works for me when I put the myapp.rc in the same folder as where the Qt project file is located (and that contains the images folder). I did not try any other file locations.

Friday, March 2, 2012

adding icons to an application with Qt Creator

The book "C++ GUI Programming with Qt 4" gives a short description of how to use a resource file to include images into the executable which can then (for example) be used as icons. The example given is clear by itself but when using the latest QtCreator version (2.4.1) things turn out to work just a little bit different. Furthermore a resource file as given in the book does not load correctly in QtCreator.

To add image resources I now do this:
1. Create a new resource file by clicking File -> New File or Project and then Choose Qt resource file.
2. Add a prefix for example /icons
3. Add your images files for example: images/icon.png (with a images a subdirectory in your project folder
4. Refer to these resources in your code as ":/icons/images/icon.png"

The changed point compared to the resource file example in the book is the prefix. You now have to add a prefix which (probably) did not exist in the past. In the example application (called "application") that comes with QtCreator the prefix that is used is simply "/".

Adding the icon to a widget (e.g. your main window) is now simple:

setWindowIcon(QIcon(":/icons/images/icon.png"));

And as requested some screen shots.

Step 1, creating a new resource file:


Step 2, add a prefix:


Simply type over the default prefix label generator by QtCreator. Your prefix will appear in the main window:



Step 3, add image files (after selecting the prefix in the main window):


The final result then looks something like this: