source: trunk/doc/src/resources.qdoc@ 321

Last change on this file since 321 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 8.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42/*!
43 \page resources.html
44 \title The Qt Resource System
45 \ingroup buildsystem
46
47 \keyword resource system
48
49 The Qt resource system is a platform-independent mechanism for
50 storing binary files in the application's executable. This is
51 useful if your application always needs a certain set of files
52 (icons, translation files, etc.) and you don't want to run the
53 risk of losing the files.
54
55 The resource system is based on tight cooperation between \l qmake,
56 \l rcc (Qt's resource compiler), and QFile. It obsoletes Qt 3's
57 \c qembed tool and the
58 \l{http://doc.trolltech.com/qq/qq05-iconography.html#imagestorage}{image
59 collection} mechanism.
60
61 \section1 Resource Collection Files (\c{.qrc})
62
63 The resources associated with an application are specified in a
64 \c .qrc file, an XML-based file format that lists files on the
65 disk and optionally assigns them a resource name that the
66 application must use to access the resource.
67
68 Here's an example \c .qrc file:
69
70 \quotefile mainwindows/application/application.qrc
71
72 The resource files listed in the \c .qrc file are files that are
73 part of the application's source tree. The specified paths are
74 relative to the directory containing the \c .qrc file. Note that
75 the listed resource files must be located in the same directory as
76 the \c .qrc file, or one of its subdirectories.
77
78 Resource data can either be compiled into the binary and thus accessed
79 immediately in application code, or a binary resource can be created
80 and at a later point in application code registered with the resource
81 system.
82
83 By default, resources are accessible in the application under the
84 same name as they have in the source tree, with a \c :/ prefix.
85 For example, the path \c :/images/cut.png would give access to the
86 \c cut.png file, whose location in the application's source tree
87 is \c images/cut.png. This can be changed using the \c file tag's
88 \c alias attribute:
89
90 \snippet doc/src/snippets/code/doc_src_resources.qdoc 0
91
92 The file is then accessible as \c :/cut-img.png from the
93 application. It is also possible to specify a path prefix for all
94 files in the \c .qrc file using the \c qresource tag's \c prefix
95 attribute:
96
97 \snippet doc/src/snippets/code/doc_src_resources.qdoc 1
98
99 In this case, the file is accessible as \c
100 :/myresources/cut-img.png.
101
102 Some resources, such as translation files and icons, many need to
103 change based on the user's locale. This is done by adding a \c lang
104 attribute to the \c qresource tag, specifying a suitable locale
105 string. For example:
106
107 \snippet doc/src/snippets/code/doc_src_resources.qdoc 2
108
109 If the user's locale is French (i.e., QLocale::system().name() returns
110 "fr_FR"), \c :/cut.jpg becomes a reference to the \c cut_fr.jpg
111 image. For other locales, \c cut.jpg is used.
112
113 See the QLocale documentation for a description of the format to use
114 for locale strings.
115
116
117 \section2 External Binary Resources
118
119 For an external binary resource to be created you must create the resource
120 data (commonly given the \c .rcc extension) by passing the -binary switch to
121 \l rcc. Once the binary resource is created you can register the resource
122 with the QResource API.
123
124 For example, a set of resource data specified in a \c .qrc file can be