1

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!

4

1 回答 1

6

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.

于 2012-06-22T11:27:32.817 回答