<?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.11</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.11</a></td><td >QML Coding Conventions</td></tr></table><table class="buildversion"><tr> <td id="buildversion" width="100%" align="right">Qt 5.11.2 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, <a href="../qtqml/qtqml-syntax-objectattributes.html">QML object attributes</a> 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"> <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">id</span>: <span class="name">photo</span> <span class="comment">// id on the first line makes it easy to find an object</span> property <span class="type"><a href="../qtqml/qml-bool.html">bool</a></span> <span class="name">thumbnail</span>: <span class="number">false</span> <span class="comment">// property declarations</span> property <span class="type">alias</span> <span class="name">image</span>: <span class="name">photoImage</span>.<span class="name">source</span> signal <span class="type">clicked</span> <span class="comment">// signal declarations</span> <span class="keyword">function</span> <span class="name">doSomething</span>(<span class="name">x</span>) <span class="comment">// javascript functions</span> { <span class="keyword">return</span> <span class="name">x</span> <span class="operator">+</span> <span class="name">photoImage</span>.<span class="name">width</span> } <span class="name">color</span>: <span class="string">"gray"</span> <span class="comment">// object properties</span> <span class="name">x</span>: <span class="number">20</span> <span class="comment">// try to group related properties together</span> <span class="name">y</span>: <span class="number">20</span> <span class="name">height</span>: <span class="number">150</span> <span class="name">width</span>: { <span class="comment">// large bindings</span> <span class="keyword">if</span> (<span class="name">photoImage</span>.<span class="name">width</span> <span class="operator">></span> <span class="number">200</span>) { <span class="name">photoImage</span>.<span class="name">width</span>; } <span class="keyword">else</span> { <span class="number">200</span>; } } <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="comment">// child objects</span> <span class="name">id</span>: <span class="name">border</span> <span class="name">anchors</span>.centerIn: <span class="name">parent</span>; <span class="name">color</span>: <span class="string">"white"</span> <span class="type"><a href="../qtquick/qml-qtquick-image.html">Image</a></span> { <span class="name">id</span>: <span class="name">photoImage</span> <span class="name">anchors</span>.centerIn: <span class="name">parent</span> } } <span class="name">states</span>: <span class="name">State</span> { <span class="comment">// states</span> <span class="name">name</span>: <span class="string">"selected"</span> <span class="type"><a href="../qtquick/qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">border</span>; <span class="name">color</span>: <span class="string">"red"</span> } } <span class="name">transitions</span>: <span class="name">Transition</span> { <span class="comment">// transitions</span> <span class="name">from</span>: <span class="string">""</span> <span class="name">to</span>: <span class="string">"selected"</span> <span class="type"><a href="../qtquick/qml-qtquick-coloranimation.html">ColorAnimation</a></span> { <span class="name">target</span>: <span class="name">border</span>; <span class="name">duration</span>: <span class="number">200</span> } } } </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"> <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">anchors</span>.left: <span class="name">parent</span>.<span class="name">left</span>; <span class="name">anchors</span>.top: <span class="name">parent</span>.<span class="name">top</span>; <span class="name">anchors</span>.right: <span class="name">parent</span>.<span class="name">right</span>; <span class="name">anchors</span>.leftMargin: <span class="number">20</span> } <span class="type"><a href="../qtquick/qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">"hello"</span> <span class="name">font</span>.bold: <span class="number">true</span>; <span class="name">font</span>.italic: <span class="number">true</span>; <span class="name">font</span>.pixelSize: <span class="number">20</span>; <span class="name">font</span>.capitalization: <span class="name">Font</span>.<span class="name">AllUppercase</span> } </pre> <p>could be written like this:</p> <pre class="qml"> <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="type">anchors</span> { <span class="name">left</span>: <span class="name">parent</span>.<span class="name">left</span>; <span class="name">top</span>: <span class="name">parent</span>.<span class="name">top</span>; <span class="name">right</span>: <span class="name">parent</span>.<span class="name">right</span>; <span class="name">leftMargin</span>: <span class="number">20</span> } } <span class="type"><a href="../qtquick/qml-qtquick-text.html">Text</a></span> { <span class="name">text</span>: <span class="string">"hello"</span> <span class="type"><a href="../qtquick/qml-font.html">font</a></span> { <span class="name">bold</span>: <span class="number">true</span>; <span class="name">italic</span>: <span class="number">true</span>; <span class="name">pixelSize</span>: <span class="number">20</span>; <span class="name">capitalization</span>: <span class="name">Font</span>.<span class="name">AllUppercase</span> } } </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"> <span class="name">states</span>: [ <span class="type"><a href="../qtqml/qml-qtqml-statemachine-state.html">State</a></span> { <span class="name">name</span>: <span class="string">"open"</span> <span class="type"><a href="../qtquick/qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">container</span>; <span class="name">width</span>: <span class="number">200</span> } } ] </pre> <p>we will write this:</p> <pre class="qml"> <span class="name">states</span>: <span class="name">State</span> { <span class="name">name</span>: <span class="string">"open"</span> <span class="type"><a href="../qtquick/qml-qtquick-propertychanges.html">PropertyChanges</a></span> { <span class="name">target</span>: <span class="name">container</span>; <span class="name">width</span>: <span class="number">200</span> } } </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"> <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">color</span>: <span class="string">"blue"</span>; <span class="name">width</span>: <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">3</span> } </pre> <p>If the script is only a couple of lines long, we generally use a block:</p> <pre class="qml"> <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">color</span>: <span class="string">"blue"</span> <span class="name">width</span>: { var <span class="name">w</span> = <span class="name">parent</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">3</span> <span class="name">console</span>.<span class="name">debug</span>(<span class="name">w</span>) <span class="keyword">return</span> <span class="name">w</span> } } </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"> <span class="keyword">function</span> <span class="name">calculateWidth</span>(<span class="name">object</span>) { var <span class="name">w</span> = <span class="name">object</span>.<span class="name">width</span> <span class="operator">/</span> <span class="number">3</span> <span class="comment">// ...</span> <span class="comment">// more javascript code</span> <span class="comment">// ...</span> <span class="name">console</span>.<span class="name">debug</span>(<span class="name">w</span>) <span class="keyword">return</span> <span class="name">w</span> } <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">color</span>: <span class="string">"blue"</span>; <span class="name">width</span>: <span class="name">calculateWidth</span>(<span class="name">parent</span>) } </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 <span class="type"><a href="../qtquick/qml-qtquick-rectangle.html">Rectangle</a></span> { <span class="name">color</span>: <span class="string">"blue"</span>; <span class="name">width</span>: <span class="name">Script</span>.<span class="name">calculateWidth</span>(<span class="name">parent</span>) } </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"> <span class="type"><a href="../qtquick/qml-qtquick-mousearea.html">MouseArea</a></span> { <span class="name">anchors</span>.fill: <span class="name">parent</span> <span class="name">onClicked</span>: { var <span class="name">scenePos</span> = <span class="name">mapToItem</span>(<span class="number">null</span>, <span class="name">mouseX</span>, <span class="name">mouseY</span>); <span class="name">console</span>.<span class="name">log</span>(<span class="string">"MouseArea was clicked at scene pos "</span> <span class="operator">+</span> <span class="name">scenePos</span>); } } </pre> <a name="related-information"></a> <h2 id="related-information">Related Information</h2> <ul> <li><a href="../qtquick/qtquick-bestpractices.html">Best Practices for QML and Qt Quick</a></li> </ul> </div> <!-- @@@qml-codingconventions.html --> </div> </div> </div> </div> </div> <div class="footer"> <p> <acronym title="Copyright">©</acronym> 2018 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>