How to set application icon in a Qt-based project?

C++Qt

C++ Problem Overview


How do you set application icon for application made using Qt? Is there some easy way? It's a qmake-based project.

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

Verified in Linux (Qt 4.8.6) and Windows (Qt 5.6):

  1. Add the icon file to your project folder;

  2. In the main function call setWindowIcon() method. For example:

    QApplication a(argc, argv); a.setWindowIcon(QIcon("./images/icon.png"));

Solution 3 - C++

To extend Rob's answer, you can set an application icon for macOS by adding and modifying the following line onto your .pro file.

macx: ICON = <app_icon>.icns

Note that the ICON qmake variable is only meant to target macOS.

For Windows, use

  • RC_ICONS = <app_icon>.ico if you're attaching a .ico file
  • or RC_FILE = <app_icon>.rc if you want to attach your icon through a .rc file. (Be sure to add IDI_ICON1 ICON DISCARDABLE "myappico.ico" into the rc file. Indentation not mine.)

For further reading, see Setting the Application Icon.

Solution 4 - C++

For Qt5 on Windows, move your ico file to project folder and add this line to your .pro file:

RC_ICONS = myappico.ico

Offical link: https://doc.qt.io/qt-5/appicon.html

Solution 5 - C++

Now that Qt has upgraded to 5.0.1, there is new method to add an application icon. First, you need prepare a resource file, named as .qrc

  1. Without Qt Designer, I assume there is a QMainWindow instance whose name is MainWin. You can use:

    QIcon icon(":icon/app.icon"); MainWin.setWindowIcon(icon);

  2. With Qt Designer, you can modify the property of QMainWindow. Choose icon resource from .qrc and insert it into the row of windowIcon.

The above method can be used in Qt4.7, Qt4.8.x.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPavelsView Question on Stackoverflow
Solution 1 - C++RobView Answer on Stackoverflow
Solution 2 - C++korishView Answer on Stackoverflow
Solution 3 - C++TrebledJView Answer on Stackoverflow
Solution 4 - C++quxView Answer on Stackoverflow
Solution 5 - C++Tody.LuView Answer on Stackoverflow