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
comma.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2025 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\page qmllint-warnings-and-errors-comma.html
6
\ingroup qmllint-warnings-and-errors
7
8
\title Comma
9
\brief [comma] Do not use comma expressions.
10
11
\qmllintwarningcategory comma
12
13
\section1 Do not use comma expressions
14
15
\section2 What happened?
16
A JavaScript comma expression was used outside of a for loop.
17
18
\section2 Why is this bad?
19
Comma expressions reduce readability of the code and obscure side-effects.
20
21
\section2 Example
22
\qml
23
import QtQuick
24
25
Item {
26
Component.onCompleted: init(config, true), enableLogging(categories), run(1000) // millis
27
}
28
\endqml
29
30
To fix this warning, refactor the code to use distinct statements for each
31
operation. This way, each side effect is explicit instead of happening as part
32
of another unrelated operation:
33
34
\qml
35
import QtQuick
36
37
Item {
38
Component.onCompleted: {
39
init(config, true)
40
enableLogging(categories)
41
run(1000) // millis
42
}
43
}
44
\endqml
45
46
In addition, there are some special considerations for cases where comma
47
expressions appear because a variable is intentionally being captured for a
48
binding, as in the following code, where \c previewOfFirstPage is a function
49
defined in C++ which internally depends on \c{Config.fontSize}:
50
51
\qml
52
Text {
53
// This causes the function to re-run when fontSize changes
54
text: Config.fontSize, documentProvider.previewOfFirstPage()
55
}
56
\endqml
57
58
59
If you encounter this situation, consider one of the following
60
approaches:
61
62
\list
63
\li If a function such as \c previewOfFirstPage depends on a property,
64
prefer making this dependency explicit by passing the value as an argument.
65
\qml
66
Text {
67
text: documentProvider.previewOfFirstPage(Config.fontSize)
68
}
69
\endqml
70
\li If changing the function signature is undesirable for API reasons, consider
71
replacing the function with a Q_PROPERTY instead, so that change
72
notifications for it can be emitted when the depenency is modified in C++:
73
\code
74
void Config::setFontSize(int fontSize) {
75
if (m_fontSize == fontSize)
76
return;
77
m_fontSize = fontSize;
78
emit fontSizeChanged();
79
emit previewOfFirstPageChanged();
80
}
81
\endcode
82
\qml
83
Text {
84
text: documentProvider.previewOfFirstPage
85
}
86
\endqml
87
\li If modifying the C++ implementation or adding QML dependencies is not
88
possible, use a \c qmllint directive to silence the warning. Include a
89
comment explaining that the intention is to capture the variable in the
90
binding.
91
\code
92
Text {
93
// This causes the function to re-run when fontSize changes
94
text: Config.fontSize, documentProvider.previewOfFirstPage() // qmllint disable comma
95
}
96
97
\endcode
98
\endlist
99
*/
qtdeclarative
src
qml
doc
src
qmllint
comma.qdoc
Generated on
for Qt by
1.16.1