Qt QRC资源文件-无法加载图标 [英] Qt qrc resource file - can't load icon
问题描述
我有一个Qt5桌面项目,并使用Qt Creator编辑器添加了一个"resource.qrc"文件,该文件在项目的.pro文件中创建了以下行:
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:
RESOURCES = resource.qrc
我放了一个空白前缀和一个png文件(14x14),然后尝试像这样使用它:
I put a blank prefix and a png file (14x14) and I tried to use it like this:
QPixmap pixmap = QPixmap ("://my_image.png");
ui->combobox->addItem(QIcon(pixmap), "itemname");
问题是:该图标不会显示!
The problem is: the icon won't show up!
以下作品:
QPixmap pixmap(14,14);
pixmap.fill(QColor("red"));
ui->combobox->addItem(QIcon(pixmap), "itemname");
所以问题肯定出在资源嵌入过程中..我注意到生成的"exe"里面没有资源部分...我没有静态链接的外部库,所以我认为我需要Q_INIT_RESOURCE(resource)宏(它给了我未定义的外部)
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)
更新: 我在这里发布我的qrc文件:
Update: I'm posting here my qrc file:
<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
推荐答案
@Nikos C.为您提供了有用的建议,但是我认为您的主要问题是您没有使用正确的资源链接.
@Nikos C. gives you useful advice, but I think your main problem was that you didn't use the correct link to the resource.
在您的代码中,您拥有:
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.
这篇关于Qt QRC资源文件-无法加载图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!