/usr/share/qt5/doc/qtdoc/qml-codingconventions.html is in qt5-doc-html 5.9.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | <?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.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 >QML Coding Conventions</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.9.5 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>
</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">bool</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="name">y</span>: <span class="number">20</span>; <span class="name">height</span>: <span class="number">150</span> <span class="comment">// try to group related properties together</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">font</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>
</div>
<!-- @@@qml-codingconventions.html -->
</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>
|