How to set application icon in a Qt-based project?
For Qt 5, this process is automated by qmake. Just add the following to the project file:
win32:RC_ICONS += your_icon.ico
The automated resource file generation also uses the values of the following qmake variables: VERSION, QMAKE_TARGET_COMPANY, QMAKE_TARGET_DESCRIPTION, QMAKE_TARGET_COPYRIGHT, QMAKE_TARGET_PRODUCT, RC_LANG, RC_CODEPAGE
.
For Qt 4, you need to do it manually. On Windows, you need to create a .rc file and add it to your project (.pro). The RC file should look like this:
IDI_ICON1 ICON DISCARDABLE "path_to_you_icon.ico"
The .pro entry should also be win32 specific, e.g.:
win32:RC_FILE += MyApplication.rc
HowTo set Icon to Qt Application, created with Qt Visual Studio Add-in?
Totally, I have found the correct solution, here it is:
Preamble: You need Visual Studio Add-in already installed!
Open Visual Studio (or Visual C++)
Create new project from menu: File | New | Project... and select project type as Qt4 Projects and for example Qt Application from templates.
When the first dialog window will be shown, just click Next > button twice.
And finally in Generated Class tap the Add default application icon (Windows only) checkbox.
alt text http://www.freeimagehosting.net/uploads/7f87801fb9.png
To finish process, just press Finish.
Project will create a default icon, with name {your_project_name}.ico. Replace it and have fun!
How to set/change the application icon?
You have done the right thing to set the main window icon. Just setting windowIcon
property in the designer will set an icon for the application window.
For setting an icon to the application executable file there is an automated process In Qt 5.
You can just add the following to the .pro file:
win32: RC_ICONS = myIcon.ico
Also store the .ico file in your application's source code directory.
Note that this is only for Windows. There are other ways to set application icon in Linux and Mac.
Set icon for Qt application built in Visual Studio
I don't know whether this is the Qt-offical way of doing things but this is what works for me:
- Create a
*.ico
file of whatever you want to use as a logo. I called minefavicon.ico
. - Create a file named
yourapplication.rc
. - Add the following line to
yourapplication.rc
:IDI_ICON1 ICON DISCARDABLE "favicon.ico"
. - Add the
*.rc
file to your buildsystem as a source file. - Make sure that your
*.exe
ships along side thefavicon.ico
file.
Edit: Official Qt5 documentation on this: https://doc.qt.io/qt-5/appicon.html
how to set an icon on a Main window and action with QT
Create a resources file named resources.qrc
:
<!DOCTYPE RCC>
<RCC version="1.0">
<qresource>
<file>path/to/icon.png</file>
</qresource>
</RCC>
Make sure that path/to/icon.png
is an actual path, relative to the directory that contains resources.qrc
.
In your .pro
file, include the resource:
TARGET = your_app
TEMPLATE = app
QT += widgets
RESOURCES += path/to/resources.qrc
Again, make sure that path/to/resources.qrc
exists, relative to the directory that contains the project file.
After compiling, your resource will be embedded into your executable. It can be accessed like:
setWindowIcon(QIcon(":/path/to/icon.png"));
If the icon is not appearing, try this stackoverflow question or this one.
Another approach would be to use the Application Icon. This will set the application icon for your application on the desktop and start menus, and also on the top left corner of QMainWindow
s and QDialogs
Set .exe icon for Qt windows app without qmake/VS
Yes, you can. Instead of windres.exe
, the Windows SDK provides rc.exe
, a.k.a. the Resource Compiler. You need first an image in .ico format (named here "app.ico") and a text file named "app.rc" with this content:
IDI_ICON1 ICON DISCARDABLE "app.ico"
You can compile this file with this command:
rc app.rc
That command will produce a file with the name "app.res". This RES file can be processed by the linker directly, or you may convert it to .obj with this command:
cvtres /MACHINE:X64 app.res
That command produces a file with the name "app.obj" (for architecture x86_64, use cvtres /h
to see other parameters). You need to give this .obj file to the linker when producing the executable. "cvtres.exe" comes from the VC compiler bin directory, and not the Windows SDK.
Define a window icon for a QML application
For me it only worked when using a PNG instead of an ICO file.
Also you might want to test it with a full path:
app.setWindowIcon(QIcon("C:/path_to_ico/favicon.png"));
Or directly - if it resides in your working dir:
app.setWindowIcon(QIcon("favicon.png"));
As soon as this works you can try to use a relative path or resource access again :-)
Related Topics
Is C++ Considered Weakly Typed? Why
C++ Frontend Only Compiler (Convert C++ to C)
Global Variable "Count" Ambiguous
Warning C4003 and Errors C2589 and C2059 On: X = Std::Numeric_Limits<Int>::Max();
Qt 5.5 Embed External Application into Qwidget
Weird Undefined Symbols of Static Constants Inside a Struct/Class
Is Begin() == End() for Any Empty() Vector
Aliasing Struct and Array the C++ Way
C++11 Variadic Printf Performance
Qthread Emits Finished() Signal But Isrunning() Returns True and Isfinished() Returns False
Best Practices for Use of C++ Header Files
What Is the Purpose of C++20 Std::Common_Reference
Node Packages Not Building on Windows 8.1 - Missing Microsoft.Cpp.Default.Props
Is It Ever Not Safe to Throw an Exception in a Constructor