<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- resources.qdoc --> <title>Qt 4.8: The Qt Resource System</title> <link rel="stylesheet" type="text/css" href="style/style.css" /> <script src="scripts/jquery.js" type="text/javascript"></script> <script src="scripts/functions.js" type="text/javascript"></script> <link rel="stylesheet" type="text/css" href="style/superfish.css" /> <link rel="stylesheet" type="text/css" href="style/narrow.css" /> <!--[if IE]> <meta name="MSSmartTagsPreventParsing" content="true"> <meta http-equiv="imagetoolbar" content="no"> <![endif]--> <!--[if lt IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie6.css"> <![endif]--> <!--[if IE 7]> <link rel="stylesheet" type="text/css" href="style/style_ie7.css"> <![endif]--> <!--[if IE 8]> <link rel="stylesheet" type="text/css" href="style/style_ie8.css"> <![endif]--> <script src="scripts/superfish.js" type="text/javascript"></script> <script src="scripts/narrow.js" type="text/javascript"></script> </head> <body class="" onload="CheckEmptyAndLoadList();"> <div class="header" id="qtdocheader"> <div class="content"> <div id="nav-logo"> <a href="index.html">Home</a></div> <a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a> <div id="narrowsearch"></div> <div id="nav-topright"> <ul> <li class="nav-topright-home"><a href="http://qt.digia.com/">Qt HOME</a></li> <li class="nav-topright-dev"><a href="http://qt-project.org/">DEV</a></li> <li class="nav-topright-doc nav-topright-doc-active"><a href="http://qt-project.org/doc/"> DOC</a></li> <li class="nav-topright-blog"><a href="http://blog.qt.digia.com/">BLOG</a></li> </ul> </div> <div id="shortCut"> <ul> <li class="shortCut-topleft-inactive"><span><a href="index.html">Qt 4.8</a></span></li> <li class="shortCut-topleft-active"><a href="http://qt-project.org/doc/">ALL VERSIONS </a></li> </ul> </div> <ul class="sf-menu" id="narrowmenu"> <li><a href="#">API Lookup</a> <ul> <li><a href="classes.html">Class index</a></li> <li><a href="functions.html">Function index</a></li> <li><a href="modules.html">Modules</a></li> <li><a href="namespaces.html">Namespaces</a></li> <li><a href="qtglobal.html">Global Declarations</a></li> <li><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </li> <li><a href="#">Qt Topics</a> <ul> <li><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li><a href="supported-platforms.html">Supported Platforms</a></li> <li><a href="technology-apis.html">Qt and Key Technologies</a></li> <li><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </li> <li><a href="#">Examples</a> <ul> <li><a href="all-examples.html">Examples</a></li> <li><a href="tutorials.html">Tutorials</a></li> <li><a href="demos.html">Demos</a></li> <li><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </li> </ul> </div> </div> <div class="wrapper"> <div class="hd"> <span></span> </div> <div class="bd group"> <div class="sidebar"> <div class="searchlabel"> Search index:</div> <div class="search" id="sidebarsearch"> <form id="qtdocsearch" action="" onsubmit="return false;"> <fieldset> <input type="text" name="searchstring" id="pageType" value="" /> <div id="resultdialog"> <a href="#" id="resultclose">Close</a> <p id="resultlinks" class="all"><a href="#" id="showallresults">All</a> | <a href="#" id="showapiresults">API</a> | <a href="#" id="showarticleresults">Articles</a> | <a href="#" id="showexampleresults">Examples</a></p> <p id="searchcount" class="all"><span id="resultcount"></span><span id="apicount"></span><span id="articlecount"></span><span id="examplecount"></span> results:</p> <ul id="resultlist" class="all"> </ul> </div> </fieldset> </form> </div> <div class="box first bottombar" id="lookup"> <h2 title="API Lookup"><span></span> API Lookup</h2> <div id="list001" class="list"> <ul id="ul001" > <li class="defaultLink"><a href="classes.html">Class index</a></li> <li class="defaultLink"><a href="functions.html">Function index</a></li> <li class="defaultLink"><a href="modules.html">Modules</a></li> <li class="defaultLink"><a href="namespaces.html">Namespaces</a></li> <li class="defaultLink"><a href="qtglobal.html">Global Declarations</a></li> <li class="defaultLink"><a href="qdeclarativeelements.html">QML elements</a></li> </ul> </div> </div> <div class="box bottombar" id="topics"> <h2 title="Qt Topics"><span></span> Qt Topics</h2> <div id="list002" class="list"> <ul id="ul002" > <li class="defaultLink"><a href="qt-basic-concepts.html">Programming with Qt</a></li> <li class="defaultLink"><a href="qtquick.html">Device UIs & Qt Quick</a></li> <li class="defaultLink"><a href="qt-gui-concepts.html">UI Design with Qt</a></li> <li class="defaultLink"><a href="supported-platforms.html">Supported Platforms</a></li> <li class="defaultLink"><a href="technology-apis.html">Qt and Key Technologies</a></li> <li class="defaultLink"><a href="best-practices.html">How-To's and Best Practices</a></li> </ul> </div> </div> <div class="box" id="examples"> <h2 title="Examples"><span></span> Examples</h2> <div id="list003" class="list"> <ul id="ul003"> <li class="defaultLink"><a href="all-examples.html">Examples</a></li> <li class="defaultLink"><a href="tutorials.html">Tutorials</a></li> <li class="defaultLink"><a href="demos.html">Demos</a></li> <li class="defaultLink"><a href="qdeclarativeexamples.html">QML Examples</a></li> </ul> </div> </div> </div> <div class="wrap"> <div class="toolbar"> <div class="breadcrumb toolblock"> <ul> <li class="first"><a href="index.html">Home</a></li> <!-- Breadcrumbs go here --> <li>The Qt Resource System</li> </ul> </div> <div class="toolbuttons toolblock"> <ul> <li id="smallA" class="t_button">A</li> <li id="medA" class="t_button active">A</li> <li id="bigA" class="t_button">A</li> <li id="print" class="t_button"><a href="javascript:this.print();"> <span>Print</span></a></li> </ul> </div> </div> <div class="content mainContent"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#resource-collection-files">Resource Collection Files (<tt>.qrc</tt>)</a></li> <li class="level2"><a href="#external-binary-resources">External Binary Resources</a></li> <li class="level2"><a href="#compiled-in-resources">Compiled-In Resources</a></li> <li class="level1"><a href="#compression">Compression</a></li> <li class="level1"><a href="#using-resources-in-the-application">Using Resources in the Application</a></li> </ul> </div> <h1 class="title">The Qt Resource System</h1> <span class="subtitle"></span> <!-- $$$resources.html-description --> <div class="descr"> <a name="details"></a> <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> <p>The resource system is based on tight cooperation between <a href="qmake-manual.html#qmake">qmake</a>, <a href="rcc.html#rcc">rcc</a> (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.qt.digia.com/qq/qq05-iconography.html#imagestorage">image collection</a> mechanism.</p> <a name="resource-collection-files"></a> <h2>Resource Collection Files (<tt>.qrc</tt>)</h2> <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> <p>Here's an example <tt>.qrc</tt> file:</p> <pre class="cpp"> <!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></pre> <p>The resource files listed in the <tt>.qrc</tt> file are files that are part of the application's source tree. The specified paths are relative to the directory containing the <tt>.qrc</tt> file. Note that the listed resource files must be located in the same directory as the <tt>.qrc</tt> file, or one of its subdirectories.</p> <p>Resource data can either be compiled into the binary and thus accessed immediately in application code, or a binary resource can be created and at a later point in application code registered with the resource system.</p> <p>By default, resources are accessible in the application under the same file name as they have in the source tree, with a <tt>:/</tt> prefix, or by a <a href="qurl.html">URL</a> with a <tt>qrc</tt> scheme.</p> <p>For example, the file path <tt>:/images/cut.png</tt> or the URL <tt>qrc:///images/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> <pre class="cpp"> <file alias="cut-img.png">images/cut.png</file></pre> <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> <pre class="cpp"> <qresource prefix="/myresources"> <file alias="cut-img.png">images/cut.png</file> </qresource></pre> <p>In this case, the file is accessible as <tt>:/myresources/cut-img.png</tt>.</p> <p>Some resources need to change based on the user's locale, such as translation files or icons. This is done by adding a <tt>lang</tt> attribute to the <tt>qresource</tt> tag, specifying a suitable locale string. For example:</p> <pre class="cpp"> <qresource> <file>cut.jpg</file> </qresource> <qresource lang="fr"> <file alias="cut.jpg">cut_fr.jpg</file> </qresource></pre> <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> <p>See the <a href="qlocale.html">QLocale</a> documentation for a description of the format to use for locale strings.</p> <a name="external-binary-resources"></a> <h3>External Binary Resources</h3> <p>For an external binary resource to be created you must create the resource data (commonly given the <tt>.rcc</tt> extension) by passing the -binary switch to <a href="rcc.html#rcc">rcc</a>. Once the binary resource is created you can register the resource with the <a href="qresource.html">QResource</a> API.</p> <p>For example, a set of resource data specified in a <tt>.qrc</tt> file can be compiled in the following way:</p> <pre class="cpp"> rcc -binary myresource.qrc -o myresource.rcc</pre> <p>In the application, this resource would be registered with code like this:</p> <pre class="cpp"> <span class="type"><a href="qresource.html">QResource</a></span><span class="operator">::</span>registerResource(<span class="string">"/path/to/myresource.rcc"</span>);</pre> <a name="compiled-in-resources"></a> <h3>Compiled-In Resources</h3> <p>For a resource to be compiled into the binary 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> <pre class="cpp"> RESOURCES = application.qrc</pre> <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> <p class="centerAlign"><img src="images/resources.png" alt="Building resources into an application" /></p><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> <a name="compression"></a> <h2>Compression</h2> <p>Resources are compressed by default (in the <tt>ZIP</tt> format). It is possible to turn off compression. This can be useful if your resources already contain a compressed format, such as <tt>.png</tt> files. You do this by giving the <tt>-no-compress</tt> command line argument.</p> <pre class="cpp"> rcc <span class="operator">-</span>no<span class="operator">-</span>compress myresources<span class="operator">.</span>qrc</pre> <p><tt>rcc</tt> also gives you some control over the compression. You can specify the compression level and the threshold level to consider while compressing files, for example:</p> <pre class="cpp"> rcc <span class="operator">-</span>compress <span class="number">2</span> <span class="operator">-</span>threshold <span class="number">3</span> myresources<span class="operator">.</span>qrc</pre> <a name="using-resources-in-the-application"></a> <h2>Using Resources in the Application</h2> <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> <pre class="cpp"> cutAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qaction.html">QAction</a></span>(<span class="type"><a href="qicon.html">QIcon</a></span>(<span class="string">":/images/cut.png"</span>)<span class="operator">,</span> tr(<span class="string">"Cu&t"</span>)<span class="operator">,</span> <span class="keyword">this</span>);</pre> <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> <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> <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#addSearchPath">QDir::addSearchPath</a>() to add paths to it.</p> <p>If you have resources in a static library, you might need to force initialization of your resources by calling <a href="qdir.html#Q_INIT_RESOURCE">Q_INIT_RESOURCE</a>() with the base name of the <tt>.qrc</tt> file. For example:</p> <pre class="cpp"> <span class="type">int</span> main(<span class="type">int</span> argc<span class="operator">,</span> <span class="type">char</span> <span class="operator">*</span>argv<span class="operator">[</span><span class="operator">]</span>) { <span class="type"><a href="qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv); Q_INIT_RESOURCE(graphlib); <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> <span class="keyword">return</span> app<span class="operator">.</span>exec(); }</pre> <p>Similarly, if you must unload a set of resources explicitly (because a plugin is being unloaded or the resources are not valid any longer), you can force removal of your resources by calling <a href="qdir.html#Q_CLEANUP_RESOURCE">Q_CLEANUP_RESOURCE</a>() with the same base name as above.</p> </div> <!-- @@@resources.html --> </div> </div> </div> <div class="ft"> <span></span> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2015 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners.</p> <br /> <p> The documentation provided herein is licensed under the terms of the <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation License version 1.3</a> as published by the Free Software Foundation.</p> <p> Documentation sources may be obtained from <a href="http://www.qt-project.org"> www.qt-project.org</a>.</p> <br /> <p> Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners. <a title="Privacy Policy" href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p> </div> <script src="scripts/functions.js" type="text/javascript"></script> </body> </html>