Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > bdbbdfc3f538bf93bb0eb988a7a43005 > files > 406

qtdoc5-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" />
<!-- codingconventions.qdoc -->
  <title>QML Coding Conventions | Qt 5.12</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.12</a></td><td >QML Coding Conventions</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.12.6 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="#qml-object-declarations">QML Object Declarations</a></li>
<li class="level1"><a href="#grouped-properties">Grouped Properties</a></li>
<li class="level1"><a href="#lists">Lists</a></li>
<li class="level1"><a href="#javascript-code">JavaScript Code</a></li>
<li class="level1"><a href="#related-information">Related Information</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QML Coding Conventions</h1>
<span class="subtitle"></span>
<!-- $$$qml-codingconventions.html-description -->
<div class="descr"> <a name="details"></a>
<p>This document contains the QML coding conventions that we follow in our documentation and examples and recommend that others follow.</p>
<a name="qml-object-declarations"></a>
<h2 id="qml-object-declarations">QML Object Declarations</h2>
<p>Throughout our documentation and examples, QML object attributes are always structured in the following order:</p>
<ul>
<li>id</li>
<li>property declarations</li>
<li>signal declarations</li>
<li>JavaScript functions</li>
<li>object properties</li>
<li>child objects</li>
<li>states</li>
<li>transitions</li>
</ul>
<p>For better readability, we separate these different parts with an empty line.</p>
<p>For example, a hypothetical <i>photo</i> QML object would look like this:</p>
<pre class="qml">

  Rectangle {
      id: photo                                               // id on the first line makes it easy to find an object

      property bool thumbnail: false                          // property declarations
      property alias image: photoImage.source

      signal clicked                                          // signal declarations

      function doSomething(x)                                 // javascript functions
      {
          return x + photoImage.width
      }

      color: "gray"                                           // object properties
      x: 20                                                   // try to group related properties together
      y: 20
      height: 150
      width: {                                                // large bindings
          if (photoImage.width > 200) {
              photoImage.width;
          } else {
              200;
          }
      }

      Rectangle {                                             // child objects
          id: border
          anchors.centerIn: parent; color: "white"

          Image {
              id: photoImage
              anchors.centerIn: parent
          }
      }

      states: State {                                         // states
          name: "selected"
          PropertyChanges { target: border; color: "red" }
      }

      transitions: Transition {                               // transitions
          from: ""
          to: "selected"
          ColorAnimation { target: border; duration: 200 }
      }
  }

</pre>
<a name="grouped-properties"></a>
<h2 id="grouped-properties">Grouped Properties</h2>
<p>If using multiple properties from a group of properties, consider using <i>group notation</i> instead of <i>dot notation</i> if it improves readability.</p>
<p>For example, this:</p>
<pre class="qml">

  Rectangle {
      anchors.left: parent.left; anchors.top: parent.top; anchors.right: parent.right; anchors.leftMargin: 20
  }

  Text {
      text: "hello"
      font.bold: true; font.italic: true; font.pixelSize: 20; font.capitalization: Font.AllUppercase
  }

</pre>
<p>could be written like this:</p>
<pre class="qml">

  Rectangle {
      anchors { left: parent.left; top: parent.top; right: parent.right; leftMargin: 20 }
  }

  Text {
      text: "hello"
      font { bold: true; italic: true; pixelSize: 20; capitalization: Font.AllUppercase }
  }

</pre>
<a name="lists"></a>
<h2 id="lists">Lists</h2>
<p>If a list contains only one element, we generally omit the square brackets.</p>
<p>For example, it is very common for a component to only have one state.</p>
<p>In this case, instead of:</p>
<pre class="qml">

  states: [
      State {
          name: "open"
          PropertyChanges { target: container; width: 200 }
      }
  ]

</pre>
<p>we will write this:</p>
<pre class="qml">

  states: State {
      name: "open"
      PropertyChanges { target: container; width: 200 }
  }

</pre>
<a name="javascript-code"></a>
<h2 id="javascript-code">JavaScript Code</h2>
<p>If the script is a single expression, we recommend writing it inline:</p>
<pre class="qml">

  Rectangle { color: "blue"; width: parent.width / 3 }

</pre>
<p>If the script is only a couple of lines long, we generally use a block:</p>
<pre class="qml">

  Rectangle {
      color: "blue"
      width: {
          var w = parent.width / 3
          console.debug(w)
          return w
      }
  }

</pre>
<p>If the script is more than a couple of lines long or can be used by different objects, we recommend creating a function and calling it like this:</p>
<pre class="qml">

  function calculateWidth(object)
  {
      var w = object.width / 3
      // ...
      // more javascript code
      // ...
      console.debug(w)
      return w
  }

  Rectangle { color: "blue"; width: calculateWidth(parent) }

</pre>
<p>For long scripts, we will put the functions in their own JavaScript file and import it like this:</p>
<pre class="qml">

  import "myscript.js" as Script

  Rectangle { color: "blue"; width: Script.calculateWidth(parent) }

</pre>
<p>If the code is longer than one line and hence within a block, we use semicolons to indicate the end of each statement:</p>
<pre class="qml">

  MouseArea {
      anchors.fill: parent
      onClicked: {
          var scenePos = mapToItem(null, mouseX, mouseY);
          console.log("MouseArea was clicked at scene pos " + scenePos);
      }
  }

</pre>
<a name="related-information"></a>
<h2 id="related-information">Related Information</h2>
<ul>
<li>Best Practices for QML and Qt Quick</li>
</ul>
</div>
<!-- @@@qml-codingconventions.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>