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