~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/resources.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
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 -->
 
6
<head>
 
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>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
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">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <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>&nbsp;   &lt;!DOCTYPE RCC&gt;&lt;RCC version=&quot;1.0&quot;&gt;
 
28
    &lt;qresource&gt;
 
29
        &lt;file&gt;images/copy.png&lt;/file&gt;
 
30
        &lt;file&gt;images/cut.png&lt;/file&gt;
 
31
        &lt;file&gt;images/new.png&lt;/file&gt;
 
32
        &lt;file&gt;images/open.png&lt;/file&gt;
 
33
        &lt;file&gt;images/paste.png&lt;/file&gt;
 
34
        &lt;file&gt;images/save.png&lt;/file&gt;
 
35
    &lt;/qresource&gt;
 
36
    &lt;/RCC&gt;</pre>
 
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>&nbsp;   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>&nbsp;   &lt;file alias=&quot;cut-img.png&quot;&gt;images/cut.png&lt;/file&gt;</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>&nbsp;   &lt;qresource prefix=&quot;/myresources&quot;&gt;
 
46
        &lt;file alias=&quot;cut-img.png&quot;&gt;images/cut.png&lt;/file&gt;
 
47
    &lt;/qresource&gt;</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>&nbsp;   &lt;qresource&gt;
 
51
        &lt;file&gt;cut.jpg&lt;/file&gt;
 
52
    &lt;/qresource&gt;
 
53
    &lt;qresource lang=&quot;fr&quot;&gt;
 
54
        &lt;file alias=&quot;cut.jpg&quot;&gt;cut_fr.jpg&lt;/file&gt;
 
55
    &lt;/qresource&gt;</pre>
 
56
<p>If the user's locale is French (i.e., <a href="qlocale.html#system">QLocale::system</a>().name() returns &quot;fr_FR&quot;), <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>&nbsp;       cutAct = new QAction(QIcon(&quot;:/images/cut.png&quot;), tr(&quot;Cu&amp;t&quot;), 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 &quot;:/&quot; 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>&nbsp;   int main(int argc, char *argv[])
 
66
    {
 
67
        QApplication app(argc, argv);
 
68
        Q_INIT_RESOURCE(graphlib);
 
69
        ...
 
70
        return app.exec();
 
71
    }</pre>
 
72
<p /><address><hr /><div align="center">
 
73
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
74
<td width="30%">Copyright &copy; 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>
 
78
</html>