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
use-proper-function.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2023 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 Use proper function
8
\brief [use-proper-function] Calling something that might not be a function.
9
10
\qmllintwarningcategory use-proper-function
11
12
\section1 Property is a variant property: It may or may not be a method
13
14
\section2 What happened?
15
You used a property of the type \l[Qml]{var} as if it were a callable function.
16
17
\section2 Why is this bad?
18
19
This affects the readability of the code, the QML engine will error out if the
20
property does not contain a callable function, and the QML tooling can't apply
21
specific method optimizations.
22
23
24
\section2 Example
25
\qml
26
import QtQuick
27
28
Item {
29
property var fun: () => console.log("Hello")
30
Component.onCompleted: { fun(); }
31
}
32
33
\endqml
34
To fix this warning, declare \c fun as a function to be able to call it:
35
\qml
36
import QtQuick
37
38
Item {
39
function fun() {
40
console.log("Hello")
41
}
42
43
Component.onCompleted: { fun(); }
44
}
45
46
\endqml
47
48
\section1 Property is a QJSValue property: It may or may not be a method
49
50
\section2 What happened?
51
You used a property of the type \c QJSValue as if it were a callable function.
52
53
\note Properties of the type QJSValue can only be defined in C++.
54
55
\section2 Why is this bad?
56
The property was very probably not meant to be called and will make the QML engine
57
error out at runtime.
58
59
\section2 Example
60
\qml
61
import QtQuick
62
63
Rectangle {
64
// Rectangle has a property gradient of the type QJSValue
65
Component.onCompleted: { console.log(gradient()); }
66
}
67
68
\endqml
69
To fix this warning, remove the call to \c gradient:
70
\qml
71
import QtQuick
72
73
Item {
74
// Rectangle has a property gradient of the type QJSValue
75
Component.onCompleted: { console.log(gradient); }
76
}
77
78
\endqml
79
80
Alternatively, consider replacing the property definition with a regular
81
\l{Q_INVOKABLE} method or a \l{signalsandslots.html#slots}{slot} if you are
82
the author of the property. This is only possible if the function is never
83
meant to be changed in QML. If you actually need to store a callback, you're
84
out of luck.
85
86
\section1 Signal is shadowed by a property
87
88
\section2 What happened?
89
You called a signal that is shadowed by a property or you shadowed a signal
90
with a property.
91
92
\section2 Why is this bad?
93
The caller very probably expected to call a signal, and not the shadowing
94
property. This could make the QML engine error out at runtime.
95
96
\section2 Example
97
\qml
98
import QtQuick
99
100
Item {
101
component Base: Item {
102
signal helloSignal
103
}
104
105
Base {
106
property int helloSignal // shadows helloSignal inherited from Base
107
Component.onCompleted: { helloSignal(); }
108
}
109
}
110
111
\endqml
112
To fix this warning, rename the shadowing property:
113
\qml
114
import QtQuick
115
116
Item {
117
component Base: Item {
118
signal helloSignal
119
}
120
121
Base {
122
property int helloSignal2 // does not shadow anymore
123
Component.onCompleted: { helloSignal(); }
124
}
125
}
126
127
\endqml
128
129
\section1 Method is shadowed by a property
130
131
See \{Signal is shadowed by a property}.
132
133
\section1 Slot is shadowed by a property
134
135
See \{Signal is shadowed by a property}.
136
137
\section1 Property is not a method
138
139
\section2 What happened?
140
You used a property of a type other than \l[Qml]{var} or \c QJSValue
141
as if it were a callable function.
142
143
\section2 Why is this bad?
144
The property can't be called and will make the QML engine error out at runtime.
145
146
\section2 Example
147
\qml
148
import QtQuick
149
150
Item {
151
property int hello
152
Component.onCompleted: { console.log(hello()); }
153
}
154
155
\endqml
156
To fix this warning, remove the call or make \c hello a function:
157
\qml
158
import QtQuick
159
160
Item {
161
property int hello
162
Component.onCompleted: { console.log(hello); }
163
164
// alternative:
165
function helloAlternative() { ... }
166
Component.onCompleted: { console.log(helloAlternative()); }
167
}
168
169
\endqml
170
171
*/
qtdeclarative
src
qml
doc
src
qmllint
use-proper-function.qdoc
Generated on
for Qt by
1.16.1