Sophie

Sophie

distrib > Mageia > 7 > i586 > media > core-updates > by-pkgid > 6e2327ca1c896c6d674ae53117299f21 > files > 201

qtdeclarative5-doc-5.12.6-1.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" />
<!-- definetypes.qdoc -->
  <title>Defining Object Types through QML Documents | Qt QML 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="qtqml-index.html">Qt QML</a></td><td >Defining Object Types through QML Documents</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-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="#defining-an-object-type-with-a-qml-file">Defining an Object Type with a QML File</a></li>
<li class="level2"><a href="#naming-custom-qml-object-types">Naming Custom QML Object Types</a></li>
<li class="level2"><a href="#custom-qml-type-definition">Custom QML Type Definition</a></li>
<li class="level2"><a href="#importing-types-defined-outside-the-current-directory">Importing Types Defined Outside the Current Directory</a></li>
<li class="level1"><a href="#accessible-attributes-of-custom-types">Accessible Attributes of Custom Types</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Defining Object Types through QML Documents</h1>
<span class="subtitle"></span>
<!-- $$$qtqml-documents-definetypes.html-description -->
<div class="descr"> <a name="details"></a>
<p>One of the core features of QML is that it enables QML object types to be easily defined in a lightweight manner through QML documents to suit the needs of individual QML applications. The standard Qt Quick module provides various types like Rectangle, Text and Image for building a QML application; beyond these, you can easily define your own QML types to be reused within your application. This ability to create your own types forms the building blocks of any QML application.</p>
<a name="defining-an-object-type-with-a-qml-file"></a>
<h2 id="defining-an-object-type-with-a-qml-file">Defining an Object Type with a QML File</h2>
<a name="naming-custom-qml-object-types"></a>
<h3 id="naming-custom-qml-object-types">Naming Custom QML Object Types</h3>
<p>To create an object type, a QML document should be placed into a text file named as <i>&lt;TypeName&gt;.qml</i> where <i>&lt;TypeName&gt;</i> is the desired name of the type. The type name has the following requirements:</p>
<ul>
<li>It must be comprised of alphanumeric characters or underscores.</li>
<li>It must begin with an uppercase letter.</li>
</ul>
<p>This document is then automatically recognized by the engine as a definition of a QML type. Additionally, a type defined in this manner is automatically made available to other QML files within the same directory as the engine searches within the immediate directory when resolving QML type names.</p>
<a name="custom-qml-type-definition"></a>
<h3 id="custom-qml-type-definition">Custom QML Type Definition</h3>
<p>For example, below is a document that declares a Rectangle with a child MouseArea. The document has been saved to file named <code>SquareButton.qml</code>:</p>
<pre class="qml">



</pre>
<p>Since the file is named <code>SquareButton.qml</code>, <b>this can now be used as a type named <code>SquareButton</code> by any other QML file within the same directory</b>. For example, if there was a <code>myapplication.qml</code> file in the same directory, it could refer to the <code>SquareButton</code> type:</p>
<pre class="qml">



</pre>
<p class="centerAlign"><img src="images/documents-definetypes-simple.png" alt="" /></p><p>This creates a 100 x 100 red Rectangle with an inner MouseArea, as defined in <code>SquareButton.qml</code>. When this <code>myapplication.qml</code> document is loaded by the engine, it loads the SquareButton.qml document as a component and instantiates it to create a <code>SquareButton</code> object.</p>
<p>The <code>SquareButton</code> type encapsulates the tree of QML objects declared in <code>SquareButton.qml</code>. When the QML engine instantiates a <code>SquareButton</code> object from this type, it is instantiating an object from the Rectangle tree declared in <code>SquareButton.qml</code>.</p>
<p><b>Note: </b>the letter case of the file name is significant on some (notably UNIX) filesystems. It is recommended the file name case matches the case of the desired QML type name exactly - for example, <code>Box.qml</code> and not <code>BoX.qml</code> - regardless of the platform to which the QML type will be deployed.</p><a name="importing-types-defined-outside-the-current-directory"></a>
<h3 id="importing-types-defined-outside-the-current-directory">Importing Types Defined Outside the Current Directory</h3>
<p>If <code>SquareButton.qml</code> was not in the same directory as <code>myapplication.qml</code>, the <code>SquareButton</code> type would need to be specifically made available through an <i>import</i> statement in <code>myapplication.qml</code>. It could be imported from a relative path on the file system, or as an installed module; see <a href="qtqml-modules-topic.html">module</a> for more details.</p>
<a name="accessible-attributes-of-custom-types"></a>
<h2 id="accessible-attributes-of-custom-types">Accessible Attributes of Custom Types</h2>
<p>The <b>root object</b> definition in a .qml file <b>defines the attributes that are available for a QML type</b>. All properties, signals and methods that belong to this root object - whether they are custom declared, or come from the QML type of the root object - are externally accessible and can be read and modified for objects of this type.</p>
<p>For example, the root object type in the <code>SquareButton.qml</code> file above is Rectangle. This means any properties defined by the Rectangle type can be modified for a <code>SquareButton</code> object. The code below defines three <code>SquareButton</code> objects with customized values for some of the properties of the root Rectangle object of the <code>SquareButton</code> type:</p>
<pre class="qml">



</pre>
<p class="centerAlign"><img src="images/documents-definetypes-attributes.png" alt="" /></p><p>The attributes that are accessible to objects of the custom QML type include any <a href="qtqml-syntax-objectattributes.html#defining-property-attributes">custom properties</a>, <a href="qtqml-syntax-objectattributes.html#defining-method-attributes">methods</a> and <a href="qtqml-syntax-objectattributes.html#defining-signal-attributes">signals</a> that have additionally been defined for an object. For example, suppose the Rectangle in <code>SquareButton.qml</code> had been defined as follows, with additional properties, methods and signals:</p>
<pre class="qml">



</pre>
<p>Any <code>SquareButton</code> object could make use of the <code>pressed</code> property, <code>buttonClicked</code> signal and <code>randomizeColor()</code> method that have been added to the root Rectangle:</p>
<pre class="qml">



</pre>
<p>Note that any of the <code>id</code> values defined in <code>SquareButton.qml</code> are not accessible to <code>SquareButton</code> objects, as id values are only accessible from within the component scope in which a component is declared. The <code>SquareButton</code> object definition above cannot refer to <code>mouseArea</code> in order to refer to the MouseArea child, and if it had an <code>id</code> of <code>root</code> rather than <code>squareButton</code>, this would not conflict with the <code>id</code> of the same value for the root object defined in <code>SquareButton.qml</code> as the two would be declared within separate scopes.</p>
</div>
<!-- @@@qtqml-documents-definetypes.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>