C++ Qt qrc 资源文件 - 无法加载图标

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

Qt qrc resource file - can't load icon

c++qticonsqt5qicon

提问by Johnny Pauling

I have a Qt5 desktop project and I added a "resource.qrc" file with the Qt Creator editor which created the following line into the project's .pro file:

我有一个 Qt5 桌面项目,我使用 Qt Creator 编辑器添加了一个“resource.qrc”文件,该文件在项目的 .pro 文件中创建了以下行:

RESOURCES = resource.qrc

I put a blank prefix and a png file (14x14) and I tried to use it like this:

我放了一个空白前缀和一个 png 文件(14x14),我尝试像这样使用它:

QPixmap pixmap = QPixmap ("://my_image.png");
ui->combobox->addItem(QIcon(pixmap), "itemname");

The problem is: the icon won't show up!

问题是:图标不会显示!

The following works:

以下工作:

QPixmap pixmap(14,14);
pixmap.fill(QColor("red"));
ui->combobox->addItem(QIcon(pixmap), "itemname");

so the problem must be in the resource embedding process.. I noticed that the generated "exe" hasn't a resource section inside it... I don't have static linked external libraries, so I don't think I need the Q_INIT_RESOURCE(resource) macro (it gives me undefined external)

所以问题一定出在资源嵌入过程中..我注意到生成的“exe”里面没有资源部分......我没有静态链接的外部库,所以我认为我不需要Q_INIT_RESOURCE(resource) 宏(它给了我未定义的外部)

Update: I'm posting here my qrc file:

更新:我在这里发布我的 qrc 文件:

<RCC>
    <qresource prefix="/">
        <file>my_image.png</file>
    </qresource>
</RCC>

it's pretty simple and I don't understand why at runtime icons are not showing up

这很简单,我不明白为什么在运行时图标没有显示

采纳答案by troyane

@Nikos C. gives you useful advice, but I think your main problem was that you didn't use the correct link to the resource.

@Nikos C. 为您提供了有用的建议,但我认为您的主要问题是您没有使用正确的资源链接。

In your code, you have:

在您的代码中,您有:

QPixmap pixmap = QPixmap ("://my_image.png");

but, according to the documentation, it should be

但是,根据文档,它应该是

QPixmap pixmap = QPixmap (":/my_image.png");

or you can give aliases to your resources, and use those instead.

或者您可以为您的资源指定别名,然后使用这些别名。

回答by Terrabits

I had this same issue recently, where I malformed the resource string. If you are using a current version of Qt Creator, you can open your .qrc file for edit and then right-click the resource (in this case image) that you are trying to address, then click "Copy Resource Path to Clipboard". And voila, you have the correct resource string every time.

我最近遇到了同样的问题,我的资源字符串格式错误。如果您使用的是当前版本的 Qt Creator,您可以打开 .qrc 文件进行编辑,然后右键单击您尝试寻址的资源(在本例中为图像),然后单击“将资源路径复制到剪贴板”。瞧,您每次都拥有正确的资源字符串。

Qt Creator is awesome.

Qt Creator 很棒。

Hope this helps!

希望这可以帮助!

回答by Raj

Issue is solved is use rcc.exe C:\root\QT>c:\root\QT\4.7.4\bin\rcc.exe Headless.qrc -o qtresources.cpp During compilation you should have images in the path. Create the qtresources.cpp file include this file in makefile or project. You should able to see the image.

问题已解决,使用 rcc.exe C:\root\QT>c:\root\QT\4.7.4\bin\rcc.exe Headless.qrc -o qtresources.cpp 在编译期间,路径中应该有图像。创建 qtresources.cpp 文件,将此文件包含在 makefile 或项目中。您应该能够看到图像。