Write a QT resource file (qrc)

Source: Internet
Author: User

 

Original address: http://my.chinaunix.net/space.php? Uid = 25601623 & Do = Blog & id = 335144

 

Example of a. qrc file:


<! Doctype RCC> <RCC version = "1.0">

<Qresource>

<File> images/copy.png </File>

<File> images/cut.png </File>

<File> images/new.png </File>

<File> images/open.png </File>

<File> images/paste.png </File>

<File> images/save.png </File>

</Qresource>

</RCC>

 

The resource files listed in the. qrc file are part of the source code tree of the program. The specified path is the relative path of the directory where the. qrc file is located. Note: The listed resource files must be located in the directory or subdirectory of The. qrc file.

Resource data can also be compiled into binary files, so the application code can be accessed immediately. You can also create a binary resource, which is later specified in the Resource System Code registered in the program.

Lack of time, the program can use the resource name in the source code tree plus a:/prefix to access it. For example, in the source code tree of a program, the images/cut.png file can be accessed through:/images/cut.png. However, you can also use the alias attribute in the file tag to specify:

<File alias = "cut-img.png"> images/cut.png </File>

The file can then be accessed through:/cut-img.png. You can also use the prefix attribute of the qresource label in the. qrc file: It can specify a prefix for all files in the. qrc file:

<Qresource prefix = "/myresources">

 

<File alias = "cut-img.png"> images/cut.png </File>

</Qresource>

The file can be accessed with:/myresources/cut-img.png.

Some resources, such as translation files and icons, need to change with the user's local configuration. This can be achieved by specifying an appropriate localized string in the lang attribute of the qresource tag. For example:

<Qresource>

 

<File> cut.jpg </File>

</Qresource>

<Qresource lang = "FR">

 

<File alias = "cut.jpg"> cut_fr.jpg </File>

</Qresource>

If your localization settings are French (that is, qlocale: System (). Name () returns "fr_fr"),:/cut.jpg references the cut_fr.jpg image. For other localization settings, cut.jpg is still used.

For the format of localized strings, see qlocale.

Use external binary Resources

To create an external binary resource, you must pass the-binary switch to RCC to generate resource data (generally. RCC extension ). Then, you can use the qresource API to register resources. For example, a resource dataset specified by A. qrc file can be compiled using the following method:

RCC-binary myresource. qrc-O myresource. RCC

In the application, use the following code to register the resource: qresource: registerresource ("/path/to/myresource. RCC ");

Compiled-in resources

You must specify the. qrc file in the. Pro file of the application so that qmake can know and compile the resource into the binary file. For example:

Resources + = application. qrc

Qmake generates a make rule to generate a file named qrc_application.cpp and link it to the application. In this file, all the data of images and other resources are stored in the static C ++ array as compressed binary data. When the. qrc file is changed or one of the files it references is changed, qrc_application.cpp is automatically regenerated. If you do not use the. Pro file, you can also manually call RCC or add creation rules to your compilation system.

Generally, QT stores data directly in executable files, even in Windows and Mac OS X operating systems that provide local resource support. This may change in future QT versions.

Using Resources in the Application

In most applications, the resource path can be used to replace the original file system path. In particular, you can pass the resource path in the qicon, qimage, or qpixmap constructor to replace the file name:

Cutact = new qaction (qicon (":/images/cut.png"), TR ("Cu & T"), this );

In memory, resources are described in the resource object tree. This tree is automatically built at startup and uses qfile to parse the resource path. You can use the ":/" initialized qdir to browse from the root of the Resource Tree.

Qt resources support the search path list concept. If you use ":" instead of ":/" as the prefix to reference a resource, the resource will be queried using the search path list. The search path list is empty at startup. You can call qdir: addresourcesearchpath () to add a path.

If you have resources in a static library, you must use the base name of the. qrc file as the parameter to call q_init_resource () to forcibly initialize the resource. For example:

Int main (INT argc, char * argv [])

{

Qapplication app (argc, argv );

Q_init_resource (graphlib );

...

Return app.exe C ();

}

Similarly, if you need to explicitly Uninstall a resource set (because the plug-in is uninstalled or the resource is invalid), you need to call q_cleanup_resource () with the same base name as the preceding parameter () to forcibly remove the resource.

 

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.