Qt 4.8
|
The QResource class provides an interface for reading directly from resources. More...
#include <qresource.h>
Public Functions | |
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. More... | |
const uchar * | data () const |
Returns direct access to a read only segment of data that this resource represents. More... | |
QString | fileName () const |
Returns the full path to the file that this QResource represents as it was passed. More... | |
bool | isCompressed () const |
Returns true if the resource represents a file and the data backing it is in a compressed format, false otherwise. More... | |
bool | isValid () const |
Returns true if the resource really exists in the resource hierarchy, false otherwise. More... | |
QLocale | locale () const |
Returns the locale used to locate the data for the QResource. More... | |
QResource (const QString &file=QString(), const QLocale &locale=QLocale()) | |
Constructs a QResource pointing to file. More... | |
void | setFileName (const QString &file) |
Sets a QResource to point to file. More... | |
void | setLocale (const QLocale &locale) |
Sets a QResource to only load the localization of resource to for locale. More... | |
qint64 | size () const |
Returns the size of the data backing the resource. More... | |
~QResource () | |
Releases the resources of the QResource object. More... | |
Static Public Functions | |
static void | addSearchPath (const QString &path) |
Use QDir::addSearchPath() with a prefix instead. More... | |
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. More... | |
static bool | registerResource (const uchar *rccData, const QString &resourceRoot=QString()) |
static QStringList | searchPaths () |
Use QDir::searchPaths() instead. More... | |
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. More... | |
static bool | unregisterResource (const uchar *rccData, const QString &resourceRoot=QString()) |
Protected Functions | |
QStringList | children () const |
Returns a list of all resources in this directory, if the resource represents a file the list will be empty. More... | |
bool | isDir () const |
Returns true if the resource represents a directory and thus may have children() in it, false if it represents a file. More... | |
bool | isFile () const |
Returns true if the resource represents a file and thus has data backing it, false if it represents a directory. More... | |
Protected Variables | |
QScopedPointer< QResourcePrivate > | d_ptr |
Friends | |
class | QResourceFileEngine |
class | QResourceFileEngineIterator |
The QResource class provides an interface for reading directly from resources.
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.
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 QResources 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.
Definition at line 58 of file qresource.h.
Constructs a QResource pointing to file.
locale is used to load a specific localization of a resource data.
Definition at line 382 of file qresource.cpp.
QResource::~QResource | ( | ) |
Releases the resources of the QResource object.
Definition at line 392 of file qresource.cpp.
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.
Definition at line 457 of file qresource.cpp.
|
static |
Use QDir::addSearchPath() with a prefix instead.
Adds path to the search paths searched in to find resources that are not specified with an absolute path. The path must be an absolute path (start with /
).
The default search path is to search only in the root (:/
). The last path added will be consulted first upon next QResource creation.
Definition at line 577 of file qresource.cpp.
Referenced by QDir::addResourceSearchPath().
|
protected |
Returns a list of all resources in this directory, if the resource represents a file the list will be empty.
Definition at line 554 of file qresource.cpp.
Referenced by QResourceFileEngineIterator::hasNext(), and qwsFontPath().
const uchar * QResource::data | ( | ) | const |
Returns direct access to a read only segment of data that this resource represents.
If the resource is compressed the data returns is compressed and qUncompress() must be used to access the data. If the resource is a directory 0 is returned.
Definition at line 526 of file qresource.cpp.
Referenced by QFontDatabasePrivate::addQPF2File(), QTranslatorPrivate::do_load(), loadSingleEngine(), and QDynamicFileResourceRoot::registerSelf().
QString QResource::fileName | ( | ) | const |
Returns the full path to the file that this QResource represents as it was passed.
Definition at line 443 of file qresource.cpp.
Referenced by QDynamicFileResourceRoot::mappingFile().
bool QResource::isCompressed | ( | ) | const |
Returns true if the resource represents a file and the data backing it is in a compressed format, false otherwise.
Definition at line 497 of file qresource.cpp.
Referenced by QTranslatorPrivate::do_load().
|
protected |
Returns true if the resource represents a directory and thus may have children() in it, false if it represents a file.
Definition at line 540 of file qresource.cpp.
Referenced by qwsFontPath().
|
inlineprotected |
Returns true if the resource represents a file and thus has data backing it, false if it represents a directory.
Definition at line 90 of file qresource.h.
Referenced by qwsFontPath().
bool QResource::isValid | ( | ) | const |
Returns true if the resource really exists in the resource hierarchy, false otherwise.
Definition at line 470 of file qresource.cpp.
Referenced by QTranslatorPrivate::do_load(), QResourceFileEngineIterator::hasNext(), and loadSingleEngine().
QLocale QResource::locale | ( | ) | const |
Returns the locale used to locate the data for the QResource.
Definition at line 415 of file qresource.cpp.
Referenced by QResource(), and setLocale().
|
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.
Definition at line 1077 of file qresource.cpp.
|
static |
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.
Definition at line 1151 of file qresource.cpp.
|
static |
Use QDir::searchPaths() instead.
Returns the current search path list. This list is consulted when creating a relative resource.
Definition at line 603 of file qresource.cpp.
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().
Definition at line 429 of file qresource.cpp.
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.
Definition at line 404 of file qresource.cpp.
qint64 QResource::size | ( | ) | const |
Returns the size of the data backing the resource.
Definition at line 510 of file qresource.cpp.
Referenced by QFontDatabasePrivate::addQPF2File(), QTranslatorPrivate::do_load(), and loadSingleEngine().
|
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.
Definition at line 1112 of file qresource.cpp.
|
static |
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.
Definition at line 1183 of file qresource.cpp.
|
friend |
Definition at line 87 of file qresource.h.
|
friend |
Definition at line 88 of file qresource.h.
|
protected |
Definition at line 94 of file qresource.h.