如何在主窗口上设置图标并使用QT进行操作 [英] how to set an icon on a Main window and action with QT

查看:152
本文介绍了如何在主窗口上设置图标并使用QT进行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

老实说,我不了解资源文件以及如何获取资源,因此我的工作无法完成,因为它向我部分解释了,我很困惑在哪里放置图标以及如何使其在程序中可见。

Honestly I do not understand resource files and how to get so that my things can get done, because it was partially explained to me and I'm quite confused where to put icon and how to make it visible on my programs.

setWindowIcon(QIcon(":/images/icon.png")); 

它不显示甚至不显示错误。

It doesn't show up or even show a error.

推荐答案

创建一个名为 resources.qrc 的资源文件:

Create a resources file named resources.qrc:

<!DOCTYPE RCC><RCC version="1.0">
<qresource>
  <file>path/to/icon.png</file>
</qresource>
</RCC>

请确保 path / to / icon.png 是实际路径,相对于包含 resources.qrc 的目录。

Make sure that path/to/icon.png is an actual path, relative to the directory that contains resources.qrc.

在您的 .pro 文件中,包括以下资源:

In your .pro file, include the resource:

TARGET = your_app
TEMPLATE = app
QT += widgets 
RESOURCES += path/to/resources.qrc

同样,请确保 path / to / resources.qrc 存在,相对于包含项目文件的目录

编译后,您的资源将嵌入到可执行文件中。可以像这样访问它:

After compiling, your resource will be embedded into your executable. It can be accessed like:

setWindowIcon(QIcon(":/path/to/icon.png"));

如果没有出现该图标,请尝试 stackoverflow问题这一个

If the icon is not appearing, try this stackoverflow question or this one.

另一种方法是使用应用程序图标。这将在桌面和开始菜单上以及 QMainWindow s和 QDialogs

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 QMainWindows and QDialogs

这篇关于如何在主窗口上设置图标并使用QT进行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆