Qt 4.8
Public Functions | Static Public Functions | Protected Functions | Protected Variables | Friends | List of all members
QResource Class Reference

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 uchardata () 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< QResourcePrivated_ptr
 

Friends

class QResourceFileEngine
 
class QResourceFileEngineIterator
 

Detailed Description

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

Note
This class or function is 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.

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 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.

See also
{The Qt Resource System}, QFile, QDir, QFileInfo

Definition at line 58 of file qresource.h.

Constructors and Destructors

◆ 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 382 of file qresource.cpp.

382  : d_ptr(new QResourcePrivate(this))
383 {
384  Q_D(QResource);
385  d->fileName = file;
386  d->locale = locale;
387 }
double d
Definition: qnumeric_p.h:62
QLocale locale() const
Returns the locale used to locate the data for the QResource.
Definition: qresource.cpp:415
#define Q_D(Class)
Definition: qglobal.h:2482
QScopedPointer< QResourcePrivate > d_ptr
Definition: qresource.h:94
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ ~QResource()

QResource::~QResource ( )

Releases the resources of the QResource object.

Definition at line 392 of file qresource.cpp.

393 {
394 }

Functions

◆ 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 457 of file qresource.cpp.

458 {
459  Q_D(const QResource);
460  d->ensureInitialized();
461  return d->absoluteFilePath;
462 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ addSearchPath()

void QResource::addSearchPath ( const QString path)
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().

578 {
579  if (!path.startsWith(QLatin1Char('/'))) {
580  qWarning("QResource::addResourceSearchPath: Search paths must be absolute (start with /) [%s]",
581  path.toLocal8Bit().data());
582  return;
583  }
584  QMutexLocker lock(resourceMutex());
585  resourceSearchPaths()->prepend(path);
586 }
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
Q_CORE_EXPORT void qWarning(const char *,...)
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QReadWriteLock lock
Definition: proxyconf.cpp:399
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ 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 554 of file qresource.cpp.

Referenced by QResourceFileEngineIterator::hasNext(), and qwsFontPath().

555 {
556  Q_D(const QResource);
557  d->ensureChildren();
558  return d->children;
559 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ data()

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.

See also
size(), isCompressed(), isFile()

Definition at line 526 of file qresource.cpp.

Referenced by QFontDatabasePrivate::addQPF2File(), QTranslatorPrivate::do_load(), loadSingleEngine(), and QDynamicFileResourceRoot::registerSelf().

527 {
528  Q_D(const QResource);
529  d->ensureInitialized();
530  return d->data;
531 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ 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 443 of file qresource.cpp.

Referenced by QDynamicFileResourceRoot::mappingFile().

444 {
445  Q_D(const QResource);
446  d->ensureInitialized();
447  return d->fileName;
448 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ isCompressed()

bool QResource::isCompressed ( ) const

Returns true if the resource represents a file and the data backing it is in a compressed format, false otherwise.

See also
data(), isFile()

Definition at line 497 of file qresource.cpp.

Referenced by QTranslatorPrivate::do_load().

498 {
499  Q_D(const QResource);
500  d->ensureInitialized();
501  return d->compressed;
502 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ 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 540 of file qresource.cpp.

Referenced by qwsFontPath().

541 {
542  Q_D(const QResource);
543  d->ensureInitialized();
544  return d->container;
545 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ 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 90 of file qresource.h.

Referenced by qwsFontPath().

90 { return !isDir(); }
bool isDir() const
Returns true if the resource represents a directory and thus may have children() in it...
Definition: qresource.cpp:540

◆ isValid()

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().

471 {
472  Q_D(const QResource);
473  d->ensureInitialized();
474  return !d->related.isEmpty();
475 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ locale()

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().

416 {
417  Q_D(const QResource);
418  return d->locale;
419 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ 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 1077 of file qresource.cpp.

1078 {
1079  QString r = qt_resource_fixResourceRoot(resourceRoot);
1080  if(!r.isEmpty() && r[0] != QLatin1Char('/')) {
1081  qWarning("QDir::registerResource: Registering a resource [%s] must be rooted in an absolute path (start with /) [%s]",
1082  rccFilename.toLocal8Bit().data(), resourceRoot.toLocal8Bit().data());
1083  return false;
1084  }
1085 
1087  if(root->registerSelf(rccFilename)) {
1088  root->ref.ref();
1089  QMutexLocker lock(resourceMutex());
1090  resourceList()->append(root);
1091  return true;
1092  }
1093  delete root;
1094  return false;
1095 }
bool registerSelf(const QString &f)
Definition: qresource.cpp:983
static QString qt_resource_fixResourceRoot(QString r)
Definition: qresource.cpp:1052
bool ref()
Atomically increments the value of this QAtomicInt.
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
Q_CORE_EXPORT void qWarning(const char *,...)
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QReadWriteLock lock
Definition: proxyconf.cpp:399
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QAtomicInt ref
Definition: qresource.cpp:121

◆ 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 1151 of file qresource.cpp.

1152 {
1153  QString r = qt_resource_fixResourceRoot(resourceRoot);
1154  if(!r.isEmpty() && r[0] != QLatin1Char('/')) {
1155  qWarning("QDir::registerResource: Registering a resource [%p] must be rooted in an absolute path (start with /) [%s]",
1156  rccData, resourceRoot.toLocal8Bit().data());
1157  return false;
1158  }
1159 
1161  if(root->registerSelf(rccData)) {
1162  root->ref.ref();
1163  QMutexLocker lock(resourceMutex());
1164  resourceList()->append(root);
1165  return true;
1166  }
1167  delete root;
1168  return false;
1169 }
static QString qt_resource_fixResourceRoot(QString r)
Definition: qresource.cpp:1052
bool ref()
Atomically increments the value of this QAtomicInt.
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
Q_CORE_EXPORT void qWarning(const char *,...)
bool registerSelf(const uchar *b)
Definition: qresource.cpp:907
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QReadWriteLock lock
Definition: proxyconf.cpp:399
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QAtomicInt ref
Definition: qresource.cpp:121

◆ searchPaths()

QStringList QResource::searchPaths ( )
static

Use QDir::searchPaths() instead.

Returns the current search path list. This list is consulted when creating a relative resource.

See also
QDir::addSearchPath() QDir::setSearchPaths()

Definition at line 603 of file qresource.cpp.

604 {
605  QMutexLocker lock(resourceMutex());
606  return *resourceSearchPaths();
607 }
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
static QReadWriteLock lock
Definition: proxyconf.cpp:399

◆ 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 429 of file qresource.cpp.

430 {
431  Q_D(QResource);
432  d->clear();
433  d->fileName = file;
434 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ 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 404 of file qresource.cpp.

405 {
406  Q_D(QResource);
407  d->clear();
408  d->locale = locale;
409 }
double d
Definition: qnumeric_p.h:62
QLocale locale() const
Returns the locale used to locate the data for the QResource.
Definition: qresource.cpp:415
#define Q_D(Class)
Definition: qglobal.h:2482
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ size()

qint64 QResource::size ( ) const

Returns the size of the data backing the resource.

See also
data(), isFile()

Definition at line 510 of file qresource.cpp.

Referenced by QFontDatabasePrivate::addQPF2File(), QTranslatorPrivate::do_load(), and loadSingleEngine().

511 {
512  Q_D(const QResource);
513  d->ensureInitialized();
514  return d->size;
515 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QResource class provides an interface for reading directly from resources.
Definition: qresource.h:58

◆ 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 1112 of file qresource.cpp.

1113 {
1114  QString r = qt_resource_fixResourceRoot(resourceRoot);
1115 
1116  QMutexLocker lock(resourceMutex());
1117  ResourceList *list = resourceList();
1118  for(int i = 0; i < list->size(); ++i) {
1119  QResourceRoot *res = list->at(i);
1120  if(res->type() == QResourceRoot::Resource_File) {
1121  QDynamicFileResourceRoot *root = reinterpret_cast<QDynamicFileResourceRoot*>(res);
1122  if(root->mappingFile() == rccFilename && root->mappingRoot() == r) {
1123  resourceList()->removeAt(i);
1124  if(!root->ref.deref()) {
1125  delete root;
1126  return true;
1127  }
1128  return false;
1129  }
1130  }
1131  }
1132  return false;
1133 }
static QString qt_resource_fixResourceRoot(QString r)
Definition: qresource.cpp:1052
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual QString mappingRoot() const
Definition: qresource.cpp:904
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
bool deref()
Atomically decrements the value of this QAtomicInt.
QString mappingFile() const
Definition: qresource.cpp:980
virtual ResourceRootType type() const
Definition: qresource.cpp:138
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static QReadWriteLock lock
Definition: proxyconf.cpp:399
QAtomicInt ref
Definition: qresource.cpp:121

◆ 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 1183 of file qresource.cpp.

1184 {
1185  QString r = qt_resource_fixResourceRoot(resourceRoot);
1186 
1187  QMutexLocker lock(resourceMutex());
1188  ResourceList *list = resourceList();
1189  for(int i = 0; i < list->size(); ++i) {
1190  QResourceRoot *res = list->at(i);
1191  if(res->type() == QResourceRoot::Resource_Buffer) {
1192  QDynamicBufferResourceRoot *root = reinterpret_cast<QDynamicBufferResourceRoot*>(res);
1193  if(root->mappingBuffer() == rccData && root->mappingRoot() == r) {
1194  resourceList()->removeAt(i);
1195  if(!root->ref.deref()) {
1196  delete root;
1197  return true;
1198  }
1199  return false;
1200  }
1201  }
1202  }
1203  return false;
1204 }
static QString qt_resource_fixResourceRoot(QString r)
Definition: qresource.cpp:1052
The QString class provides a Unicode character string.
Definition: qstring.h:83
virtual QString mappingRoot() const
Definition: qresource.cpp:904
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
bool deref()
Atomically decrements the value of this QAtomicInt.
virtual ResourceRootType type() const
Definition: qresource.cpp:138
The QMutexLocker class is a convenience class that simplifies locking and unlocking mutexes...
Definition: qmutex.h:101
const uchar * mappingBuffer() const
Definition: qresource.cpp:903
int size() const
Returns the number of items in the list.
Definition: qlist.h:137
static QReadWriteLock lock
Definition: proxyconf.cpp:399
QAtomicInt ref
Definition: qresource.cpp:121

Friends and Related Functions

◆ QResourceFileEngine

friend class QResourceFileEngine
friend

Definition at line 87 of file qresource.h.

◆ QResourceFileEngineIterator

Definition at line 88 of file qresource.h.

Properties

◆ d_ptr

QScopedPointer<QResourcePrivate> QResource::d_ptr
protected

Definition at line 94 of file qresource.h.


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