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?
15You used a property of the type \l[Qml]{var} as if it were a callable function.
16
17\section2 Why is this bad?
18
19This affects the readability of the code, the QML engine will error out if the
20property does not contain a callable function, and the QML tooling can't apply
21specific method optimizations.
22
23
24\section2 Example
25\qml
26import QtQuick
27
28Item {
29 property var fun: () => console.log("Hello")
30 Component.onCompleted: { fun(); }
31}
32
33\endqml
34To fix this warning, declare \c fun as a function to be able to call it:
35\qml
36import QtQuick
37
38Item {
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?
51You 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?
56The property was very probably not meant to be called and will make the QML engine
57error out at runtime.
58
59\section2 Example
60\qml
61import QtQuick
62
63Rectangle {
64 // Rectangle has a property gradient of the type QJSValue
65 Component.onCompleted: { console.log(gradient()); }
66}
67
68\endqml
69To fix this warning, remove the call to \c gradient:
70\qml
71import QtQuick
72
73Item {
74 // Rectangle has a property gradient of the type QJSValue
75 Component.onCompleted: { console.log(gradient); }
76}
77
78\endqml
79
80Alternatively, consider replacing the property definition with a regular
81\l{Q_INVOKABLE} method or a \l{signalsandslots.html#slots}{slot} if you are
82the author of the property. This is only possible if the function is never
83meant to be changed in QML. If you actually need to store a callback, you're
84out of luck.
85
86\section1 Signal is shadowed by a property
87
88\section2 What happened?
89You called a signal that is shadowed by a property or you shadowed a signal
90with a property.
91
92\section2 Why is this bad?
93The caller very probably expected to call a signal, and not the shadowing
94property. This could make the QML engine error out at runtime.
95
96\section2 Example
97\qml
98import QtQuick
99
100Item {
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
112To fix this warning, rename the shadowing property:
113\qml
114import QtQuick
115
116Item {
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
131See \{Signal is shadowed by a property}.
132
133\section1 Slot is shadowed by a property
134
135See \{Signal is shadowed by a property}.
136
137\section1 Property is not a method
138
139\section2 What happened?
140You used a property of a type other than \l[Qml]{var} or \c QJSValue
141as if it were a callable function.
142
143\section2 Why is this bad?
144The property can't be called and will make the QML engine error out at runtime.
145
146\section2 Example
147\qml
148import QtQuick
149
150Item {
151 property int hello
152 Component.onCompleted: { console.log(hello()); }
153}
154
155\endqml
156To fix this warning, remove the call or make \c hello a function:
157\qml
158import QtQuick
159
160Item {
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*/