C++ 如何在主窗口上设置图标并使用 QT 操作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29284643/
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-28 21:10:07  来源:igfitidea点击:

how to set an icon on a Main window and action with QT

c++qt

提问by Parias Lunkamba Mukeba

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.

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

回答by Elegant Codeworks

Create a resources file named resources.qrc:

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

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

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

确保这path/to/icon.png是一个实际路径,相对于包含resources.qrc.

In your .profile, include the resource:

在您的.pro文件中,包含资源:

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

Again, make sure that path/to/resources.qrcexists, relative to the directory that contains the project file.

再次确保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"));

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

如果图标没有出现,试试这个stackoverflow questionthis 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 QMainWindows and QDialogs

另一种方法是使用应用程序图标。这将在桌面和开始菜单上以及QMainWindows 和 s 的左上角为您的应用程序设置应用程序图标QDialogs

回答by murison

Wouldn't it be the simplest to use QtCreator's Designer window? In the lower right corner you can find 3 tabs - click on the most righthand one, then click on the pen:

使用QtCreator的Designer窗口不是最简单的吗?在右下角,您可以找到 3 个选项卡 - 单击最右侧的一个,然后单击笔:

qt designer resources

qt设计师资源

This will open Resources editor:

这将打开资源编辑器:

Qt creator Resources Editor

Qt Creator 资源编辑器

Define new resources file (below the left pane), than add a "namespace" (rigth pane) and add your files. You can set aliases for them, so that when you decide to replace your icon with somethin else - you only have to switch the path, as long as alias is kept the same. You can then reference your resources via their alias to set your icon where needed, eg: setWindowIcon(QIcon(":/HurBudClientGUI/plug"));

定义新的资源文件(在左窗格下方),然后添加“命名空间”(右窗格)并添加您的文件。您可以为它们设置别名,这样当您决定用其他东西替换您的图标时 - 只要别名保持不变,您只需切换路径。然后,您可以通过它们的别名引用您的资源以在需要的地方设置您的图标,例如: setWindowIcon(QIcon(":/HurBudClientGUI/plug"));

Also - take your time to read this: http://doc.qt.io/qt-5/resources.htmland that: http://doc.qt.io/qt-5/designer-resources.html

另外 - 花点时间阅读:http: //doc.qt.io/qt-5/resources.html和:http: //doc.qt.io/qt-5/designer-resources.html

Good luck!

祝你好运!

回答by Zeinab

There is simper solution: just go to properties panel of the main window, then change the WindowIconproperty to whatever icon you like.

有一个更简单的解决方案:只需转到主窗口的属性面板,然后将WindowIcon属性更改为您喜欢的任何图标。