Qt Image from resource file

我怕爱的太早我们不能终老 提交于 2019-12-01 17:24:12

问题


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>

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?


回答1:


All this will work if your png files are located in the same folder as .pro, .qrc, and .cpp files of your project.

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>

And in .cpp file:

QImage *green = new QImage(":/Resources/green.png");



回答2:


Did you remember to run qmake after adding the resource file?



来源:https://stackoverflow.com/questions/31406038/qt-image-from-resource-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!