Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > d5e62c01ae8d1e579463c6a871dd44bf > files > 990

qtbase5-doc-5.12.6-2.mga7.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- resource-system.qdoc -->
  <title>The Qt Resource System | Qt Core 5.12.6</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td >Qt 5.12</td><td ><a href="qtcore-index.html">Qt Core</a></td><td >The Qt Resource System</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtcore-index.html">Qt 5.12.6 Reference Documentation</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#resource-collection-files-op-op-qrc">Resource Collection Files (<code>.qrc</code>)</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>
<li class="level1"><a href="#using-resources-in-a-library">Using Resources in a Library</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">The Qt Resource System</h1>
<span class="subtitle"></span>
<!-- $$$resources.html-description -->
<div class="descr"> <a name="details"></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/qmake-manual.html">qmake</a>, rcc (Qt's resource compiler), and <a href="qfile.html">QFile</a>.</p>
<a name="resource-collection-files-op-op-qrc"></a>
<h2 id="resource-collection-files-op-op-qrc">Resource Collection Files (<code>.qrc</code>)</h2>
<a name="resource-collection-files"></a><p>The resources associated with an application are specified in a <code>.qrc</code> 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 <code>.qrc</code> file:</p>
<pre class="cpp">

  &lt;!DOCTYPE RCC&gt;&lt;RCC version=&quot;1.0&quot;&gt;
  &lt;qresource&gt;
      &lt;file&gt;images/copy.png&lt;/file&gt;
      &lt;file&gt;images/cut.png&lt;/file&gt;
      &lt;file&gt;images/new.png&lt;/file&gt;
      &lt;file&gt;images/open.png&lt;/file&gt;
      &lt;file&gt;images/paste.png&lt;/file&gt;
      &lt;file&gt;images/save.png&lt;/file&gt;
  &lt;/qresource&gt;
  &lt;/RCC&gt;

</pre>
<p>The resource files listed in the <code>.qrc</code> file are files that are part of the application's source tree. The specified paths are relative to the directory containing the <code>.qrc</code> file. Note that the listed resource files must be located in the same directory as the <code>.qrc</code> 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 <code>:/</code> prefix, or by a <a href="qurl.html">URL</a> with a <code>qrc</code> scheme.</p>
<p>For example, the file path <code>:/images/cut.png</code> or the URL <code>qrc:///images/cut.png</code> would give access to the <code>cut.png</code> file, whose location in the application's source tree is <code>images/cut.png</code>. This can be changed using the <code>file</code> tag's <code>alias</code> attribute:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>file alias<span class="operator">=</span><span class="string">&quot;cut-img.png&quot;</span><span class="operator">&gt;</span>images<span class="operator">/</span>cut<span class="operator">.</span>png<span class="operator">&lt;</span><span class="operator">/</span>file<span class="operator">&gt;</span>

</pre>
<p>The file is then accessible as <code>:/cut-img.png</code> from the application. It is also possible to specify a path prefix for all files in the <code>.qrc</code> file using the <code>qresource</code> tag's <code>prefix</code> attribute:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>qresource prefix<span class="operator">=</span><span class="string">&quot;/myresources&quot;</span><span class="operator">&gt;</span>
      <span class="operator">&lt;</span>file alias<span class="operator">=</span><span class="string">&quot;cut-img.png&quot;</span><span class="operator">&gt;</span>images<span class="operator">/</span>cut<span class="operator">.</span>png<span class="operator">&lt;</span><span class="operator">/</span>file<span class="operator">&gt;</span>
  <span class="operator">&lt;</span><span class="operator">/</span>qresource<span class="operator">&gt;</span>

</pre>
<p>In this case, the file is accessible as <code>:/myresources/cut-img.png</code>.</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 <code>lang</code> attribute to the <code>qresource</code> tag, specifying a suitable locale string. For example:</p>
<pre class="cpp">

  <span class="operator">&lt;</span>qresource<span class="operator">&gt;</span>
      <span class="operator">&lt;</span>file<span class="operator">&gt;</span>cut<span class="operator">.</span>jpg<span class="operator">&lt;</span><span class="operator">/</span>file<span class="operator">&gt;</span>
  <span class="operator">&lt;</span><span class="operator">/</span>qresource<span class="operator">&gt;</span>
  <span class="operator">&lt;</span>qresource lang<span class="operator">=</span><span class="string">&quot;fr&quot;</span><span class="operator">&gt;</span>
      <span class="operator">&lt;</span>file alias<span class="operator">=</span><span class="string">&quot;cut.jpg&quot;</span><span class="operator">&gt;</span>cut_fr<span class="operator">.</span>jpg<span class="operator">&lt;</span><span class="operator">/</span>file<span class="operator">&gt;</span>
  <span class="operator">&lt;</span><span class="operator">/</span>qresource<span class="operator">&gt;</span>

</pre>
<p>If the user's locale is French (i.e&#x2e;, <a href="qlocale.html#system">QLocale::system</a>().name() returns &quot;fr_FR&quot;), <code>:/cut.jpg</code> becomes a reference to the <code>cut_fr.jpg</code> image. For other locales, <code>cut.jpg</code> 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 id="external-binary-resources">External Binary Resources</h3>
<p>For an external binary resource to be created you must create the resource data (commonly given the <code>.rcc</code> extension) by passing the -binary switch to rcc. 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 <code>.qrc</code> file can be compiled in the following way:</p>
<pre class="cpp">

  rcc <span class="operator">-</span>binary myresource<span class="operator">.</span>qrc <span class="operator">-</span>o myresource<span class="operator">.</span>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">&quot;/path/to/myresource.rcc&quot;</span>);

</pre>
<a name="compiled-in-resources"></a>
<h3 id="compiled-in-resources">Compiled-In Resources</h3>
<p>For a resource to be compiled into the binary the <code>.qrc</code> file must be mentioned in the application's <code>.pro</code> file so that <code>qmake</code> knows about it. For example:</p>
<pre class="cpp">

  RESOURCES     = application.qrc

</pre>
<p><code>qmake</code> will produce make rules to generate a file called <code>qrc_application.cpp</code> 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 <code>qrc_application.cpp</code> file is automatically regenerated whenever the <code>.qrc</code> file changes or one of the files that it refers to changes. If you don't use <code>.pro</code> files, you can either invoke <code>rcc</code> 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, macOS, and iOS, where the operating system provides native support for resources. This might change in a future Qt release.</p>
<a name="compression"></a>
<h2 id="compression">Compression</h2>
<p>Resources are compressed by default (in the <code>ZIP</code> format). It is possible to turn off compression. This can be useful if your resources already contain a compressed format, such as <code>.png</code> files. You do this by giving the <code>-no-compress</code> 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><code>rcc</code> 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 id="using-resources-in-the-application">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="../qtgui/qicon.html">QIcon</a>, <a href="../qtgui/qimage.html">QImage</a>, or <a href="../qtgui/qpixmap.html">QPixmap</a> constructor:</p>
<pre class="cpp">

      cutAct <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="../qtwidgets/qaction.html">QAction</a></span>(<span class="type"><a href="../qtgui/qicon.html">QIcon</a></span>(<span class="string">&quot;:/images/cut.png&quot;</span>)<span class="operator">,</span> tr(<span class="string">&quot;Cu&amp;t&quot;</span>)<span class="operator">,</span> <span class="keyword">this</span>);

</pre>
<p>See the <a href="../qtwidgets/qtwidgets-mainwindows-application-example.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 &quot;:/&quot; 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 <code>:</code> instead of <code>:/</code> 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>
<a name="using-resources-in-a-library"></a>
<h2 id="using-resources-in-a-library">Using Resources in a Library</h2>
<p>If you have resources in a library, you 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 <code>.qrc</code> file. For example:</p>
<pre class="cpp">

  MyClass<span class="operator">::</span>MyClass() : BaseClass()
  {
      Q_INIT_RESOURCE(resources);

      <span class="type"><a href="qfile.html">QFile</a></span> file(<span class="string">&quot;:/myfile.dat&quot;</span>);
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  }

</pre>
<p>This ensures that the resources are linked into the final application binary in the case of static linking. You should put the initialization code close to where the resources are used in your library, so that clients of your library will only link in the resources if they use the feature of the library that depends on them.</p>
<p>Note: As the resource initializers generated by rcc are declared in the global namespace, your calls to <a href="qdir.html#Q_INIT_RESOURCE">Q_INIT_RESOURCE</a>() also need to be done outside of any namespace.</p>
<p>If the library includes resources that are not used internally, but instead exposed to clients of the library, the initialization needs to happen in the application code. 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="../qtwidgets/qapplication.html">QApplication</a></span> app(argc<span class="operator">,</span> argv);
      Q_INIT_RESOURCE(graphlib);

      <span class="type"><a href="qfile.html">QFile</a></span> file(<span class="string">&quot;:/graph.png&quot;</span>);
      <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>As before, this ensures that the resources are linked into the final application binary in the case of static linking, but also triggers loading of the library in the case of dynamic linking, such as plugins.</p>
<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>
<p>Note: The use of <a href="qdir.html#Q_INIT_RESOURCE">Q_INIT_RESOURCE</a>() and <a href="qdir.html#Q_CLEANUP_RESOURCE">Q_CLEANUP_RESOURCE</a>() is not necessary when the resource is built as part of the application.</p>
</div>
<!-- @@@resources.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    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.<br/>    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. </p>
</div>
</body>
</html>