QResource 类提供用于直接读取资源的接口。 更多...
头: | #include <QResource> |
CMake: |
find_package(Qt6 COMPONENTS Core REQUIRED)
target_link_libraries(mytarget PRIVATE Qt6::Core) |
qmake: | QT += core |
注意: 此类的所有函数 可重入 .
enum | 压缩 { NoCompression, ZlibCompression, ZstdCompression } |
QResource (const QString & file = QString(), const QLocale & locale = QLocale()) | |
~QResource () | |
QString | absoluteFilePath () const |
QResource::Compression | compressionAlgorithm () const |
const uchar * | data () const |
QString | fileName () const |
bool | isValid () const |
QDateTime | lastModified () const |
QLocale | locale () const |
void | setFileName (const QString & file ) |
void | setLocale (const QLocale & locale ) |
qint64 | size () const |
QByteArray | uncompressedData () const |
qint64 | uncompressedSize () const |
bool | registerResource (const QString & rccFileName , const QString & mapRoot = QString()) |
bool | registerResource (const uchar * rccData , const QString & mapRoot = QString()) |
bool | unregisterResource (const QString & rccFileName , const QString & mapRoot = QString()) |
bool | unregisterResource (const uchar * rccData , const QString & mapRoot = QString()) |
QStringList | children () const |
bool | isDir () const |
bool | isFile () const |
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 Qt 资源系统 文档编制。
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 ().
unregisterResource () function removes a reference to a particular file. If there are QResource objects 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.
另请参阅 Qt 资源系统 , QFile , QDir ,和 QFileInfo .
[since 5.13]
enum QResource::
压缩
此枚举用于 compressionAlgorithm () to indicate which algorithm the RCC tool used to compress the payload.
常量 | 值 | 描述 |
---|---|---|
QResource::NoCompression
|
0
|
内容未压缩 |
QResource::ZlibCompression
|
1
|
内容被压缩使用 zlib and can be decompressed using the qUncompress () 函数。 |
QResource::ZstdCompression
|
2
|
内容被压缩使用
zstd
. To decompress, use the
ZSTD_decompress
function from the zstd library.
|
该枚举在 Qt 5.13 引入或被修改。
另请参阅 compressionAlgorithm ().
Constructs a QResource pointing to file . locale is used to load a specific localization of a resource data.
另请参阅 QFileInfo , QDir::searchPaths (), setFileName (),和 setLocale ().
Releases the resources of the QResource 对象。
Returns the real path that this QResource represents, if the resource was found via the QDir::searchPaths () it will be indicated in the path.
另请参阅 fileName ().
[protected]
QStringList
QResource::
children
() const
Returns a list of all resources in this directory, if the resource represents a file the list will be empty.
另请参阅 isDir ().
[since 5.13]
QResource::Compression
QResource::
compressionAlgorithm
() const
Returns the compression type that this resource is compressed with, if any. If it is not compressed, this function returns QResource::NoCompression .
若此函数返回 QResource::ZlibCompression , you may decompress the data using the qUncompress () function. Up until Qt 5.13, this was the only possible compression algorithm.
若此函数返回
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}. \sa data(), isFile()
该函数在 Qt 5.13 引入。
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
被返回。
另请参阅 uncompressedData (), size (),和 isFile ().
Returns the full path to the file that this QResource represents as it was passed.
另请参阅 setFileName () 和 absoluteFilePath ().
[protected]
bool
QResource::
isDir
() const
返回
true
if the resource represents a directory and thus may have
children
() in it, false if it represents a file.
另请参阅 isFile ().
[protected]
bool
QResource::
isFile
() const
返回
true
if the resource represents a file and thus has data backing it, false if it represents a directory.
另请参阅 isDir ().
返回
true
if the resource really exists in the resource hierarchy, false otherwise.
[since 5.8]
QDateTime
QResource::
lastModified
() const
Returns the date and time when the file was last modified before packaging into a resource.
该函数在 Qt 5.8 引入。
Returns the locale used to locate the data for the QResource .
另请参阅 setLocale ().
[static]
bool
QResource::
registerResource
(const
QString
&
rccFileName
, const
QString
&
mapRoot
= QString())
Registers the resource with the given
rccFileName
at the location in the resource tree specified by
mapRoot
,并返回
true
if the file is successfully opened; otherwise returns
false
.
另请参阅 unregisterResource ().
[static]
bool
QResource::
registerResource
(const
uchar
*
rccData
, const
QString
&
mapRoot
= QString())
Registers the resource with the given
rccData
at the location in the resource tree specified by
mapRoot
,并返回
true
if the file is successfully opened; otherwise returns
false
.
警告: The data must remain valid throughout the life of any QFile that may reference the resource data.
另请参阅 unregisterResource ().
设置 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 ().
另请参阅 fileName () 和 absoluteFilePath ().
设置 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.
另请参阅 locale () 和 setFileName ().
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.
另请参阅 data (), uncompressedSize (),和 isFile ().
[since 5.15]
QByteArray
QResource::
uncompressedData
() const
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 被返回。
注意: If the data was compressed, this function will decompress every time it is called. The result is not cached between calls.
该函数在 Qt 5.15 引入。
另请参阅 uncompressedSize (), size (), compressionAlgorithm (),和 isFile ().
[since 5.15]
qint64
QResource::
uncompressedSize
() const
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.
该函数在 Qt 5.15 引入。
另请参阅 size (), uncompressedData (),和 isFile ().
[static]
bool
QResource::
unregisterResource
(const
QString
&
rccFileName
, const
QString
&
mapRoot
= QString())
Unregisters the resource with the given
rccFileName
at the location in the resource tree specified by
mapRoot
,并返回
true
if the resource is successfully unloaded and no references exist for the resource; otherwise returns
false
.
另请参阅 registerResource ().
[static]
bool
QResource::
unregisterResource
(const
uchar
*
rccData
, const
QString
&
mapRoot
= QString())
Unregisters the resource with the given
rccData
at the location in the resource tree specified by
mapRoot
,并返回
true
if the resource is successfully unloaded and no references exist into the resource; otherwise returns
false
.
另请参阅 registerResource ().