Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
codingconventions.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qml-codingconventions.html
6
\title QML Coding Conventions
7
\brief code style convention
8
9
This document contains the QML coding conventions that we follow in our
10
documentation and examples and recommend that others follow.
11
12
\section1 QML object declarations
13
14
Throughout our documentation and examples,
15
\l{QML Object Attributes}{QML object attributes} are always structured in the
16
following order:
17
18
\list
19
\li id
20
\li property declarations
21
\li signal declarations
22
\li JavaScript functions
23
\li object properties
24
\li child objects
25
\endlist
26
27
For better readability, we separate these different parts with an empty line.
28
29
30
For example, a hypothetical \e photo QML object would look like this:
31
32
\snippet qmlapp/codingconventions/photo.qml 0
33
34
35
\section1 Grouped properties
36
37
If using multiple properties from a group of properties,
38
consider using \e {group notation} instead of \e {dot notation} if it
39
improves readability.
40
41
For example, this:
42
43
\snippet qmlapp/codingconventions/dotproperties.qml 0
44
45
could be written like this:
46
47
\snippet qmlapp/codingconventions/dotproperties.qml 1
48
49
\section1 Unqualified access
50
51
In order to improve readability and performance always reference properties of
52
parent components by their id explicitly:
53
54
\snippet qmlapp/codingconventions/qualifiedaccess.qml 0
55
56
\section1 Required properties
57
58
When requiring data defined outside the component, make this explicit by using
59
\l{Required Properties}. Required properties must be set or else the creation
60
of the component will fail. These are preferable to unqualified lookups because
61
they are more performant and allow for both users and tooling to reason about
62
an external property's type. Additionally they remove assumptions that a
63
component otherwise has to make about the environment in which it is created.
64
65
\section1 Signal handlers
66
67
When handling parameters in signal handlers use functions which name them
68
explicitly:
69
70
\snippet qmlapp/codingconventions/signalhandler.qml 0
71
72
\section1 JavaScript code
73
74
For better readability and maintainability, we generally declare each property
75
on a separate line, even for simple expressions.
76
77
\snippet qmlapp/codingconventions/javascript.qml 0
78
79
For script expressions spanning multiple lines, we use a block format:
80
81
\snippet qmlapp/codingconventions/javascript.qml 1
82
83
If the script is more than a couple of lines long or can be used by different
84
objects, we recommend creating a function and calling it like this:
85
86
\snippet qmlapp/codingconventions/javascript.qml 2
87
88
Also note that is recommended to add type annotations to your function in order
89
to more easily reason about and refactor your application since both parameter
90
and return types are immediately visible from the function signature.
91
92
For long scripts, we will put the functions in their own JavaScript file and
93
import it like this:
94
95
\snippet qmlapp/codingconventions/javascript-imports.qml 0
96
97
If the code is longer than one line and hence within a block,
98
we use semicolons to indicate the end of each statement:
99
100
\snippet qmlapp/codingconventions/javascript-semicolons.qml 0
101
102
*/
qtdeclarative
src
quick
doc
src
guidelines
codingconventions.qdoc
Generated on
for Qt by
1.14.0