<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <!-- tutorial.qdoc --> <title>QML Tutorial 1 - Basic Types | Qt Quick 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 >Qt 5.9</td><td ><a href="qtquick-index.html">Qt Quick</a></td><td >QML Tutorial 1 - Basic Types</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"> <link rel="prev" href="qml-tutorial.html" /> <link rel="next" href="qml-tutorial2.html" /> <p class="naviNextPrevious headerNavi"> <a class="prevPage" href="qml-tutorial.html">QML Tutorial</a> <span class="naviSeparator"> ◦ </span> <a class="nextPage" href="qml-tutorial2.html">QML Tutorial 2 - QML Components</a> </p><p/> <div class="sidebar"> <div class="toc"> <h3><a name="toc">Contents</a></h3> <ul> <li class="level1"><a href="#walkthrough">Walkthrough</a></li> <li class="level2"><a href="#import">Import</a></li> <li class="level2"><a href="#rectangle-type">Rectangle Type</a></li> <li class="level2"><a href="#text-type">Text Type</a></li> <li class="level2"><a href="#viewing-the-example">Viewing the Example</a></li> </ul> </div> <div class="sidebar-content" id="sidebar-content"></div></div> <h1 class="title">QML Tutorial 1 - Basic Types</h1> <span class="subtitle"></span> <!-- $$$qml-tutorial1.html-description --> <div class="descr"> <a name="details"></a> <p>This first program is a very simple "Hello world" example that introduces some basic QML concepts. The picture below is a screenshot of this program.</p> <p class="centerAlign"><img src="images/declarative-tutorial1.png" alt="" /></p><p>Here is the QML code for the application:</p> <pre class="qml"> import QtQuick 2.0 <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">page</span> <span class="name">width</span>: <span class="number">320</span>; <span class="name">height</span>: <span class="number">480</span> <span class="name">color</span>: <span class="string">"lightgray"</span> <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">id</span>: <span class="name">helloText</span> <span class="name">text</span>: <span class="string">"Hello world!"</span> <span class="name">y</span>: <span class="number">30</span> <span class="name">anchors</span>.horizontalCenter: <span class="name">page</span>.<span class="name">horizontalCenter</span> <span class="name">font</span>.pointSize: <span class="number">24</span>; <span class="name">font</span>.bold: <span class="number">true</span> } } </pre> <a name="walkthrough"></a> <h2 id="walkthrough">Walkthrough</h2> <a name="import"></a> <h3 >Import</h3> <p>First, we need to import the types that we need for this example. Most QML files will import the built-in QML types (like <a href="qml-qtquick-rectangle.html">Rectangle</a>, <a href="qtquick-imageelements-example.html#image">Image</a>, ...) that come with Qt, using:</p> <pre class="qml"> import QtQuick 2.0 </pre> <a name="rectangle-type"></a> <h3 >Rectangle Type</h3> <pre class="qml"> <span class="type"><a href="qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">page</span> <span class="name">width</span>: <span class="number">320</span>; <span class="name">height</span>: <span class="number">480</span> <span class="name">color</span>: <span class="string">"lightgray"</span> </pre> <p>We declare a root object of type <a href="qml-qtquick-rectangle.html">Rectangle</a>. It is one of the basic building blocks you can use to create an application in QML. We give it an <code>id</code> to be able to refer to it later. In this case, we call it "page". We also set the <code>width</code>, <code>height</code> and <code>color</code> properties. The <a href="qml-qtquick-rectangle.html">Rectangle</a> type contains many other properties (such as <code>x</code> and <code>y</code>), but these are left at their default values.</p> <a name="text-type"></a> <h3 >Text Type</h3> <pre class="qml"> <span class="type"><a href="qml-qtquick-text.html">Text</a></span> { <span class="name">id</span>: <span class="name">helloText</span> <span class="name">text</span>: <span class="string">"Hello world!"</span> <span class="name">y</span>: <span class="number">30</span> <span class="name">anchors</span>.horizontalCenter: <span class="name">page</span>.<span class="name">horizontalCenter</span> <span class="name">font</span>.pointSize: <span class="number">24</span>; <span class="name">font</span>.bold: <span class="number">true</span> } </pre> <p>We add a <a href="qml-qtquick-text.html">Text</a> type as a child of the root Rectangle type that displays the text 'Hello world!'.</p> <p>The <code>y</code> property is used to position the text vertically at 30 pixels from the top of its parent.</p> <p>The <code>anchors.horizontalCenter</code> property refers to the horizontal center of an type. In this case, we specify that our text type should be horizontally centered in the <i>page</i> element (see <a href="qtquick-positioning-anchors.html">Anchor-Based Layout</a>).</p> <p>The <code>font.pointSize</code> and <code>font.bold</code> properties are related to fonts and use the dot notation.</p> <a name="viewing-the-example"></a> <h3 >Viewing the Example</h3> <p>To view what you have created, run the qmlscene tool (located in the <code>bin</code> directory) with your filename as the first argument. For example, to run the provided completed Tutorial 1 example from the install location, you would type:</p> <pre class="cpp"> qmlscene tutorials<span class="operator">/</span>helloworld<span class="operator">/</span>tutorial1<span class="operator">.</span>qml </pre> </div> <!-- @@@qml-tutorial1.html --> <p class="naviNextPrevious footerNavi"> <a class="prevPage" href="qml-tutorial.html">QML Tutorial</a> <span class="naviSeparator"> ◦ </span> <a class="nextPage" href="qml-tutorial2.html">QML Tutorial 2 - QML Components</a> </p> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</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>