来自资源文件的 Qt 图像 [英] Qt Image from resource file
问题描述
我正在尝试通过资源文件将图像插入到我的程序中,如下所示:
I'm trying to insert an image to my program via resource file, which is like:
<RCC>
<qresource prefix="/">
<file>green.png</file>
<file>other files</file>
</qresource>
</RCC>
当我尝试使用 QImage 或 QPixmap 加载它时,例如:
and when I'm trying to load it using QImage or QPixmap, like:
QImage *green = new QImage(":/green.png");
if(green->isNull()) qDebug("null");
我总是看到那个空消息,表明我做错了什么.一种解决方案可能是使用绝对路径,如
I always see that null message, indicating that I'm doing something wrong. One solution may be using absolute path like
"C:\\Users\\user\\Documents\\project\\green.png",
这当然有效,但我更喜欢使用资源文件来实现它.你能给我一些建议吗?
which works of course, but I'd prefer implement it using resource file. Could you please give me some advice?
推荐答案
如果您的 png 文件与项目的 .pro、.qrc 和 .cpp 文件位于同一文件夹中,所有这些都将起作用.
All this will work if your png files are located in the same folder as .pro, .qrc, and .cpp files of your project.
例如,通常将所有图像放在特殊的子文件夹 Resources 中会很方便.然后在 .qrc 中,该行将如下所示:
Usually it is convenient to put all images to special subfolder Resources, for example. Then in .qrc the line will look like:
<file>Resources/green.png</file>
在 .cpp 文件中:
And in .cpp file:
QImage *green = new QImage(":/Resources/green.png");
这篇关于来自资源文件的 Qt 图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!