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