简体   繁体   English

如何在Qt的窗口标题栏中包含图标?

[英]How to include an icon in the title bar of window in Qt?

I want to be able to replace the existing standard icon with my own but I am unsure how to do so. 我希望能够用自己的图标替换现有的标准图标,但是我不确定该怎么做。 I only started Qt one week ago so I don't know much about the application. 我一周前才开始使用Qt,所以对应用程序了解不多。 In addition to this I looked at the Qt website but their method for adding an icon did not work on my computer. 除此之外,我查看了Qt网站,但是他们添加图标的方法在我的计算机上不起作用。

Besides using a RC file (Windows) you may specify the icon directly using qmake . 除了使用RC文件(Windows)外,您还可以直接使用qmake指定图标。

win32: RC_ICONS = icon.ico

Make sure it detects the path correctly. 确保它正确检测到路径。 In the provided case the icon resides in the project root directory right beside the project file. 在所提供的情况下,图标位于项目文件旁边的项目根目录中。


For window icons you typically, if application wide applicable, use some method of QApplication . 对于窗口图标,通常,如果适用于整个应用程序,请使用QApplication某些方法。

...
QApplication app(argc, argv);
...
app.setWindowIcon(QIcon(":/icon");
...

which is consuming the resource file using the alias icon . 使用别名图标消耗资源文件。 Here is an example ( res.qrc ): 这是一个示例( res.qrc ):

<RCC>
    <qresource prefix="/">
        <file alias="icon">icon.png</file>
    </qresource>
</RCC>

More information on resources are here . 有关资源的更多信息,请参见此处

也许尝试在QWidget或QApplication上使用setWindowIcon函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM