How to access resource image with fopen?

不羁岁月 提交于 2019-12-20 07:28:06

问题


I use fopen(filename.c_str(), "rb"); in Qt with images resources.

But fopen wants a full path. It doesn't work if my filename is :/images/img.png!


回答1:


fopen is not part of Qt. Qt's resource system only works with Qt classes. See the Qt documentation of Using Resources in the Application for details.

The Qt classes which work for resources first check if the given filename starts with the resource marker (:) and if so, they look up the resource internally (no physical file is opened!), otherwise, they open the physical file. Note that resources are included in the source code of your application (as a simple array of bytes) during compile time (using the Qt resource compiler) and read using a special resource reader in Qt internally, which can treat the resource file as it would be a physical file, but it still isn't.

What happens if you pass a resource file path (starting with :) to fopen? fopen tries to read a physical directory called : and searches in this directory for the subpath you provided. Of course, no such directory exists on your harddrive, since the file is part of your executable file. (And please don't try to read the resource file manually from within the executable by opening the executable using fopen and seeking to the resource!)

In your concrete situation: Are you trying to read the image using an external image reader? Qt has its own PNG reader. Just call the QImage(QString filename) constructor.



来源:https://stackoverflow.com/questions/11154118/how-to-access-resource-image-with-fopen

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