Resources of qtinternal

Source: Internet
Author: User


Note: This article is translated from
Http://developer.qt.nokia.com
 
In
Resources
 
For Chinese translations, see
Simplified Chinese version
If you are interested in translating the Wiki, please refer to the Wiki Chinese help

Overview

A resource is a standard way of embedding any binary data, including images and sounds, as part of the binary data of an application. For more information, see
Qt document
.

Qt resources are not associated with res files in Windows or resource forks in Mac OS X.

How it works

A resource file (that is, the. qrc file) is an XML file used to specify which files should be packaged into the final binary file. Resource compiler
RCC
 
Parse the XML file and generate C/C ++ code. The generated code contains a C struct containing the original bytes of all files listed in. qrc. Just as a file can be arranged in a hierarchy, these C structs are clustered into another c struct to form a tree structure. The details of these C struct are not very interesting, so we will not discuss it here. Specify-Compressed
 
The data in the struct can be compressed using zlib (compression is disabled by default ).

To be understood by QT, these C structures need to be registered. The trick is that at the end of each c ++ file generated by RCC, RCC places a call to the function qregisterresourcedata () that registers the C struct to the QT resource system. Pass
Constructor function pattern)
 
Qregisterresourcedata () is called before Main.Constructor Mode
 
It is a global static variable that calls a function to initialize its own usage.

#ifndef Q_CONSTRUCTOR_FUNCTION# define Q_CONSTRUCTOR_FUNCTION0(AFUNC) /   static const int AFUNC ## __init_variable__ = AFUNC();# define Q_CONSTRUCTOR_FUNCTION(AFUNC) Q_CONSTRUCTOR_FUNCTION0(AFUNC)#endif

The end of the C ++ file generated by RCC contains something similar to the following:

int qInitResources_images(){    qRegisterResourceData(0x01, qt_resource_struct, qt_resource_name, qt_resource_data);    return 1;}Q_CONSTRUCTOR_FUNCTION(qInitResources_images)

Because static variables are initialized before Main () is called, all resources are automatically registered before the program is started.

Q_init_resource

The above constructor technique only works when the generated C ++ file is directly compiled and linked to the main application. If a resource is used in a static database and then linked to the main application, the constructor will never be called. This is the essence of the C/C ++ link-functions and variables in the library that are not used or called by the final application will be removed (optimized) from the library ). If the resource is in the shared library, the constructor is not called. However, most platforms Load dynamic libraries as needed, and the constructor is called only when the shared libraries are imported. The result is that no resources in the plug-in used by the plug-in itself are found unless the plug-in itself is loaded into the main application. If the resources in the shared library are used only for the shared library, this is not a problem. Please note that accessing resources within a shared object of the main application does not trigger the loading of shared objects, because there is no symbol exported by the qrc system to trigger the loading of shared objects.

A solution to the above problems is to use generated variables (as mentioned above
Afunc ##__ init_variable __
) Or call the qinitresource_images () generated above (). The significance of the existence of macro q_init_resource lies in this.
It is required when your static or shared library has resources
 
.

 

#define Q_INIT_RESOURCE_EXTERN(name) /    extern int qInitResources_ ## name();

For each qrc file, the macro above is usually placed at the beginning of the main function.

Access resources

Qfile, qdir, and qpixmap can all access the qrc file. In QT, all files are
Qw.actfileengine
 
Accessed. When the file name is prefixed with ":", qresource registers a derived class object of qiniactfilesystemengine that provides a valid File System engine (via qiniactfileengine: Create) at startup.

Namespacing Resources)

When a resource is used in a plug-in, the resource path must be named in a space. Using the plug-in name as the resource namespace is a good technique. For example,:/Plugin_name/resourcename
 
. If the path of an existing resource file has been registered with different data, it will be ignored and the first file will be retained.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.