Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
resource-system.qdoc
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/*!
5 \page resources.html
6 \title The Qt Resource System
7 \brief A platform-independent mechanism for shipping resource files in an
8 application.
9
10 \keyword resource system
11
12 The Qt resource system is a platform-independent mechanism for shipping
13 resource files in an application. Use it if your application always needs a
14 certain set of files (like icons, translation files, images), and you don't
15 want to use system-specific means to package and locate these resources.
16
17 Most commonly, the resource files are embedded into your application
18 executable, or in libraries and plugins that are loaded by the application
19 executable. Alternatively, the resource files can also be stored in an
20 \l{External Resource Files}{external resource file}.
21
22 The resource system is based on tight cooperation between Qt's \l rcc
23 resource compiler, the build system, and the Qt runtime API.
24
25 \note Currently, the Qt resource system does not make use of any
26 system-specific capabilities for handling resources, such as the ones on
27 Windows, \macos, and iOS. This might change in a future Qt release.
28
29 \section1 The Qt Resource Compiler (rcc)
30
31 The \l{Resource Compiler (rcc)} command line tool reads resource files and
32 generates either a C++ or Python source file, or an \c .rcc file.
33
34 The list of files and related metadata is passed to \c rcc in the form of a
35 \l{Qt Resource Collection File}.
36
37 By default, rcc will generate C++ source code that is then compiled as part
38 of an executable or library. The \c{-g python} option generates Python
39 source code instead. The \c -binary option generates a binary archive that
40 is by convention saved in an \c .rcc file and can be loaded at runtime.
41
42 \note While it is possible to run \c rcc from the command line, this is
43 typically best left to a build system. See also the sections about
44 \l{qmake} and \l{CMake} below.
45
46 \section1 Qt Resource Collection File (.qrc)
47 \target {Qt Resource Collection File}
48 \target {Resource Collection Files}
49
50 A \c .qrc file is an XML document that enumerates local files to be
51 included as runtime resources. It serves as input to \c{rcc}.
52
53 Here's an example \c .qrc file:
54
55 \quotefile resource-system/application.qrc
56
57 Each \c <file> element in the XML identifies a file in the application's
58 source tree. The path is resolved relative to the directory containing
59 the \c .qrc file.
60
61 The path is also used by default to identify the file's content at runtime.
62 That is, the file \c copy.png will be available in the resource system as
63 \c{:/images/copy.png} or \c{qrc:/images/copy.png}.
64 To override this default run-time name, see \l{Prefixes} and \l{Aliases}.
65
66 \e{\QC}, \e{\QDS}, \e{\QD}, \e{\QVSC}, and
67 \e{Qt Visual Studio Tools} allow you to create, inspect and edit \c .qrc
68 files through a convenient user interface. Except for \QD, they also provide
69 wizards for projects using the Qt resource system.
70
71 \section1 Build System Integration
72
73 The processing of resource files with \c rcc is typically done at the time
74 the application is built. Several build tools have dedicated support for
75 this, including \l CMake and \l qmake.
76
77 \section2 CMake
78
79 If \c CMAKE_AUTORCC is enabled, you can just add \c .qrc files as sources
80 to your executable or library. The referenced resource files will then be
81 embedded into the binary:
82
83 \snippet resource-system/CMakeLists.txt AUTORCC
84
85 See \l {https://cmake.org/cmake/help/latest/prop_tgt/AUTORCC.html}
86 {CMake's AUTORCC documentation} for more details about AUTORCC.
87
88 An alternative to AUTORCC is using Qt6Core's CMake function
89 \l qt_add_resources, which gives more control over the creation of
90 resources. For example, it allows you to specify the content of the
91 resource directly in the project file without writing a \c .qrc file first:
92
93 \snippet resource-system/CMakeLists.txt qt_add_resources
94
95 Finally, \l qt_add_qml_module allows you to embed Qt Quick resources into
96 the resource system of your application. The function is defined in the
97 \c Qml component of the \c Qt6 CMake package.
98
99 \section2 qmake
100
101 \l{qmake Manual}{qmake} supports handing resources with the \l{RESOURCES}
102 variable. If you add a \c .qrc file path to the variable, the listed
103 resource files will be embedded into the generated library or executable:
104
105 \snippet resource-system/application.pro 0
106
107 For simple applications, it is also possible to let qmake generate the
108 \c .qrc file for you, avoiding the need for an additional file to be
109 maintained:
110
111 \snippet resource-system/application.pro 1
112
113 This creates a resource of several \c{.png} files, that are addressable
114 like this: \c{":/images/copy.png"}.
115
116 If the directory layout of the files you want to embed into the resource
117 doesn't match the expectations of the application, you can specify
118 \c{resources.base}. \c base is a path prefix that denotes the root point of
119 the file's alias. In the example below, if \c{resources.base} is set to
120 \c{"images"}, then \c{copy.png} is addressable as \c{":/copy.png"}.
121
122 \section1 Runtime API
123
124 Qt API that deals with iterating and reading files has built-in support for
125 the Qt Resource System. You can pass a resource path instead of a local
126 file path to QFile and QDir, but also for instance to the QIcon, QImage, and
127 QPixmap constructors:
128
129 \snippet resource-system/mainwindow.cpp 1
130
131 The \c : prefix makes it explicit that "/images/cut.png" should be loaded
132 from the Qt Resource System.
133
134 You can also reference the Qt resource system through a QUrl. Use the
135 \c qrc scheme in this case:
136
137 \snippet resource-system/main.cpp url
138
139 \section1 Advanced Topics
140
141 \section2 Prefixes
142
143 A \c .qrc file can set a prefix to be added to each local file name, given
144 in a \c <file> element, to get the name by which the file shall be known
145 within the resource system.
146
147 Prefixes allow you to structure the resources, avoiding clashes between
148 resource files added through different \c .qrc files in different libraries
149 or plugins.
150
151 \note The \c /qt and \c /qt-project.org prefixes are reserved for documented
152 use cases in Qt. The \l{Using qt.conf}{qt.conf} file is for instance looked
153 up in \c{:/qt/etc/qt.conf} or \c{qrc:/qt/etc/qt.conf}.
154
155 \section2 Aliases
156
157 Sometimes it is convenient to make a resource file available under a
158 different path at runtime. \c .qrc files allow this by setting an
159 \c alias attribute:
160
161 \snippet code/doc_src_resources.qdoc 0
162
163 The file is from the application then only accessible as \c :/cut-img.png
164 or \c{qrc:/cut-img.png}.
165
166 \section2 Discarding the file contents
167
168 Sometimes you want to add a file node to the resource file system but
169 don't actually want to add the file contents. \c .qrc files allow this
170 by setting the \c empty attribute to \c{true}.
171
172 \snippet code/doc_src_resources.qdoc 4
173
174 The resulting file is then still accessible from the application, but
175 its contents are empty.
176
177 This is useful to strip QML source code from an application binary.
178
179 When building with CMake, use the \l{QT_DISCARD_FILE_CONTENTS} source file
180 property to achieve the same.
181
182 \note If you omit the QML source code from the binary, the QML engine has to
183 rely on the compilation units created by \l{qmlcachegen} or \l{qmlsc}.
184 Those are tied to the specific version of Qt they were built with. If you
185 change the version of Qt your application uses, they can't be loaded
186 anymore.
187
188 \section2 Language Selectors
189
190 Some resources need to change based on the user's locale, such as
191 translation files or icons. \l{Resource Collection Files} support this
192 through a \c lang attribute to the \c qresource tag, specifying a suitable
193 locale string. For example:
194
195 \snippet code/doc_src_resources.qdoc 2
196
197 If the user's locale is French (i.e., QLocale::system().language() is
198 French), \c :/cut.jpg or \c qrc:/cut.jpg becomes a reference to the
199 \c cut_fr.jpg image. For other locales, \c cut.jpg is used.
200
201 See the QLocale documentation for a description of the format to use for
202 locale strings.
203
204 See QFileSelector for an additional mechanism to select locale-specific
205 resources.
206
207 \section2 Embedding Large Files
208
209 By default, \c rcc embeds the resource files into executables in the form
210 of C++ arrays. This can be problematic especially for large resources.
211
212 If the compiler takes too long, or even fails because of memory overflow,
213 you can opt into a special mode where the resources are embedded as part of
214 a two-step process. The C++ compiler only reserves enough space in the
215 target executable or library for the resources. The actual embedding of the
216 resource file's content and metadata is then done after the compilation and
217 linking phase, through another rcc call.
218
219 For qmake, this is enabled by adding \c resources_big to the \c CONFIG
220 variable:
221
222 \snippet resource-system/application.pro 2
223
224 For CMake, you need to use the \l{qt_add_big_resources} function.
225
226 \section2 External Resource Files
227
228 An alternative to embedding the resource files into the binary is to store
229 them in a separate \c .rcc file. \c rcc allows this with the \c -binary
230 option. Such a \c .rcc file must then be loaded at runtime with QResource.
231
232 For example, a set of resource data specified in a \c .qrc file can be
233 compiled in the following way:
234
235 \snippet code/doc_src_resources.qdoc 3
236
237 In the application, this resource would be registered with code like this:
238
239 \snippet code/doc_src_resources.cpp 4
240
241 If you use CMake, you can use the \l{qt_add_binary_resources} function
242 to schedule the \c rcc call above:
243
244 \snippet resource-system/CMakeLists.txt qt_add_binary_resources
245
246 \section2 Resources in a Qt for Python application
247
248 The resource collection file is converted to a Python module by using the
249 resource compiler \l rcc:
250
251 \code
252 rcc -g python mainwindow.qrc > mainwindow_rc.py
253 \endcode
254
255 The module can then be imported in the application:
256
257 \code
258 import mainwindow_rc.py
259 \endcode
260
261 \section2 Compression
262
263 \c rcc attempts to compress the content to optimize disk space usage in the
264 final binaries. By default, it will perform a heuristic check to determine
265 whether compressing is worth it and will store the content uncompressed if
266 it fails to sufficiently compress. To control the threshold, you can use
267 the \c {-threshold} option, which tells \c rcc the percentage of the
268 original file size that must be gained for it to store the file in
269 compressed form.
270
271 \code
272 rcc -threshold 25 myresources.qrc
273 \endcode
274
275 The default value is "70", indicating that the compressed file must be 70%
276 smaller than the original (no more than 30% of the original file size).
277
278 It is possible to turn off compression if desired. This can be useful if
279 your resources already contain a compressed format, such as \c .png files,
280 and you do not want to incur the CPU cost at build time to confirm that it
281 can't be compressed. Another reason is if disk usage is not a problem and
282 the application would prefer to keep the content as clean memory pages at
283 runtime. You do this by giving the \c {-no-compress} command line argument.
284
285 \code
286 rcc -no-compress myresources.qrc
287 \endcode
288
289 \c rcc also gives you some control over the compression level and
290 compression algorithm, for example:
291
292 \code
293 rcc -compress 2 -compress-algo zlib myresources.qrc
294 \endcode
295
296 It is also possible to use \c compress, \c threshold as attributes in a .qrc \c file tag.
297 For selecting the algorithm, set the \c compression-algorithm attribute.
298
299 \code
300 <qresource>
301 <file compress="1" compression-algorithm="zstd">data.txt</file>
302 </qresource>
303 \endcode
304
305 The above will select the \c zstd algorithm with compression level 1.
306
307 \c rcc supports the following compression algorithms and compression
308 levels:
309
310 \list
311 \li \c best: use the best algorithm among the ones below, at its highest
312 compression level, to achieve the most compression at the expense of
313 using a lot of CPU time during compilation. This value is useful in the
314 XML file to indicate a file should be most compressed, regardless of
315 which algorithms \c rcc supports.
316
317 \li \c zstd: use the \l{Zstandard Site}{Zstandard} library to compress
318 contents. Valid compression levels range from 1 to 19, 1 is least
319 compression (least CPU time) and 19 is the most compression (most CPU
320 time). The default level is 14. A special value of 0 tells the \c zstd
321 library to choose an implementation-defined default.
322
323 \li \c zlib: use the \l{https://zlib.net}{zlib} library to compress
324 contents. Valid compression levels range from 1 to 9, with 1 applying
325 the least compression (least CPU time) and 9 the most compression (most
326 CPU time). The special value 0 means "no compression" and should not be
327 used. The default is implementation-defined, but usually is level 6.
328
329 \li \c none: no compression. This is the same as the \c -no-compress
330 option.
331 \endlist
332
333 Support for both Zstandard and zlib are optional. If a given library was
334 not detected at compile time, attempting to pass \c {-compress-algo} for
335 that library will result in an error. The default compression algorithm is
336 \c zstd if it is enabled, \c zlib if not.
337
338 \section2 Explicit Loading and Unloading of Embedded Resources
339
340 Resources embedded in C++ executable or library code are automatically
341 registered to the Qt resource system in a constructor of an internal
342 global variable. Since the global variables are initialized before
343 main() runs, the resources are available from the start of main().
344 Don't rely on them in the constructors of other global variables, which
345 might run before the resources are registered.
346
347 When embedding resources in \e{static} libraries, the C++ linker might
348 remove the static variables that register the resources. If you
349 embed resources in a static library, you therefore need to explicitly
350 register your resources by calling \l Q_INIT_RESOURCE() with the base
351 name of the \c .qrc file.
352 For example:
353
354 \snippet code/doc_src_resources.cpp 5
355
356 You can also explicitly remove registered resources from the application,
357 for instance when unloading a plugin. Use \l Q_CLEANUP_RESOURCE() for this.
358
359 Note: As the resource initializers generated by rcc are declared in the
360 global namespace, your calls to \l Q_INIT_RESOURCE() and
361 \l Q_CLEANUP_RESOURCE() need to be done outside any namespace.
362*/