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