C++ 如何在基于 Qt 的项目中设置应用程序图标?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1460361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:03:49  来源:igfitidea点击:

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

c++qt

提问by Pavels

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

如何为使用 Qt 制作的应用程序设置应用程序图标?有什么简单的方法吗?这是一个基于 qmake 的项目。

回答by Rob

For Qt 5, this process is automated by qmake. Just add the following to the project file:

对于Qt 5,这个过程由 qmake 自动化。只需将以下内容添加到项目文件中:

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.

自动资源文件生成还使用以下 qmake 变量的值: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:

对于Qt 4,您需要手动完成。在 Windows 上,您需要创建一个 .rc 文件并将其添加到您的项目 (.pro) 中。RC 文件应如下所示:

IDI_ICON1 ICON DISCARDABLE "path_to_you_icon.ico"

The .pro entry should also be win32 specific, e.g.:

.pro 条目也应该是特定于 win32 的,例如:

win32:RC_FILE += MyApplication.rc

回答by korish

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

在 Linux (Qt 4.8.6) 和 Windows (Qt 5.6) 中验证:

1) Add the icon file to your project folder;

1) 将图标文件添加到您的项目文件夹中;

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

2) 在主函数中调用 setWindowIcon() 方法。例如:

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

回答by TrebledJ

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

为了扩展 Rob 的答案,您可以通过在.pro文件中添加和修改以下行来为 macOS 设置应用程序图标。

macx: ICON = <app_icon>.icns

Note that the ICONqmake variableis only meant to target macOS.

请注意,ICONqmake 变量仅适用于 macOS。

For Windows, use

对于 Windows,请使用

  • RC_ICONS = <app_icon>.icoif you're attaching a .icofile
  • orRC_FILE = <app_icon>.rcif you want to attach your icon through a .rcfile. (Be sure to add IDI_ICON1 ICON DISCARDABLE "myappico.ico"into the rc file. Indentation not mine.)
  • RC_ICONS = <app_icon>.ico如果您要附加.ico文件
  • 或者RC_FILE = <app_icon>.rc如果您想通过.rc文件附加您的图标。(一定要添加IDI_ICON1 ICON DISCARDABLE "myappico.ico"到 rc 文件中。缩进不是我的。)

For further reading, see Setting the Application Icon.

如需进一步阅读,请参阅设置应用程序图标

回答by Tody.Lu

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

现在 Qt 已经升级到 5.0.1,有新的方法来添加应用程序图标。首先,你需要准备一个资源文件,命名为.qrc

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

1) 没有Qt Designer,我假设有一个QMainWindow名为的实例MainWin。您可以使用:

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

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

2) 使用Qt Designer,可以修改 的属性QMainWindow。从中选择图标资源.qrc并将其插入到windowIcon.

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

以上方法可以在Qt4.7、Qt4.8.x中使用。