QResource¶

Synopsis¶
Functions¶
def
absoluteFilePath()def
children()def
compressionAlgorithm()def
data()def
fileName()def
isCompressed()def
isDir()def
isFile()def
isValid()def
lastModified()def
locale()def
setFileName(file)def
setLocale(locale)def
size()def
uncompressedData()def
uncompressedSize()
Static functions¶
def
addSearchPath(path)def
registerResource(rccFilename[, resourceRoot=””])def
registerResourceData(rccData[, resourceRoot=””])def
searchPaths()def
unregisterResource(rccFilename[, resourceRoot=””])def
unregisterResourceData(rccData[, resourceRoot=””])
Detailed Description¶
QResourceis an object that represents a set of data (and possibly children) relating to a single resource entity.QResourcegives 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 withQFile. The data and children behind aQResourceare 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
QResourcecan 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 bysearchPaths().A
QResourcethat is representing a file will have data backing it, this data can possibly be compressed, in which caseqUncompress()must be used to access the real data; this happens implicitly when accessed through aQFile. AQResourcethat 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 intoregisterResource()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 areQResourceobjects 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 furtherQResourcecan be created pointing into this resource data. The resource itself will be unmapped from memory when the lastQResourcethat points to it is destroyed.See also
- class PySide2.QtCore.QResource([file=""[, locale=QLocale()]])¶
- param locale:
- param file:
str
Constructs a
QResourcepointing tofile.localeis used to load a specific localization of a resource data.See also
- PySide2.QtCore.QResource.Compression¶
This enum is used by
compressionAlgorithm()to indicate which algorithm the RCC tool used to compress the payload.Constant
Description
QResource.NoCompression
Contents are not compressed
QResource.ZlibCompression
Contents are compressed using zlib and can be decompressed using the
qUncompress()function.QResource.ZstdCompression
Contents are compressed using zstd . To decompress, use the
ZSTD_decompressfunction from the zstd library.See also
New in version 5.13.
- PySide2.QtCore.QResource.absoluteFilePath()¶
- Return type:
str
Returns the real path that this
QResourcerepresents, if the resource was found via thesearchPaths()it will be indicated in the path.See also
- static PySide2.QtCore.QResource.addSearchPath(path)¶
- Parameters:
path – str
Note
This function is deprecated.
Use
addSearchPath()with a prefix instead.Adds
pathto the search paths searched in to find resources that are not specified with an absolute path. Thepathmust 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 nextQResourcecreation.
- PySide2.QtCore.QResource.children()¶
- Return type:
list of strings
Returns a list of all resources in this directory, if the resource represents a file the list will be empty.
See also
- PySide2.QtCore.QResource.compressionAlgorithm()¶
- Return type:
Returns the compression type that this resource is compressed with, if any. If it is not compressed, this function returns
NoCompression.If this function returns
ZlibCompression, you may decompress the data using theqUncompress()function. Up until Qt 5.13, this was the only possible compression algorithm.If this function returns
ZstdCompression, you need to use the Zstandard library functions (<zstd.h>header). Qt does not provide a wrapper.See Zstandard manual .
- PySide2.QtCore.QResource.data()¶
- Return type:
PyObject
Returns a read only buffer object pointing to the 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 None is returned.
- PySide2.QtCore.QResource.fileName()¶
- Return type:
str
Returns the full path to the file that this
QResourcerepresents as it was passed.See also
- PySide2.QtCore.QResource.isCompressed()¶
- Return type:
bool
Note
This function is deprecated.
Returns
trueif the resource represents a file and the data backing it is in a compressed format, false otherwise. If the data is compressed, checkcompressionAlgorithm()to verify what algorithm to use to decompress the data.Note
This function is deprecated and can be replaced with
compressionAlgorithm() != NoCompression
See also
- PySide2.QtCore.QResource.isDir()¶
- Return type:
bool
Returns
trueif the resource represents a directory and thus may havechildren()in it, false if it represents a file.See also
- PySide2.QtCore.QResource.isFile()¶
- Return type:
bool
Returns
trueif the resource represents a file and thus has data backing it, false if it represents a directory.See also
- PySide2.QtCore.QResource.isValid()¶
- Return type:
bool
Returns
trueif the resource really exists in the resource hierarchy, false otherwise.
- PySide2.QtCore.QResource.lastModified()¶
- Return type:
Returns the date and time when the file was last modified before packaging into a resource.
- PySide2.QtCore.QResource.locale()¶
- Return type:
Returns the locale used to locate the data for the
QResource.See also
- static PySide2.QtCore.QResource.registerResource(rccFilename[, resourceRoot=""])¶
- Parameters:
rccFilename – str
resourceRoot – str
- Return type:
bool
- static PySide2.QtCore.QResource.registerResourceData(rccData[, resourceRoot=""])¶
- Parameters:
rccData – str
resourceRoot – str
- Return type:
bool
- static PySide2.QtCore.QResource.searchPaths()¶
- Return type:
list of strings
Note
This function is deprecated.
Use
searchPaths()instead.Returns the current search path list. This list is consulted when creating a relative resource.
See also
- PySide2.QtCore.QResource.setFileName(file)¶
- Parameters:
file – str
Sets a
QResourceto point tofile.filecan either be absolute, in which case it is opened directly, if relative then the file will be tried to be found insearchPaths().See also
- PySide2.QtCore.QResource.setLocale(locale)¶
- Parameters:
locale –
PySide2.QtCore.QLocale
Sets a
QResourceto only load the localization of resource to forlocale. If a resource for the specific locale is not found then the C locale is used.See also
- PySide2.QtCore.QResource.size()¶
- Return type:
int
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
- PySide2.QtCore.QResource.uncompressedData()¶
- Return type:
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
QByteArrayis 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
- PySide2.QtCore.QResource.uncompressedSize()¶
- Return type:
int
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
- static PySide2.QtCore.QResource.unregisterResource(rccFilename[, resourceRoot=""])¶
- Parameters:
rccFilename – str
resourceRoot – str
- Return type:
bool
- static PySide2.QtCore.QResource.unregisterResourceData(rccData[, resourceRoot=""])¶
- Parameters:
rccData – str
resourceRoot – str
- Return type:
bool
© 2022 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.