Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
QResource Class Reference

\inmodule QtCore More...

#include <qresource.h>

+ Collaboration diagram for QResource:

Public Types

enum  Compression { NoCompression , ZlibCompression , ZstdCompression }
 

Public Member Functions

 QResource (const QString &file=QString(), const QLocale &locale=QLocale())
 Constructs a QResource pointing to file.
 
 ~QResource ()
 Releases the resources of the QResource object.
 
void setFileName (const QString &file)
 Sets a QResource to point to file.
 
QString fileName () const
 Returns the full path to the file that this QResource represents as it was passed.
 
QString absoluteFilePath () const
 Returns the real path that this QResource represents, if the resource was found via the QDir::searchPaths() it will be indicated in the path.
 
void setLocale (const QLocale &locale)
 Sets a QResource to only load the localization of resource to for locale.
 
QLocale locale () const
 Returns the locale used to locate the data for the QResource.
 
bool isValid () const
 Returns true if the resource really exists in the resource hierarchy, false otherwise.
 
Compression compressionAlgorithm () const
 
qint64 size () const
 Returns the size of the stored data backing the resource.
 
const uchardata () const
 Returns direct access to a segment of read-only data, that this resource represents.
 
qint64 uncompressedSize () const
 
QByteArray uncompressedData () const
 
QDateTime lastModified () const
 

Static Public Member Functions

static bool registerResource (const QString &rccFilename, const QString &resourceRoot=QString())
 Registers the resource with the given rccFileName at the location in the resource tree specified by mapRoot, and returns true if the file is successfully opened; otherwise returns false.
 
static bool unregisterResource (const QString &rccFilename, const QString &resourceRoot=QString())
 Unregisters the resource with the given rccFileName at the location in the resource tree specified by mapRoot, and returns true if the resource is successfully unloaded and no references exist for the resource; otherwise returns false.
 
static bool registerResource (const uchar *rccData, const QString &resourceRoot=QString())
 
static bool unregisterResource (const uchar *rccData, const QString &resourceRoot=QString())
 

Protected Member Functions

bool isDir () const
 Returns true if the resource represents a directory and thus may have children() in it, false if it represents a file.
 
bool isFile () const
 Returns true if the resource represents a file and thus has data backing it, false if it represents a directory.
 
QStringList children () const
 Returns a list of all resources in this directory, if the resource represents a file the list will be empty.
 

Protected Attributes

QScopedPointer< QResourcePrivated_ptr
 

Friends

class QResourceFileEngine
 
class QResourceFileEngineIterator
 

Detailed Description

\inmodule QtCore

The QResource class provides an interface for reading directly from resources.

\reentrant

Since
4.2

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.

Definition at line 19 of file qresource.h.

Member Enumeration Documentation

◆ Compression

Since
5.13

This enum is used by compressionAlgorithm() to indicate which algorithm the RCC tool used to compress the payload.

\value NoCompression Contents are not compressed \value ZlibCompression Contents are compressed using \l{https://zlib.net}{zlib} and can be decompressed using the qUncompress() function. \value ZstdCompression Contents are compressed using \l{Zstandard Site}{zstd}. To decompress, use the {ZSTD_decompress} function from the zstd library.

See also
compressionAlgorithm()
Enumerator
NoCompression 
ZlibCompression 
ZstdCompression 

Definition at line 22 of file qresource.h.

Constructor & Destructor Documentation

◆ QResource()

QResource::QResource ( const QString & file = QString(),
const QLocale & locale = QLocale() )

Constructs a QResource pointing to file.

locale is used to load a specific localization of a resource data.

See also
QFileInfo, QDir::searchPaths(), setFileName(), setLocale()

Definition at line 507 of file qresource.cpp.

References d, file, and locale().

+ Here is the call graph for this function:

◆ ~QResource()

QResource::~QResource ( )

Releases the resources of the QResource object.

Definition at line 517 of file qresource.cpp.

Member Function Documentation

◆ absoluteFilePath()

QString QResource::absoluteFilePath ( ) const

Returns the real path that this QResource represents, if the resource was found via the QDir::searchPaths() it will be indicated in the path.

See also
fileName()

Definition at line 582 of file qresource.cpp.

References d.

◆ children()

QStringList QResource::children ( ) const
protected

Returns a list of all resources in this directory, if the resource represents a file the list will be empty.

See also
isDir()

Definition at line 755 of file qresource.cpp.

References d.

Referenced by QResourceFileEngineIterator::advance().

+ Here is the caller graph for this function:

◆ compressionAlgorithm()

QResource::Compression QResource::compressionAlgorithm ( ) const
Since
5.13

Returns the compression type that this resource is compressed with, if any. If it is not compressed, this function returns QResource::NoCompression.

If this function returns QResource::ZlibCompression, you may decompress the data using the qUncompress() function. Up until Qt 5.13, this was the only possible compression algorithm.

If this function returns QResource::ZstdCompression, you need to use the Zstandard library functions ({<zstd.h>} header). Qt does not provide a wrapper.

See \l{http://facebook.github.io/zstd/zstd_manual.html}{Zstandard manual}.

See also
data(), isFile()

Definition at line 629 of file qresource.cpp.

References d.

◆ data()

const uchar * QResource::data ( ) const

Returns direct access to a segment of read-only data, that this resource represents.

If the resource is compressed, the data returned is also compressed. The caller must then decompress the data or use uncompressedData(). If the resource is a directory, nullptr is returned.

See also
uncompressedData(), size(), isFile()

Definition at line 678 of file qresource.cpp.

References d.

◆ fileName()

QString QResource::fileName ( ) const

Returns the full path to the file that this QResource represents as it was passed.

See also
absoluteFilePath()

Definition at line 568 of file qresource.cpp.

References d.

◆ isDir()

bool QResource::isDir ( ) const
protected

Returns true if the resource represents a directory and thus may have children() in it, false if it represents a file.

See also
isFile()

Definition at line 741 of file qresource.cpp.

References d.

◆ isFile()

bool QResource::isFile ( ) const
inlineprotected

Returns true if the resource represents a file and thus has data backing it, false if it represents a directory.

See also
isDir()

Definition at line 57 of file qresource.h.

◆ isValid()

bool QResource::isValid ( ) const

Returns true if the resource really exists in the resource hierarchy, false otherwise.

Definition at line 595 of file qresource.cpp.

References d.

Referenced by QResourceFileEngineIterator::advance().

+ Here is the caller graph for this function:

◆ lastModified()

QDateTime QResource::lastModified ( ) const
Since
5.8

Returns the date and time when the file was last modified before packaging into a resource.

Definition at line 727 of file qresource.cpp.

References d, and QDateTime::fromMSecsSinceEpoch().

+ Here is the call graph for this function:

◆ locale()

QLocale QResource::locale ( ) const

Returns the locale used to locate the data for the QResource.

Definition at line 540 of file qresource.cpp.

References d.

Referenced by QResource(), and setLocale().

+ Here is the caller graph for this function:

◆ registerResource() [1/2]

bool QResource::registerResource ( const QString & rccFileName,
const QString & mapRoot = QString() )
static

Registers the resource with the given rccFileName at the location in the resource tree specified by mapRoot, and returns true if the file is successfully opened; otherwise returns false.

See also
unregisterResource()

Definition at line 1255 of file qresource.cpp.

References qt_resource_fixResourceRoot(), qUtf16Printable, qWarning, resourceList(), and resourceMutex().

Referenced by startQtApplication().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ registerResource() [2/2]

bool QResource::registerResource ( const uchar * rccData,
const QString & mapRoot = QString() )
static
Since
4.3

Registers the resource with the given rccData at the location in the resource tree specified by mapRoot, and returns true if the file is successfully opened; otherwise returns false.

Warning
The data must remain valid throughout the life of any QFile that may reference the resource data.
See also
unregisterResource()

Definition at line 1324 of file qresource.cpp.

References qt_resource_fixResourceRoot(), qUtf16Printable, qWarning, resourceList(), and resourceMutex().

+ Here is the call graph for this function:

◆ setFileName()

void QResource::setFileName ( const QString & file)

Sets a QResource to point to file.

file can either be absolute, in which case it is opened directly, if relative then the file will be tried to be found in QDir::searchPaths().

See also
absoluteFilePath()

Definition at line 554 of file qresource.cpp.

References d, and file.

◆ setLocale()

void QResource::setLocale ( const QLocale & locale)

Sets a QResource to only load the localization of resource to for locale.

If a resource for the specific locale is not found then the C locale is used.

See also
setFileName()

Definition at line 529 of file qresource.cpp.

References d, and locale().

+ Here is the call graph for this function:

◆ size()

qint64 QResource::size ( ) const

Returns the size of the stored data backing the resource.

If the resource is compressed, this function returns the size of the compressed data. See uncompressedSize() for the uncompressed size.

See also
data(), uncompressedSize(), isFile()

Definition at line 645 of file qresource.cpp.

References d.

◆ uncompressedData()

QByteArray QResource::uncompressedData ( ) const
Since
5.15

Returns the resource data, decompressing it first, if the data was stored compressed. If the resource is a directory or an error occurs while decompressing, a null QByteArray is returned.

Note
If the data was compressed, this function will decompress every time it is called. The result is not cached between calls.
See also
uncompressedSize(), size(), compressionAlgorithm(), isFile()

Definition at line 698 of file qresource.cpp.

References d, QByteArray::fromRawData(), NoCompression, QByteArray(), qWarning, uncompressedSize(), and Qt::Uninitialized.

+ Here is the call graph for this function:

◆ uncompressedSize()

qint64 QResource::uncompressedSize ( ) const
Since
5.15

Returns the size of the data in this resource. If the data was not compressed, this function returns the same as size(). If it was, then this function extracts the size of the original uncompressed data from the stored stream.

See also
size(), uncompressedData(), isFile()

Definition at line 662 of file qresource.cpp.

References d.

Referenced by uncompressedData().

+ Here is the caller graph for this function:

◆ unregisterResource() [1/2]

bool QResource::unregisterResource ( const QString & rccFileName,
const QString & mapRoot = QString() )
static

Unregisters the resource with the given rccFileName at the location in the resource tree specified by mapRoot, and returns true if the resource is successfully unloaded and no references exist for the resource; otherwise returns false.

See also
registerResource()

Definition at line 1287 of file qresource.cpp.

References QList< T >::at(), i, list, qt_resource_fixResourceRoot(), QList< T >::removeAt(), resourceList(), resourceMutex(), and QList< T >::size().

+ Here is the call graph for this function:

◆ unregisterResource() [2/2]

bool QResource::unregisterResource ( const uchar * rccData,
const QString & mapRoot = QString() )
static
Since
4.3

Unregisters the resource with the given rccData at the location in the resource tree specified by mapRoot, and returns true if the resource is successfully unloaded and no references exist into the resource; otherwise returns false.

See also
registerResource()

Definition at line 1356 of file qresource.cpp.

References QList< T >::at(), i, list, qt_resource_fixResourceRoot(), QList< T >::removeAt(), resourceList(), resourceMutex(), and QList< T >::size().

+ Here is the call graph for this function:

Friends And Related Symbol Documentation

◆ QResourceFileEngine

friend class QResourceFileEngine
friend

Definition at line 54 of file qresource.h.

◆ QResourceFileEngineIterator

friend class QResourceFileEngineIterator
friend

Definition at line 55 of file qresource.h.

Member Data Documentation

◆ d_ptr

QScopedPointer<QResourcePrivate> QResource::d_ptr
protected

Definition at line 61 of file qresource.h.


The documentation for this class was generated from the following files: