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
signal-handler-parameters.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-signal-handler-parameters.html
6
\ingroup qmllint-warnings-and-errors
7
8
\title Signal handler parameters
9
\brief [signal-handler-parameters] The signal handler does not satisfy the signal types.
10
11
\qmllintwarningcategory signal-handler-parameters
12
13
This warning category has multiple warnings, described in the sections below:
14
15
\section1 Type of parameter in signal was not found
16
17
\section2 What happened?
18
A signal handler tried to handle a signal with parameters of unknown QML types.
19
20
Usually, this happens when handling C++ defined signals in QML when the module with the C++ defined
21
signal does not properly declare its QML dependency to another QML module. If the module with the
22
C++ defined signal compiles, then this is a sign that a dependency was only declared on the C++
23
level and not on \l{qt_add_qml_module#declaring-module-dependencies}{the QML module level}.
24
25
\note If you are importing QML modules with external dependencies, verify that they are
26
actually installed, and that their modules end up in an
27
\l{Import Statements#qml-import-path}{import path}.
28
29
The warning might also indicate that the parameter type of the C++ defined signal does not have
30
a QML counterpart. The parameter type might be missing the \l QML_ELEMENT macro, for example.
31
Refer to \l{Defining QML Types from C++} or \l{Overview - QML and C++ Integration} in this case.
32
33
\section2 Why is this bad?
34
In the first case, the module with the C++ signal has an undeclared dependency on the QML module
35
level, which makes it hard to use the module, as users of the module need to guess the module's
36
hidden dependencies.
37
38
In both cases, QML tooling is not able to find the QML counterpart of the
39
C++ type: the \l{Qt Quick Compiler}{compiler} can't compile this signal handler to
40
C++ and \l{qmllint} as well as \l{\QMLLS}
41
can't analyze this handler.
42
43
\section2 Example
44
45
Let our module have a C++ class with one \c{helloWorld} signal:
46
\code
47
#include <QQuickItem>
48
#include <QtQml/qqmlregistration.h>
49
#include <QObject>
50
51
class MyCppObject : public QObject
52
{
53
Q_OBJECT
54
QML_ELEMENT
55
public:
56
MyCppObject(QObject *parent = nullptr)
57
: QObject(parent)
58
{}
59
60
signals:
61
void helloWorld(QQuickItem *i);
62
63
};
64
\endcode
65
with following CMakeLists.txt:
66
\badcode
67
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick QuickControls2)
68
69
qt_standard_project_setup(REQUIRES 6.5)
70
71
qt_add_executable(mymodule
72
main.cpp
73
)
74
75
qt_add_qml_module(mymodule
76
URI MyModule
77
VERSION 1.0
78
QML_FILES Main.qml
79
SOURCES mycppobject.cpp mycppobject.h
80
)
81
82
# declare C++ dependency to Quick
83
target_link_libraries(appuntitled27
84
PRIVATE Qt6::Quick
85
)
86
\endcode
87
The C++ dependency \c{Quick} was declared, such that this class can compile and the QQuickItem
88
include can be found. Also, mymodule does not have any QML dependency on \l{Qt Quick}.
89
90
Now, lets try to handle this \c{helloWorld} signal in QML:
91
\qml
92
import MyModule // name of the module with MyCppObject
93
94
MyCppObject {
95
onHelloWorld: function (x) { console.log(x); } // not ok: Type QQuickItem was not found!
96
}
97
\endqml
98
99
The reason of the warning message is that in the QML code, \c{QQuickItem} and its QML counterpart
100
\c{Item} are not known: the dependency \c QtQuick of MyModule was not declared in the CMakeLists.txt!
101
102
You can add it as following in the qt_add_qml_module() call:
103
\badcode
104
qt_add_qml_module(mymodule
105
URI MyModule
106
...
107
# declare QML dependencies to QtQuick:
108
DEPENDENCIES QtQuick
109
...
110
)
111
\endcode
112
113
Now, the QML code should be fine again!
114
115
\sa {qt_add_qml_module#declaring-module-dependencies}
116
117
\omit
118
TODO: QML Lint cannot detect if you pass signal parameters by value, reference or pointer!
119
Therefore, it will never print that warning.
120
\section1 Type of parameter in signal should be passed by pointer
121
\section2 What happened?
122
TODO
123
124
\section2 Why is this bad?
125
TODO
126
127
\section2 Example
128
\qml
129
\endqml
130
You can fix this warning by TODO
131
\qml
132
\endqml
133
134
TODO: QML Lint cannot detect if you pass signal parameters by value, reference or pointer!
135
Therefore, it will never print that warning.
136
that warning
137
\section1 Type of parameter in signal should be passed by value or const reference
138
\section2 What happened?
139
TODO
140
141
\section2 Why is this bad?
142
TODO
143
144
\section2 Example
145
\qml
146
\endqml
147
You can fix this warning by TODO
148
\qml
149
\endqml
150
151
\endomit
152
153
\section1 Signal handler has more formal parameters than the signal it handles
154
\section2 What happened?
155
A signal handler expects more parameters than what the signal will actually provide.
156
157
\section2 Why is this bad?
158
The extra parameters will be undefined.
159
160
\section2 Example
161
\qml
162
import QtQuick
163
164
Item {
165
signal helloWorld(x: QtObject) // signal expects only one parameter
166
167
onHelloWorld: function (x,y,z) {} // not ok: signal handler handles three parameters
168
}
169
\endqml
170
To fix this warning, remove the extra parameters of the signal handler or
171
add the missing parameters to the signal's declaration:
172
\qml
173
import QtQuick
174
175
Item {
176
signal helloWorld(x: QtObject) // signal expects only one parameter
177
178
onHelloWorld: function (x) {} // ok: signal handler handles one parameter
179
180
signal alternativeHelloWorld(x: QtObject, y: int, y: int) // signal expects three parameters
181
182
onAlternativeHelloWorld: function (x,y,z) {} // ok: signal handler handles three parameters
183
}
184
\endqml
185
186
\section1 The signal has a parameter of the same name
187
\section2 What happened?
188
The signal or signal handler might have swapped some of its arguments, or some arguments might be
189
missing.
190
191
\section2 Why is this bad?
192
This is very probably a typo and not intended by the user.
193
194
\section2 Example
195
\section3 Missing Arguments
196
\qml
197
import QtQuick
198
199
Item {
200
signal helloWorld(x: QtObject, y: int)
201
202
onHelloWorld: function (y) {} // not ok: it seems that x was forgotten
203
}
204
205
\endqml
206
To fix this warning, add the missing parameters or rename the first parameter:
207
\qml
208
import QtQuick
209
210
Item {
211
signal helloWorld(x: QtObject, y: int)
212
213
onHelloWorld: function (x, y) {} // ok: parameters have the same order as in helloWorld
214
215
signal alternativeHelloWorld(x: QtObject, y: int)
216
217
onAlternativeHelloWorld: function (x) {} // ok: parameters have the same order as in helloWorld, even if y is missing
218
}
219
\endqml
220
221
\section3 Swapped arguments
222
\qml
223
import QtQuick
224
225
Item {
226
signal helloWorld(x: QtObject, y: int)
227
228
onHelloWorld: function (y, x) {} // not ok: helloWorld expects first 'x' then 'y'
229
}
230
231
\endqml
232
To fix this warning, reorder the parameters in the correct order:
233
\qml
234
import QtQuick
235
236
Item {
237
signal helloWorld(x: QtObject, y: int)
238
239
onHelloWorld: function (x, y) {} // ok: parameters have the same order as in helloWorld
240
}
241
242
\endqml
243
*/
qtdeclarative
src
qml
doc
src
qmllint
signal-handler-parameters.qdoc
Generated on
for Qt by
1.14.0