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