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