Sophie

Sophie

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

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" />
<!-- firststepsqml.qdoc -->
  <title>First Steps with QML | 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="#creating-a-qml-document">Creating a QML Document</a></li>
<li class="level2"><a href="#importing-and-using-the-qtquick-module">Importing and Using the QtQuick Module</a></li>
<li class="level2"><a href="#defining-an-object-hierarchy">Defining an Object Hierarchy</a></li>
<li class="level2"><a href="#putting-it-all-together">Putting it All Together</a></li>
<li class="level1"><a href="#loading-and-displaying-the-qml-document">Loading and Displaying the QML Document</a></li>
<li class="level1"><a href="#handling-user-input">Handling User Input</a></li>
<li class="level1"><a href="#property-bindings">Property Bindings</a></li>
<li class="level1"><a href="#animations">Animations</a></li>
<li class="level1"><a href="#defining-custom-qml-types-for-re-use">Defining Custom QML Types for Re-use</a></li>
<li class="level1"><a href="#where-to-go-from-here">Where to Go from Here</a></li>
</ul>
</div>
<h1 class="title">First Steps with QML</h1>
<span class="subtitle"></span>
<!-- $$$qmlfirststeps.html-description -->
<div class="descr"> <a name="details"></a>
<a name="creating-a-qml-document"></a>
<h2>Creating a QML Document</h2>
<p>A QML document defines a hierarchy of objects with a highly-readable, structured layout. Every QML document consists of two parts: an imports section and an object declaration section. The types and functionality most common to user interfaces are provided in the <tt>QtQuick</tt> import.</p>
<a name="importing-and-using-the-qtquick-module"></a>
<h3>Importing and Using the QtQuick Module</h3>
<p>To use the <a href="whatsnew51.html#qt-quick">Qt Quick</a> module, a QML document needs to import it. The import syntax looks like this:</p>
<pre class="cpp">import <span class="type">QtQuick</span> <span class="number">2.0</span></pre>
<p>The types and functionality that <a href="whatsnew51.html#qt-quick">Qt Quick</a> provides can now be used in the QML document!</p>
<a name="defining-an-object-hierarchy"></a>
<h3>Defining an Object Hierarchy</h3>
<p>The object declaration in a QML document defines what will be displayed in the visual scene. <a href="whatsnew51.html#qt-quick">Qt Quick</a> provides the basic building blocks for all user interfaces, such as the objects for displaying images and text and for handling user input.</p>
<p>A simple object declaration might be a colored rectangle with some text centered in it:</p>
<pre class="qml"><span class="type">Rectangle</span> {
    <span class="name">width</span>: <span class="number">200</span>
    <span class="name">height</span>: <span class="number">100</span>
    <span class="name">color</span>: <span class="string">&quot;red&quot;</span>

    <span class="type">Text</span> {
        <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
        <span class="name">text</span>: <span class="string">&quot;Hello, World!&quot;</span>
    }
}</pre>
<p>This defines an object hierarchy with a root Rectangle object which has a child <a href="whatsnew50.html#text">Text</a> object. The <tt>parent</tt> of the <a href="whatsnew50.html#text">Text</a> object is automatically set to the Rectangle, and similarly, the <a href="whatsnew50.html#text">Text</a> object is added to the <tt>children</tt> property of the Rectangle object, by QML.</p>
<a name="putting-it-all-together"></a>
<h3>Putting it All Together</h3>
<p>The Rectangle and <a href="whatsnew50.html#text">Text</a> types used in the above example are both provided by the <tt>QtQuick</tt> import. Putting the import and object declaration together, we get a complete QML document:</p>
<pre class="qml">import QtQuick 2.0

<span class="type">Rectangle</span> {
    <span class="name">width</span>: <span class="number">200</span>
    <span class="name">height</span>: <span class="number">100</span>
    <span class="name">color</span>: <span class="string">&quot;red&quot;</span>

    <span class="type">Text</span> {
        <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
        <span class="name">text</span>: <span class="string">&quot;Hello, World!&quot;</span>
    }
}</pre>
<p>If we save that document as &quot;HelloWorld.qml&quot;, we can load and display it.</p>
<a name="loading-and-displaying-the-qml-document"></a>
<h2>Loading and Displaying the QML Document</h2>
<p>To display the graphical scene defined by the QML document, it may be loaded with the <a href="qtquick-qmlscene.html">qmlscene</a> tool. The <a href="qtquick-qmlscene.html">qmlscene</a> tool should be installed into the Qt installation directory. Assuming that the Qt binaries are installed into or are available in the system executable path, you can display the QML document with the following command:</p>
<pre class="cpp">qmlscene HelloWorld<span class="operator">.</span>qml</pre>
<p>You should see the text &quot;Hello, World!&quot; in the center of a red rectangle.</p>
<a name="handling-user-input"></a>
<h2>Handling User Input</h2>
<p>One of the great advantages of using QML to define a user interface is that it allows the user interface designer to define how the application should react to events with simple JavaScript expressions. In QML, we refer to those events as signals and these signals are handled by signal handlers.</p>
<p>For example, consider the following example:</p>
<pre class="qml">import QtQuick 2.0

<span class="type">Rectangle</span> {
    <span class="name">width</span>: <span class="number">200</span>
    <span class="name">height</span>: <span class="number">100</span>
    <span class="name">color</span>: <span class="string">&quot;red&quot;</span>

    <span class="type">Text</span> {
        <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
        <span class="name">text</span>: <span class="string">&quot;Hello, World!&quot;</span>
    }

    <span class="type">MouseArea</span> {
        <span class="name">anchors</span>.fill: <span class="name">parent</span>
        <span class="name">onClicked</span>: <span class="name">parent</span>.<span class="name">color</span> <span class="operator">=</span> <span class="string">&quot;blue&quot;</span>
    }
}</pre>
<p>This example can be saved as &quot;ClickableHelloWorld.qml&quot; and run with qmlscene. Whenever the user clicks anywhere in the window, the rectangle will change from red to blue. Note that the MouseArea type also emits the clicked signal for touch events, so this code will also work on a mobile device.</p>
<p>Keyboard user input can be similarly handled with a simple expression:</p>
<pre class="qml">import QtQuick 2.0

<span class="type">Rectangle</span> {
    <span class="name">width</span>: <span class="number">200</span>
    <span class="name">height</span>: <span class="number">100</span>
    <span class="name">color</span>: <span class="string">&quot;red&quot;</span>

    <span class="type">Text</span> {
        <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
        <span class="name">text</span>: <span class="string">&quot;Hello, World!&quot;</span>
    }

    <span class="name">focus</span>: <span class="number">true</span>
    <span class="name">Keys</span>.onPressed: {
        <span class="keyword">if</span> (<span class="name">event</span>.<span class="name">key</span> <span class="operator">==</span> <span class="name">Qt</span>.<span class="name">Key_Return</span>) {
            <span class="name">color</span> <span class="operator">=</span> <span class="string">&quot;blue&quot;</span>;
            <span class="name">event</span>.<span class="name">accepted</span> <span class="operator">=</span> <span class="number">true</span>;
        }
    }
}</pre>
<p>By accepting focus, the color can be changed to blue whenever the return key is pressed.</p>
<a name="property-bindings"></a>
<h2>Property Bindings</h2>
<p>Objects and their properties form the basis of a graphical interface defined in a QML document. The QML language allows properties to be bound to each other in various ways, enabling highly dynamic user interfaces.</p>
<p>In the following example, the geometry of each child Rectangle is bound to that of the parent Rectangle. If the geometry of the parent Rectangle were to change, the geometry of each child Rectangle would automatically update due to the property bindings.</p>
<pre class="qml">import QtQuick 2.0

<span class="type">Rectangle</span> {
    <span class="name">width</span>: <span class="number">400</span>
    <span class="name">height</span>: <span class="number">200</span>

    <span class="type">Rectangle</span> {
        <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>
        <span class="name">height</span>: <span class="name">parent</span>.<span class="name">height</span>
    }

    <span class="type">Rectangle</span> {
        <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>
        <span class="name">height</span>: <span class="name">parent</span>.<span class="name">height</span>
        <span class="name">x</span>: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">2</span>
    }
}</pre>
<a name="animations"></a>
<h2>Animations</h2>
<p>Properties can also be dynamically updated via animations. The <tt>QtQuick</tt> import provides various animation types which can be used to animate changes to a property's value. In the following example, a property is animated which then gets displayed in a <a href="whatsnew50.html#text">Text</a> area:</p>
<pre class="qml">import QtQuick 2.0

<span class="type">Rectangle</span> {
    <span class="name">color</span>: <span class="string">&quot;lightgray&quot;</span>
    <span class="name">width</span>: <span class="number">200</span>
    <span class="name">height</span>: <span class="number">200</span>

    property <span class="type">int</span> <span class="name">animatedValue</span>: <span class="number">0</span>
    SequentialAnimation on <span class="name">animatedValue</span> {
        <span class="name">loops</span>: <span class="name">Animation</span>.<span class="name">Infinite</span>
        <span class="type">PropertyAnimation</span> { <span class="name">to</span>: <span class="number">150</span>; <span class="name">duration</span>: <span class="number">1000</span> }
        <span class="type">PropertyAnimation</span> { <span class="name">to</span>: <span class="number">0</span>; <span class="name">duration</span>: <span class="number">1000</span> }
    }

    <span class="type">Text</span> {
        <span class="name">anchors</span>.centerIn: <span class="name">parent</span>
        <span class="name">text</span>: <span class="name">animatedValue</span>
    }
}</pre>
<p>The value being displayed will vary from 0 to 150 periodically.</p>
<a name="defining-custom-qml-types-for-re-use"></a>
<h2>Defining Custom QML Types for Re-use</h2>
<p>One of the most important concepts in QML is that of type re-use. An application will probably have multiple visual types which are all similar (for example, multiple push buttons), and QML allows these sort of things to be defined as re-usable, custom types, to minimize code duplication and maximize readability.</p>
<p>For example, imagine that the developer defines a new <tt>Button</tt> type in the <tt>Button.qml</tt> file:</p>
<pre class="qml"><span class="comment">// Button.qml</span>
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>

    <span class="type">MouseArea</span> {
        <span class="name">anchors</span>.fill: <span class="name">parent</span>
        <span class="name">onClicked</span>: <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;Button clicked!&quot;</span>)
    }
}</pre>
<p>That type may now be re-used multiple times in the application, as follows:</p>
<table class="generic">
 <tr valign="top" class="odd"><td ><pre class="qml"><span class="comment">// application.qml</span>
import QtQuick 2.0

<span class="type">Column</span> {
    <span class="type">Button</span> { <span class="name">width</span>: <span class="number">50</span>; <span class="name">height</span>: <span class="number">50</span> }
    <span class="type">Button</span> { <span class="name">x</span>: <span class="number">50</span>; <span class="name">width</span>: <span class="number">100</span>; <span class="name">height</span>: <span class="number">50</span>; <span class="name">color</span>: <span class="string">&quot;blue&quot;</span> }
    <span class="type">Button</span> { <span class="name">width</span>: <span class="number">50</span>; <span class="name">height</span>: <span class="number">50</span>; <span class="name">radius</span>: <span class="number">8</span> }
}</pre>
</td><td ><p class="centerAlign"><img src="images/qml-extending-types.png" alt="" /></p></td></tr>
</table>
<p>In this way, modular user interface types are assembled and reused within an application.</p>
<p>See QML Object Attributes for more details on how to develop your own reusable components.</p>
<a name="where-to-go-from-here"></a>
<h2>Where to Go from Here</h2>
<p>Now that you have seen QML in action, you are ready to take your next step. The follow page will lead you in your journey with QML.</p>
<ul>
<li><a href="qtquick-applicationdevelopers.html">QML Application Developer Resources</a></li>
</ul>
</div>
<!-- @@@qmlfirststeps.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>