QResource Class
The QResource class provides an interface for reading directly from resources. More...
| Header: | #include <QResource> |
| qmake: | QT += core |
| Since: | Qt 4.2 |
This class was introduced in Qt 4.2.
Note: All functions in this class are reentrant.
Public Types
| enum | Compression { NoCompression, ZlibCompression, ZstdCompression } |
Detailed Description
QResource is an object that represents a set of data (and possibly children) relating to a single resource entity. QResource gives direct access to the bytes in their raw format. In this way direct access allows reading data without buffer copying or indirection. Indirection is often useful when interacting with the resource entity as if it is a file, this can be achieved with QFile. The data and children behind a QResource are normally compiled into an application/library, but it is also possible to load a resource at runtime. When loaded at run time the resource file will be loaded as one big set of data and then given out in pieces via references into the resource tree.
A QResource can either be loaded with an absolute path, either treated as a file system rooted with a / character, or in resource notation rooted with a : character. A relative resource can also be opened which will be found in the list of paths returned by QDir::searchPaths().
A QResource that is representing a file will have data backing it, this data can possibly be compressed, in which case qUncompress() must be used to access the real data; this happens implicitly when accessed through a QFile. A QResource that is representing a directory will have only children and no data.
Dynamic Resource Loading
A resource can be left out of an application's binary and loaded when it is needed at run-time by using the registerResource() function. The resource file passed into registerResource() must be a binary resource as created by rcc. Further information about binary resources can be found in The Qt Resource System documentation.
This can often be useful when loading a large set of application icons that may change based on a setting, or that can be edited by a user and later recreated. The resource is immediately loaded into memory, either as a result of a single file read operation, or as a memory mapped file.
This approach can prove to be a significant performance gain as only a single file will be loaded, and pieces of data will be given out via the path requested in setFileName().
The unregisterResource() function removes a reference to a particular file. If there are QResource objects that currently reference resources related to the unregistered file, they will continue to be valid but the resource file itself will be removed from the resource roots, and thus no further QResource can be created pointing into this resource data. The resource itself will be unmapped from memory when the last QResource that points to it is destroyed.
See also The Qt Resource System, QFile, QDir, and QFileInfo.
Member Type Documentation
enum QResource::Compression
This enum is used by compressionAlgorithm() to indicate which algorithm the RCC tool used to compress the payload.
| Constant | Value | Description |
|---|---|---|
QResource::NoCompression | 0 | Contents are not compressed |
QResource::ZlibCompression | 1 | Contents are compressed using zlib and can be decompressed using the qUncompress() function. |
QResource::ZstdCompression | 2 | Contents are compressed using zstd. To decompress, use the ZSTD_decompress function from the zstd library. |
This enum was introduced or modified in Qt 5.13.
See also compressionAlgorithm().