PDA

View Full Version : setWindowIcon() with ICO format



cic
3rd July 2013, 07:40
Hello all,

No idea whether somebody has encountered similar problem.
I am setting the windows icon(on the top-left side of the application window) with QApplication::setWindowIcon(const QIcon&)
The icon file which is used is ICO format under windows system.

before I imported QT, the Windows function i used could correctly decide which image in the .ico file should be used (under proper resolution).



hAppIcon = LoadIcon(Instance(), ":icons/app.ico");


I was expecting QIcon also do the right job, but it failed.



QApplication::setWindowIcon(QIcon(QPixmap(":icons/app.ico")));


seems that QIcon only reads the first image from app.ico and displays it on the window.
e.g. the first image from app.ico is with size 48*48 but actually for the window the one with size 16*16 should be used.

by reading the documentation of QPixmap, it doesnt support .ico format. On the other hand, there are lots of sample codes on the internet in which .ico are used in QPixmap.
It confused me so much. any ideas?

sonulohani
3rd July 2013, 14:03
Better use this tutorial. It will definitely work. http://qt-project.org/doc/qt-4.8/appicon.html

cic
3rd July 2013, 16:04
hello @sonulohani,

Ive read this tutorial already. However, that actually only explains how to set up the application icon(the icon which people see on the explorer)
which on contrary i want is window icon.

ChrisW67
4th July 2013, 00:28
There is an ICO format image format plugin in Qt 4.8.4. I don't know exactly when it was added, but it has been around for quite a while. The code in that plugin is quite capable of loading the multiple images in ICO file but defaults to the first for non-animated formats. You can use the QImageReader directly to find the image you want. So, for this icon:


$ identify qtcreator.ico
qtcreator.ico[0] ICO 256x256 256x256+0+0 32-bit DirectClass 288KB 0.000u 0:00.009
qtcreator.ico[1] ICO 48x48 48x48+0+0 32-bit DirectClass 288KB 0.000u 0:00.000
qtcreator.ico[2] ICO 32x32 32x32+0+0 32-bit DirectClass 288KB 0.000u 0:00.000
qtcreator.ico[3] ICO 24x24 24x24+0+0 32-bit DirectClass 288KB 0.000u 0:00.000
qtcreator.ico[4] ICO 16x16 16x16+0+0 32-bit DirectClass 288KB 0.000u 0:00.000

This selects the 16x16 icon (and the first image if 16x16 is not matched):


#include <QtGui>
#include <QDebug>

int main(int argc, char **argv)
{
QApplication app(argc, argv);

QImageReader reader("qtcreator.ico");
QImage result = reader.read(); // this acts as a default if the size is not matched
for (int i = 0; i < reader.imageCount(); ++i) {
reader.jumpToImage(i);
QImage image = reader.read();
if (image.size() == QSize(16, 16)) {
result = image;
break;
}
}

QWidget w;
w.setWindowIcon(QPixmap::fromImage(result));
w.show();

return app.exec();
}