Sophie

Sophie

distrib > Fedora > 18 > i386 > by-pkgid > 5ab010e37991249ab4adaa24d6e39c6e > files > 187

qt5-qtdoc-5.1.1-2.fc18.noarch.rpm

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.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" />
<!-- deployment.qdoc -->
  <title>Deploying QML Applications | QtDoc 5.1</title>
  <link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader"></div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#deploying-qml-based-applications">Deploying QML-based applications</a></li>
<li class="level2"><a href="#deploying-with-qquickview">Deploying with QQuickView</a></li>
<li class="level2"><a href="#creating-a-qqmlengine-directly">Creating a QQmlEngine directly</a></li>
<li class="level1"><a href="#developing-and-prototyping-with-qml-scene">Developing and Prototyping with QML Scene</a></li>
<li class="level1"><a href="#managing-resource-files-with-the-qt-resource-system">Managing resource files with the Qt resource system</a></li>
</ul>
</div>
<h1 class="title">Deploying QML Applications</h1>
<span class="subtitle"></span>
<!-- $$$qtquick-deployment.html-description -->
<div class="descr"> <a name="details"></a>
<p>QML documents are loaded and executed by the QML runtime. This includes the Declarative UI engine along with the built-in QML types and plugin modules, and it also provides access to third-party QML types and modules.</p>
<p>Applications that use QML need to invoke the QML runtime in order to execute QML documents. This can be done by creating a QQuickView or a QQmlEngine, as described below. In addition, the Declarative UI package includes the qmlscene tool, which loads <tt>.qml</tt> files. This tool is useful for developing and testing QML code without the need to write a C++ application to load the QML runtime.</p>
<a name="deploying-qml-based-applications"></a>
<h2>Deploying QML-based applications</h2>
<p>To deploy an application that uses QML, the QML runtime must be invoked by the application. This is done by writing a Qt C++ application that loads the QQmlEngine by either:</p>
<ul>
<li>Loading the QML file through a QQuickView instance, or</li>
<li>Creating a QQmlEngine instance and loading QML files with QQmlComponent</li>
</ul>
<a name="deploying-with-qquickview"></a>
<h3>Deploying with QQuickView</h3>
<p>QQuickView is a QWindow-based class that is able to load QML files. For example, if there is a QML file, <tt>application.qml</tt>, it will look like this:</p>
<pre class="qml">import QtQuick 2.0

<span class="type">Rectangle</span> { <span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">100</span>; <span class="name">color</span>: <span class="string">&quot;red&quot;</span> }</pre>
<p>It can be loaded in a Qt application's <tt>main.cpp</tt> file like this:</p>
<pre class="cpp"><span class="preprocessor">#include &lt;QGuiApplication&gt;</span>
<span class="preprocessor">#include &lt;QQuickView&gt;</span>

<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">QGuiApplication</span> app(argc<span class="operator">,</span> argv);

    <span class="type">QQuickView</span> view;
    view<span class="operator">.</span>setSource(<span class="type">QUrl</span><span class="operator">::</span>fromLocalFile(<span class="string">&quot;application.qml&quot;</span>));
    view<span class="operator">.</span>show();

    <span class="keyword">return</span> app<span class="operator">.</span>exec();
}</pre>
<p>This creates a QWindow-based view that displays the contents of <tt>application.qml</tt>.</p>
<p>The application's <tt>.pro</tt> project file must specify the <tt>declarative</tt> module for the <tt>QT</tt> variable. For example:</p>
<pre class="cpp">TEMPLATE <span class="operator">+</span><span class="operator">=</span> app
QT <span class="operator">+</span><span class="operator">=</span> quick
SOURCES <span class="operator">+</span><span class="operator">=</span> main<span class="operator">.</span>cpp</pre>
<a name="creating-a-qqmlengine-directly"></a>
<h3>Creating a QQmlEngine directly</h3>
<p>If <tt>application.qml</tt> does not have any graphical components, or if it is preferred to avoid QQuickView for other reasons, the QQmlEngine can be constructed directly instead. In this case, <tt>application.qml</tt> is loaded as a QQmlComponent instance rather than placed into a view:</p>
<pre class="cpp"><span class="preprocessor">#include &lt;QGuiApplication&gt;</span>
<span class="preprocessor">#include &lt;QQmlEngine&gt;</span>
<span class="preprocessor">#include &lt;QQmlContext&gt;</span>
<span class="preprocessor">#include &lt;QQmlComponent&gt;</span>

<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">QGuiApplication</span> app(argc<span class="operator">,</span> argv);

    <span class="type">QQmlEngine</span> engine;
    <span class="type">QQmlContext</span> <span class="operator">*</span>objectContext <span class="operator">=</span> <span class="keyword">new</span> <span class="type">QQmlContext</span>(engine<span class="operator">.</span>rootContext());

    <span class="type">QQmlComponent</span> component(<span class="operator">&amp;</span>engine<span class="operator">,</span> <span class="string">&quot;application.qml&quot;</span>);
    <span class="type">QObject</span> <span class="operator">*</span>object <span class="operator">=</span> component<span class="operator">.</span>create(objectContext);

    <span class="comment">// ... delete object and objectContext when necessary</span>

    <span class="keyword">return</span> app<span class="operator">.</span>exec();
}</pre>
<p>QGuiApplication can be replaced by a QCoreApplication in the code above in case you are not using any graphical items from Qt Quick. This allows using QML as a language without any dependencies to the <a href="whatsnew51.html#qt-gui">Qt GUI</a> module.</p>
<p>See qtqml-cppintegration-exposecppattributes.html{Exposing Attributes of C++ Types to QML} for more information about using QQmlEngine, QQmlContext and QQmlComponent, as well as details on including QML files through Qt's Resource system.</p>
<a name="developing-and-prototyping-with-qml-scene"></a>
<h2>Developing and Prototyping with QML Scene</h2>
<p>The Declarative UI package includes a QML runtime tool, <a href="qtquick-qmlscene.html">qmlscene</a>, which loads and displays QML documents. This is useful during the application development phase for prototyping QML-based applications without writing your own C++ applications to invoke the QML runtime.</p>
<a name="managing-resource-files-with-the-qt-resource-system"></a>
<h2>Managing resource files with the Qt resource system</h2>
<p>The Qt resource system allows resource files to be stored as binary files in an application executable. This can be useful when building a mixed QML/C++ application as it enables QML files (as well as other resources such as images and sound files) to be referred to through the resource system URI scheme rather than relative or absolute paths to filesystem resources. Note, however, that if you use the resource system, the application executable must be re-compiled whenever a QML source file is changed in order to update the resources in the package.</p>
<p>To use the resource system in a mixed QML/C++ application:</p>
<ul>
<li>Create a <tt>.qrc</tt> resource collection file that lists resource files in XML format</li>
<li>From C++, load the main QML file as a resource using the <tt>:/</tt> prefix or as a URL with the <tt>qrc</tt> scheme</li>
</ul>
<p>Once this is done, all files specified by relative paths in QML will be loaded from the resource system instead. Use of the resource system is completely transparent to the QML layer; this means all QML code should refer to resource files using relative paths and should <i>not</i> use the <tt>qrc</tt> scheme. This scheme should only be used from C++ code for referring to resource files.</p>
<p>Here is a application packaged using the Qt resource system. The directory structure looks like this:</p>
<pre class="cpp">project
    <span class="operator">|</span><span class="operator">-</span> example<span class="operator">.</span>qrc
    <span class="operator">|</span><span class="operator">-</span> main<span class="operator">.</span>qml
    <span class="operator">|</span><span class="operator">-</span> images
        <span class="operator">|</span><span class="operator">-</span> background<span class="operator">.</span>png
    <span class="operator">|</span><span class="operator">-</span> main<span class="operator">.</span>cpp
    <span class="operator">|</span><span class="operator">-</span> project<span class="operator">.</span>pro</pre>
<p>The <tt>main.qml</tt> and <tt>background.png</tt> files will be packaged as resource files. This is done in the <tt>example.qrc</tt> resource collection file:</p>
<pre class="cpp">&lt;!DOCTYPE RCC&gt;
&lt;RCC version=&quot;1.0&quot;&gt;

&lt;qresource prefix=&quot;/&quot;&gt;
    &lt;file&gt;main.qml&lt;/file&gt;
    &lt;file&gt;images/background.png&lt;/file&gt;
&lt;/qresource&gt;

&lt;/RCC&gt;</pre>
<p>Since <tt>background.png</tt> is a resource file, <tt>main.qml</tt> can refer to it using the relative path specified in <tt>example.qrc</tt>:</p>
<pre class="qml"><span class="comment">// main.qml</span>
import QtQuick 2.0

<span class="type">Image</span> { <span class="name">source</span>: <span class="string">&quot;images/background.png&quot;</span> }</pre>
<p>To allow QML to locate resource files correctly, the <tt>main.cpp</tt> loads the main QML file, <tt>main.qml</tt>, as a resource file using the <tt>qrc</tt> scheme:</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">QApplication</span> app(argc<span class="operator">,</span> argv);

    <span class="type">QQuickView</span> view;
    view<span class="operator">.</span>setSource(<span class="type">QUrl</span>(<span class="string">&quot;qrc:/main.qml&quot;</span>));
    view<span class="operator">.</span>show();

    <span class="keyword">return</span> app<span class="operator">.</span>exec();
}</pre>
<p>Finally, <tt>project.pro</tt> uses the RESOURCES variable to indicate that <tt>example.qrc</tt> should be used to build the application resources:</p>
<pre class="cpp">QT += qml

SOURCES += main.cpp
RESOURCES += example.qrc</pre>
<p>See The Qt Resource System for more information.</p>
</div>
<!-- @@@qtquick-deployment.html -->
</div>
</div>
</div>
<div class="footer">
    <p>
      <acronym title="Copyright">&copy;</acronym> 2013 Digia Plc and/or its
      subsidiaries. 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>
      Digia, Qt and their respective logos are trademarks of Digia Plc 
      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>
</body>
</html>