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.
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 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 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. This enum is used by compressionAlgorithm to indicate which algorithm the RCC tool used to compress the payload.
Constructs a QResource pointing to file. Returns the real path that this QResource represents, if the resource was found via the QDir::searchPaths it will be indicated in the path. Returns a list of all resources in this directory, if the resource represents a file the list will be empty.
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. Resources are bundled into Python files which can be distributed along with your source code, guaranteeing they will continue to work on other platforms.
You can manage resources through Qt Designer and use the resource library to load icons and other data in your apps.
To demonstrate how to use the resource system, we'll create a simple application that uses two data files -- in this case, two icon image files. However, remember you can package any type of data you like as resources, including data files that your application depends on. Save this file to your computer as app. Run it and you'll see the window appear with the button, but with no icons visible.
This is what happens when you create QIcon objects with files that cannot be found -- they are simply omitted. If you've had issues when packaging your applications, this will be familiar to you. Without the icons available, nothing is shown. Download and place the animal-penguin. These icons are from the Fugue icon set by Yusuke Kamiyamane. Run the application again and you'll see the icons as expected. With the icons in the same folder as the script, they are now visible. Now the application is working as expected, we'll switch to using the QResource system to load these two files.
First we need to define our QRC file and add our resources to it. This might seem like overkill for 2 files, but as your projects get larger the advantages become clearer! You can also create QRC files and add and remove resources using Qt Designer, which we'll cover later. A very simple resource file is shown below, containing a single resource a single icon animal-penguin. The alias is the name which this resource will be known by from within your application.
You can use this rename icons to something more logical or simpler in your app, while keeping the original name externally. For example, if we want to use the name penguin. This only changes the name used inside your application, the filename remains unchanged.
Outside this tag is the qresource tag which specifies a prefix. This is a namespace which can be used to group resources together. This is effectively a virtual folder, under which nested resources can all be found. Save the following file as resources.
To use a. PyQt5 comes with a command line tool to do this, which takes a. This can then be imported into your app as for any other Python file or module. To compile our resources. If you run this now, it will look exactly as before, but now the icons are being loaded from the resources. Icons visible, loading from the QRC file. Resource data can either be compiled into the binary and thus accessed immediately in application code, or a binary resource can be created and at a later point in application code registered with the resource system.
This can be changed using the file tag's alias attribute:. It is also possible to specify a path prefix for all files in the. Some resources need to change based on the user's locale, such as translation files or icons. This is done by adding a lang attribute to the qresource tag, specifying a suitable locale string. For example:. If the user's locale is French i. For other locales, cut. See the QLocale documentation for a description of the format to use for locale strings.
See QFileSelector for an additional mechanism to select locale-specific resources, in addition to the ability to select OS-specific and other features. For an external binary resource to be created you must create the resource data commonly given the. For example, a set of resource data specified in a.
For a resource to be compiled into the binary the. If you don't use. Currently, Qt always stores the data directly in the executable, even on Windows, macOS, and iOS, where the operating system provides native support for resources. This might change in a future Qt release.
The resource collection file is converted to a Python module by using the resource compiler rcc :.
0コメント