Sophie

Sophie

distrib > Mageia > 6 > i586 > by-pkgid > f93881942bd3805980c2fe63aa853d78 > files > 210

qtdoc5-5.9.4-1.mga6.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" />
<!-- firststepsqml.qdoc -->
  <title>First Steps with QML | Qt 5.9</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 ><a href="index.html">Qt 5.9</a></td><td >First Steps with QML</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.4 Reference Documentation</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="#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="#creating-and-running-qml-projects">Creating and Running QML Projects</a></li>
<li class="level1"><a href="#creating-qml-applications-with-controls">Creating QML Applications with Controls</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>
<div class="sidebar-content" id="sidebar-content"></div></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 id="creating-a-qml-document">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 <code>QtQuick</code> import.</p>
<a name="importing-and-using-the-qtquick-module"></a>
<h3 >Importing and Using the QtQuick Module</h3>
<p>To use the Qt Quick 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.3</span>

</pre>
<p>The types and functionality that Qt Quick 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. Qt Quick 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 <code>parent</code> 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 <code>children</code> 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 <code>QtQuick</code> import. Putting the import and object declaration together, we get a complete QML document:</p>
<pre class="qml">

  import QtQuick 2.3

  <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="creating-and-running-qml-projects"></a>
<h2 id="creating-and-running-qml-projects">Creating and Running QML Projects</h2>
<p>To display the graphical scene defined by the QML document, it may be loaded with <a href="http://doc.qt.io/qtcreator/index.html">Qt Creator</a>. For simple UI files such as this one, select <b>File &gt; New File or Project &gt; Applications &gt; Qt Quick UI</b> from within Qt Creator.</p>
<p>Pressing the green <b>Run</b> button runs the application. You should see the text <b>Hello, World!</b> in the center of a red rectangle.</p>
<p>For more information about creating and running projects in Qt Creator, visit the following pages:</p>
<ul>
<li><a href="http://doc.qt.io/qtcreator/quick-projects.html">Creating Qt Quick Projects</a></li>
<li><a href="http://doc.qt.io/qtcreator/creator-build-example-application.html">Building and Running an Example</a></li>
</ul>
<a name="creating-qml-applications-with-controls"></a>
<h2 id="creating-qml-applications-with-controls">Creating QML Applications with Controls</h2>
<p>While Qt Quick provides basic graphical elements, Qt Quick Controls provides ready-made QML types for use within an application.</p>
<p>Inserting the ApplicationWindow type is a good starting point for creating applications. An application UI has this basic layout:</p>
<p class="centerAlign"><img src="images/applicationwindow.png" alt="" /></p><p>Within each area, different <i>controls</i> may be added and connected to form an application. For example, the following snippet is a basic application that uses the previous layout:</p>
<pre class="qml">

  <span class="comment">//import related modules</span>
  import QtQuick 2.3
  import QtQuick.Controls 1.2
  import QtQuick.Window 2.2

  <span class="comment">//window containing the application</span>
  <span class="type">ApplicationWindow</span> {

      <span class="comment">//title of the application</span>
      <span class="name">title</span>: <span class="name">qsTr</span>(<span class="string">&quot;Hello World&quot;</span>)
      <span class="name">width</span>: <span class="number">640</span>
      <span class="name">height</span>: <span class="number">480</span>

      <span class="comment">//menu containing two menu items</span>
      <span class="name">menuBar</span>: <span class="name">MenuBar</span> {
          <span class="type">Menu</span> {
              <span class="name">title</span>: <span class="name">qsTr</span>(<span class="string">&quot;File&quot;</span>)
              <span class="type">MenuItem</span> {
                  <span class="name">text</span>: <span class="name">qsTr</span>(<span class="string">&quot;&amp;Open&quot;</span>)
                  <span class="name">onTriggered</span>: <span class="name">console</span>.<span class="name">log</span>(<span class="string">&quot;Open action triggered&quot;</span>);
              }
              <span class="type">MenuItem</span> {
                  <span class="name">text</span>: <span class="name">qsTr</span>(<span class="string">&quot;Exit&quot;</span>)
                  <span class="name">onTriggered</span>: <span class="name">Qt</span>.<span class="name">quit</span>();
              }
          }
      }

      <span class="comment">//Content Area</span>

      <span class="comment">//a button in the middle of the content area</span>
      <span class="type">Button</span> {
          <span class="name">text</span>: <span class="name">qsTr</span>(<span class="string">&quot;Hello World&quot;</span>)
          <span class="name">anchors</span>.horizontalCenter: <span class="name">parent</span>.<span class="name">horizontalCenter</span>
          <span class="name">anchors</span>.verticalCenter: <span class="name">parent</span>.<span class="name">verticalCenter</span>
      }
  }

</pre>
<p>The application has two menu items and a button in the middle. Clicking on the <b>Exit</b> menu item closes the application.</p>
<p>There are also different navigation methods and different controls such as buttons and sliders available. The following examples are available from Qt Creator and demonstrate different controls and layouts.</p>
<ul>
<li>Basic Layouts</li>
<li>Touch Gallery</li>
</ul>
<p>Feel free to copy and paste the snippets onto this simple Hellow World application to see how QML works.</p>
<a name="handling-user-input"></a>
<h2 id="handling-user-input">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">

  <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">

  <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 id="property-bindings">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">

  <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 id="animations">Animations</h2>
<p>Properties can also be dynamically updated via animations. The <code>QtQuick</code> 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">

  <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">parent</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 id="defining-custom-qml-types-for-re-use">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 <code>Button</code> type in the <code>Button.qml</code> file:</p>
<pre class="qml">

  <span class="comment">// Button.qml</span>
  import QtQuick 2.3

  <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>
<div class="table"><table class="generic">
 <tr valign="top" class="odd"><td ><pre class="qml">

  <span class="comment">// application.qml</span>
  import QtQuick 2.3

  <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></div>
<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 id="where-to-go-from-here">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="qmlapplications.html">QML Applications</a></li>
<li><a href="qtexamplesandtutorials.html">Qt Examples and Tutorials</a></li>
</ul>
</div>
<!-- @@@qmlfirststeps.html -->
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2017 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>