QResource Class
The QResource class provides an interface for reading directly from resources. More…
| Header | #include <QResource> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Types
| enum | Compression |
Public Functions
| QResource(const QString &file = QString(), const QLocale &locale = QLocale()) | |
| ~QResource() | |
| void | setFileName(const QString &file) |
| QString | fileName() const |
| QString | absoluteFilePath() const |
| void | setLocale(const QLocale &locale) |
| QLocale | locale() const |
| bool | isValid() const |
| Compression | compressionAlgorithm() const |
| qint64 | size() const |
| const uchar * | data() const |
| qint64 | uncompressedSize() const |
| QByteArray | uncompressedData() const |
| QDateTime | lastModified() const |
Static Public Members
| bool | registerResource(const QString &rccFileName, const QString &resourceRoot = QString()) |
| bool | unregisterResource(const QString &rccFileName, const QString &resourceRoot = QString()) |
| bool | registerResource(const uchar *rccData, const QString &resourceRoot = QString()) |
| bool | unregisterResource(const uchar *rccData, const QString &resourceRoot = QString()) |
Protected Functions
| bool | isDir() const |
| bool | isFile() const |
| QStringList | children() const |
Detailed Description
Resources are files compiled into the program, or into a binary .rcc file, by ntrcc. They are read through paths that start with a colon, such as :/icons/open.png, by QFile, QFileInfo, QDir and QDirIterator, so most code never uses QResource directly.
QResource gives access to the bytes of a resource without opening it, and registers and unregisters resource trees at run time:
QResource::registerResource("/usr/share/app/themes.rcc", "/themes");
QResource readme(":/themes/dark/README");
if (readme.isValid())
process(readme.data(), readme.size());
A resource can exist in several variants for different locales. QResource picks the variant of locale(): one for its language and territory, then one for its language, then one for every locale.
See also The Nt Resource System, QFile and QDir.
Differences from Qt
Compressed resources need zlib or libzstd when Nt is built. The compiled format is Nt's own and is not compatible with the output of Qt's rcc. As in Qt 6, QResource has no search paths of its own; use QDir::setSearchPaths(). A resource file is read into memory rather than mapped. The data of a resource stays valid while the QResource exists or the tree is registered. The MapPrivateOption of registerResource() does not exist.
A cross build, such as one for Zephyr, runs an ntrcc that stores resources uncompressed, as the target has neither zlib nor Zstandard. See Nt on Zephyr.
Member Type Documentation
enum QResource::Compression
The compression algorithm of a resource's data.
| Constant | Description |
|---|---|
QResource::NoCompression | The data is stored as is. |
QResource::ZlibCompression | The data is compressed with zlib. Nt never produces it. |
QResource::ZstdCompression | The data is compressed with Zstandard. Nt never produces it. |
This function was introduced in 5.13.
See also compressionAlgorithm().
Member Function Documentation
QResource::QResource(const QString &file = QString(), const QLocale &locale = QLocale())
Constructs a resource for the path file, choosing the variant for locale. The path starts with :/ or /; both name the same resource.
See also setFileName() and setLocale().
QResource::~QResource()
Destroys the resource.
QString QResource::absoluteFilePath() const
Returns the cleaned path of the resource, which starts with :/.
See also fileName().
QStringList QResource::children() const
Returns the sorted names of the entries of a directory resource, or an empty list if it is not a directory.
See also isDir().
Compression QResource::compressionAlgorithm() const
Returns the compression algorithm of the data returned by data(), or NoCompression if it is stored as is. QFile and uncompressedData() decompress the data; they fail if Nt was built without the algorithm.
This function was introduced in 5.13.
See also data() and uncompressedData().
const uchar * QResource::data() const
Returns the data of the resource, or nullptr if it is not a file. The data is not terminated by a null byte, and is compressed if compressionAlgorithm() is not NoCompression; use uncompressedData() to read it either way.
See also size() and uncompressedData().
QString QResource::fileName() const
Returns the path of the resource as it was set.
See also setFileName() and absoluteFilePath().
bool QResource::isDir() const
Returns true if the resource is a directory; otherwise returns false.
See also isFile() and children().
bool QResource::isFile() const
Returns true if the resource is a file; otherwise returns false.
See also isDir().
bool QResource::isValid() const
Returns true if the resource exists, as a file or a directory; otherwise returns false.
See also isFile() and isDir().
QDateTime QResource::lastModified() const
Returns the time the source file of the resource was last modified when it was compiled, or an invalid QDateTime if it is unknown.
This function was introduced in 5.8.
See also QFileInfo::lastModified().
QLocale QResource::locale() const
Returns the locale used to choose a variant of the resource.
See also setLocale().
bool QResource::registerResource(const QString &rccFileName, const QString &resourceRoot = QString())
Registers the binary resource file rccFileName, written by ntrcc --binary, under resourceRoot, which is empty or starts with a slash. Returns true on success; otherwise returns false, for example if the file cannot be read or is not a valid resource file.
The file is read into memory. Registering the same file and root again increments a count that unregisterResource() decrements.
See also unregisterResource().
bool QResource::registerResource(const uchar *rccData, const QString &resourceRoot = QString())
This is an overloaded function.
Registers the resource tree at rccData under resourceRoot, which is empty or starts with a slash. The data must stay valid until it is unregistered. Returns true on success; otherwise returns false.
See also unregisterResource().
void QResource::setFileName(const QString &file)
Sets the path of the resource to file.
See also fileName() and absoluteFilePath().
void QResource::setLocale(const QLocale &locale)
Sets the locale used to choose a variant of the resource to locale.
See also locale().
qint64 QResource::size() const
Returns the size of the data in bytes, compressed if the resource is compressed, or 0 if the resource is not a file.
See also data() and uncompressedSize().
QByteArray QResource::uncompressedData() const
Returns a copy of the data of the resource, decompressed if it is compressed. Returns an empty array if it is not a file, or if it cannot be decompressed: Nt was built without the algorithm, or the data is damaged.
This function was introduced in 5.15.
See also data().
qint64 QResource::uncompressedSize() const
Returns the size of the uncompressed data, which equals size() if the resource is not compressed.
This function was introduced in 5.15.
See also uncompressedData().
bool QResource::unregisterResource(const QString &rccFileName, const QString &resourceRoot = QString())
Unregisters the resource file rccFileName from resourceRoot. Returns true if it was registered; otherwise returns false.
See also registerResource().
bool QResource::unregisterResource(const uchar *rccData, const QString &resourceRoot = QString())
This is an overloaded function.
Unregisters the resource tree at rccData from resourceRoot. Returns true if it was registered; otherwise returns false.
See also registerResource().