1
<?xml version="1.0" encoding="iso-8859-1"?>
3
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/resources.qdoc -->
7
<title>Qt 4.0: The Qt Resource System</title>
8
<style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
9
a:link { color: #004faf; text-decoration: none }
10
a:visited { color: #672967; text-decoration: none }
11
td.postheader { font-family: sans-serif }
12
tr.address { font-family: sans-serif }
13
body { background: #ffffff; color: black; }</style>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
19
<td width="1"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <a href="functions.html"><font color="#004faf">Functions</font></a></td>
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">The Qt Resource System</h1>
21
<a name="resource-system"></a><p>The Qt resource system is a platform-independent mechanism for storing binary files in the application's executable. This is useful if your application always needs a certain set of files (icons, translation files, etc.) and you don't want to run the risk of losing the files.</p>
22
<p>The resource system is based on tight cooperation between <tt>qmake</tt>, <tt>rcc</tt> (Qt's resource compiler), and <a href="qfile.html">QFile</a>. It obsoletes Qt 3's <tt>qembed</tt> tool and the <a href="http://doc.trolltech.com/qq/qq05-iconography.html#imagestorage">image collection</a> mechanism.</p>
23
<a name="resource-collection-files"></a>
24
<h2>Resource Collection Files (<tt>.qrc</tt>)</h2>
25
<p>The resources associated with an application are specified in a <tt>.qrc</tt> file, an XML-based file format that lists files on the disk and optionally assigns them a resource name that the application must use to access the resource.</p>
26
<p>Here's an example <tt>.qrc</tt> file:</p>
27
<pre> <!DOCTYPE RCC><RCC version="1.0">
29
<file>images/copy.png</file>
30
<file>images/cut.png</file>
31
<file>images/new.png</file>
32
<file>images/open.png</file>
33
<file>images/paste.png</file>
34
<file>images/save.png</file>
37
<p>The resource files listed in the <tt>.qrc</tt> file are files that are part of the application's source tree. Paths are relative to the directory where the <tt>.qrc</tt> file is located.</p>
38
<p>The <tt>.qrc</tt> file must be mentioned in the application's <tt>.pro</tt> file so that <tt>qmake</tt> knows about it. For example:</p>
39
<pre> RESOURCES = application.qrc</pre>
40
<p><tt>qmake</tt> will produce make rules to generate a file called <tt>qrc_application.cpp</tt> that is linked into the application. This file contains all the data for the images and other resources as static C++ arrays of compressed binary data. The <tt>qrc_application.cpp</tt> file is automatically regenerated whenever the <tt>.qrc</tt> file changes or one of the files that it refers to changes. If you don't use <tt>.pro</tt> files, you can either invoke <tt>rcc</tt> manually or add build rules to your build system.</p>
41
<center><img src="images/resources.png" alt="Building resources into an application" /></center><p>Currently, Qt always stores the data directly in the executable, even on Windows and Mac OS X, where the operating system provides native support for resources. This might change in a future Qt release.</p>
42
<p>By default, resources are accessible in the application under the same name as they have in the source tree, with a <tt>:/</tt> prefix. For example, the path <tt>:/image/cut.png</tt> would give access to the <tt>cut.png</tt> file, whose location in the application's source tree is <tt>images/cut.png</tt>. This can be changed using the <tt>file</tt> tag's <tt>alias</tt> attribute:</p>
43
<pre> <file alias="cut-img.png">images/cut.png</file></pre>
44
<p>The file is then accessible as <tt>:/cut-img.png</tt> from the application. It is also possible to specify a path prefix for all files in the <tt>.qrc</tt> file using the <tt>qresource</tt> tag's <tt>prefix</tt> attribute:</p>
45
<pre> <qresource prefix="/myresources">
46
<file alias="cut-img.png">images/cut.png</file>
47
</qresource></pre>
48
<p>In this case, the file is accessible as <tt>:/myresources/cut-img.png</tt>.</p>
49
<p>Some resources, such as translation files and icons, many need to change based on the user's locale. This is possible using the <tt>lang</tt> attribute. For example:</p>
50
<pre> <qresource>
51
<file>cut.jpg</file>
53
<qresource lang="fr">
54
<file alias="cut.jpg">cut_fr.jpg</file>
55
</qresource></pre>
56
<p>If the user's locale is French (i.e., <a href="qlocale.html#system">QLocale::system</a>().name() returns "fr_FR"), <tt>:/cut.jpg</tt> becomes a reference to the <tt>cut_fr.jpg</tt> image. For other locales, <tt>cut.jpg</tt> is used.</p>
57
<a name="using-resources-in-the-application"></a>
58
<h2>Using Resources in the Application</h2>
59
<p>In the application, resource paths can be used in most places instead of ordinary file system paths. In particular, you can pass a resource path instead of a file name to the <a href="qicon.html">QIcon</a>, <a href="qimage.html">QImage</a>, or <a href="qpixmap.html">QPixmap</a> constructor:</p>
60
<pre> cutAct = new QAction(QIcon(":/images/cut.png"), tr("Cu&t"), this);</pre>
61
<p>See the <a href="mainwindows-application.html">Application</a> example for an actual application that uses Qt's resource system to store its icons.</p>
62
<p>In memory, resources are represented by a tree of resource objects. The tree is automatically built at startup and used by <a href="qfile.html">QFile</a> for resolving paths to resources. You can use a <a href="qdir.html">QDir</a> initialized with ":/" to navigate through the resource tree from the root.</p>
63
<p>Qt's resources support the concept of a search path list. If you then refer to a resource with <tt>:</tt> instead of <tt>:/</tt> as the prefix, the resource will be looked up using the search path list. The search path list is empty at startup; call <a href="qdir.html#addResourceSearchPath">QDir::addResourceSearchPath</a>() to add paths to it.</p>
64
<p>If you have resources in a static library, you might need to force initialization of your resources by calling <tt>Q_INIT_RESOURCE(xxx)</tt>, where <tt>xxx</tt> is the base name of the <tt>.qrc</tt> file. For example:</p>
65
<pre> int main(int argc, char *argv[])
67
QApplication app(argc, argv);
68
Q_INIT_RESOURCE(graphlib);
72
<p /><address><hr /><div align="center">
73
<table width="100%" cellspacing="0" border="0"><tr class="address">
74
<td width="30%">Copyright © 2005 <a href="trolltech.html">Trolltech</a></td>
75
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
76
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
77
</tr></table></div></address></body>