Question

Is it possible to map a resource file in Qt?

For example:

QFile file(resource_name);
file.open(QIODevice::ReadOnly);

uchar* ptr = file.map(0, file.size());

When I try this, ptr == 0, indicating an error.
It works fine if I try to map a regular file.

I am running Qt on linux, which supports QFile::Map.

Was it helpful?

Solution

Yes, it is possible. There is one thing to keep in mind though. By default the qt resource compiler rcc compresses the resources.

The file.size() call will return the actual, un-compressed size of the original file. However, the embedded resource is compressed and is most likely a different size. The file.map(0, file.size()) returns an error since the size passed to map() is larger than the resource being mapped. Even if you pass the correct size to map(), or a smaller size, the memory will contain the compressed data, not the un-compressed data.

The solution is to not compress the embedded resource. This can be accomplished by adding:

QMAKE_RESOURCE_FLAGS += -no-compress  

to your qt project file. See here for explanation of QMAKE_RESOURCE_FLAGS.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top