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
!
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
!
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
:
) tofopen
?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 usingfopen
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.