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
9This document contains the QML coding conventions that we follow in our
10documentation and examples and recommend that others follow.
11
12\section1 QML object declarations
13
14Throughout our documentation and examples,
15\l{QML Object Attributes}{QML object attributes} are always structured in the
16following 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
27For better readability, we separate these different parts with an empty line.
28
29
30For 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
37If using multiple properties from a group of properties,
38consider using \e {group notation} instead of \e {dot notation} if it
39improves readability.
40
41For example, this:
42
43\snippet qmlapp/codingconventions/dotproperties.qml 0
44
45could be written like this:
46
47\snippet qmlapp/codingconventions/dotproperties.qml 1
48
49\section1 Unqualified access
50
51In order to improve readability and performance always reference properties of
52parent components by their id explicitly:
53
54\snippet qmlapp/codingconventions/qualifiedaccess.qml 0
55
56\section1 Required properties
57
58When requiring data defined outside the component, make this explicit by using
59\l{Required Properties}. Required properties must be set or else the creation
60of the component will fail. These are preferable to unqualified lookups because
61they are more performant and allow for both users and tooling to reason about
62an external property's type. Additionally they remove assumptions that a
63component otherwise has to make about the environment in which it is created.
64
65\section1 Signal handlers
66
67When handling parameters in signal handlers use functions which name them
68explicitly:
69
70\snippet qmlapp/codingconventions/signalhandler.qml 0
71
72\section1 JavaScript code
73
74For better readability and maintainability, we generally declare each property
75on a separate line, even for simple expressions.
76
77\snippet qmlapp/codingconventions/javascript.qml 0
78
79For script expressions spanning multiple lines, we use a block format:
80
81\snippet qmlapp/codingconventions/javascript.qml 1
82
83If the script is more than a couple of lines long or can be used by different
84objects, we recommend creating a function and calling it like this:
85
86\snippet qmlapp/codingconventions/javascript.qml 2
87
88Also note that is recommended to add type annotations to your function in order
89to more easily reason about and refactor your application since both parameter
90and return types are immediately visible from the function signature.
91
92For long scripts, we will put the functions in their own JavaScript file and
93import it like this:
94
95\snippet qmlapp/codingconventions/javascript-imports.qml 0
96
97If the code is longer than one line and hence within a block,
98we use semicolons to indicate the end of each statement:
99
100\snippet qmlapp/codingconventions/javascript-semicolons.qml 0
101
102*/