Reputation: 751
Introduction
I need to set a icon, both for .exe file, in top-left corner of my app window, and on the task bar (Windows 10). However it works only partially - I'm able to set .exe icon, but that's it. In the other two places I have default icon. I'm working with QT 5.9.7 inside Visual Studio 2017.
What I tested so far
I have following line in my .rc
file:
IDI_ICON1 ICON "favicon.ico"
Which lets me to have .exe icon. But it seems to not affect the rest of framework. I was trying to set in-app icon this way (main.cpp
):
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.setWindowIcon(QIcon(":/favicon.ico"));
// a.setWindowIcon also doesn't work.
return a.exec();
}
or even inside mainwindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
setWindowIcon(QIcon(":/favicon.ico"));
}
However it seems to be ignored - it has no effect, also it doesn't give me any error. I also tried to include imageformats/qico.dll
folllowing THIS instruction - still no luck.
The Question
So - how to set this two other icons correctly - on Windows task bar, and in the corner of my application window?
EDIT:
Adding qrc
file also doesn't fix the problem.
Upvotes: 0
Views: 249
Reputation: 1
Perhaps I solved this problem. Was the .rc file created manually, or automatically when adding a resource? If the latter, then Visual studio generates the resource.h file. In this file you need to comment out the line
#define IDI_ICON1 101
then everything works.
Upvotes: 0
Reputation: 751
I have found the solution, so I will post it here, in the case someone else will encounter the same problem:
First of all - the thing has nothing to do with .rc
or .qrc
file, the icon can be even loaded directly from app directory. The problem is in the supported formats - for some reason, under Windows 10 there is an issue with .ico
format in QT framework. Theoretically addition of imageformats/qico.dll
should fix the issue, but it doesn't. The solution is to use other format (in my case .png
), and place it directly where the .exe
file is, not in any subdirectory. Then you can call it like this:
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
a.setWindowIcon(QIcon("favicon.png"));
return a.exec();
}
Alternatively, if you want to have icon compiled in binary form, you can try to add it to resources, however the format still mustn't be .ico
to make it works, as this is the origin of the issue. The size of the icon is adjusted correctly, so it look good both in the corner of app window, and on the task bar - therefore .png
is fairly enough in this case.
Upvotes: 1
Reputation: 6329
In case you are not using Creator / qmake, you need to follow the steps from the Qt Documentation:
If you do not use qmake, the necessary steps are: first, create an .rc file and run the rc or windres program on the .rc file, then link your application with the resulting .res file.
That will create the icon for the Windows Explorer / Taskbar.
The top left icon is created by adding it to the qrc file and compiling it with the resource compiler. This icon needs to be set by calling app.setWindowIcon(QIcon(":/favicon.ico"));
where app is your QApplication-instance.
Upvotes: 1