[Interest] Binary file embedded in Resources to std::istream

Rollastre Prostrit rollastre at gmail.com
Fri Aug 2 21:18:29 CEST 2013

Hi Qters:

Due to vicissitudes of a third party library I'm using I have to read 
the bytes of png files (binary files) embedded in the resources via 
using a std::istream. I have been struggling quite a bit with this and 
googling a lot without success. Maybe this is something that it is easy 
to solve but I haven't succeeded with it yet.

I use this snippet

     QFile f(":/images/file.png");

     if (!f.exist())

     if (f.size()<=0)

    if (!f.open(QIODevice::ReadOnly))

     if (!f.isReadable())

     QByteArray bytes = f.readAll();

     char* bb = bytes.data();
     std::string str;
     for (int i=0; i<s; ++i)
         str += bb[i];

     stringstream stream;
     stream << str;

I use strings here because some of the tens of articles I've read 
suggested it. It doesn't seem to work, though. If I dump the bytes to a 
temp file, I see the file becomes corrupted. There is an extra empty 
byte right after the PNG header. My guess is that the zero-end byte is 
messing everything up. But I admit that I don't find any solution for this.

Has somebody faced this problem and/or can see what/how is the solution?
Thanks in advance!

More information about the Interest mailing list