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
12This 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?
17A signal handler tried to handle a signal with parameters of unknown QML types.
18
19Usually, this happens when handling C++ defined signals in QML when the module with the C++ defined
20signal does not properly declare its QML dependency to another QML module. If the module with the
21C++ defined signal compiles, then this is a sign that a dependency was only declared on the C++
22level 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
25actually installed, and that their modules end up in an
26\l{Import Statements#qml-import-path}{import path}.
27
28The warning might also indicate that the parameter type of the C++ defined signal does not have
29a QML counterpart. The parameter type might be missing the \l QML_ELEMENT macro, for example.
30Refer to \l{Defining QML Types from C++} or \l{Overview - QML and C++ Integration} in this case.
31
32\section2 Why is this bad?
33In the first case, the module with the C++ signal has an undeclared dependency on the QML module
34level, which makes it hard to use the module, as users of the module need to guess the module's
35hidden dependencies.
36
37In both cases, QML tooling is not able to find the QML counterpart of the
38C++ type: the \l{Qt Quick Compiler}{compiler} can't compile this signal handler to
39C++ and \l{qmllint} as well as \l{\QMLLS}
40can't analyze this handler.
41
42\section2 Example
43
44Let 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
50class MyCppObject : public QObject
51{
52 Q_OBJECT
53 QML_ELEMENT
54public:
55 MyCppObject(QObject *parent = nullptr)
56 : QObject(parent)
57 {}
58
59signals:
60 void helloWorld(QQuickItem *i);
61
62};
63\endcode
64with following CMakeLists.txt:
65\badcode
66find_package(Qt6 6.5 REQUIRED COMPONENTS Quick QuickControls2)
67
68qt_standard_project_setup(REQUIRES 6.5)
69
70qt_add_executable(mymodule
71 main.cpp
72)
73
74qt_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
82target_link_libraries(appuntitled27
83 PRIVATE Qt6::Quick
84)
85\endcode
86The C++ dependency \c{Quick} was declared, such that this class can compile and the QQuickItem
87include can be found. Also, mymodule does not have any QML dependency on \l{Qt Quick}.
88
89Now, lets try to handle this \c{helloWorld} signal in QML:
90\qml
91import MyModule // name of the module with MyCppObject
92
93MyCppObject {
94 onHelloWorld: function (x) { console.log(x); } // not ok: Type QQuickItem was not found!
95}
96\endqml
97
98The 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
101You can add it as following in the qt_add_qml_module() call:
102\badcode
103qt_add_qml_module(mymodule
104 URI MyModule
105 ...
106 # declare QML dependencies to QtQuick:
107 DEPENDENCIES QtQuick
108 ...
109)
110\endcode
111
112Now, the QML code should be fine again!
113
114\sa {qt_add_qml_module#declaring-module-dependencies}
115
116\omit
117TODO: QML Lint cannot detect if you pass signal parameters by value, reference or pointer!
118Therefore, it will never print that warning.
119\section1 Type of parameter in signal should be passed by pointer
120\section2 What happened?
121TODO
122
123\section2 Why is this bad?
124TODO
125
126\section2 Example
127\qml
128\endqml
129You can fix this warning by TODO
130\qml
131\endqml
132
133TODO: QML Lint cannot detect if you pass signal parameters by value, reference or pointer!
134Therefore, it will never print that warning.
135that warning
136\section1 Type of parameter in signal should be passed by value or const reference
137\section2 What happened?
138TODO
139
140\section2 Why is this bad?
141TODO
142
143\section2 Example
144\qml
145\endqml
146You 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?
154A signal handler expects more parameters than what the signal will actually provide.
155
156\section2 Why is this bad?
157The extra parameters will be undefined.
158
159\section2 Example
160\qml
161import QtQuick
162
163Item {
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
169To fix this warning, remove the extra parameters of the signal handler or
170add the missing parameters to the signal's declaration:
171\qml
172import QtQuick
173
174Item {
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?
187The signal or signal handler might have swapped some of its arguments, or some arguments might be
188missing.
189
190\section2 Why is this bad?
191This is very probably a typo and not intended by the user.
192
193\section2 Example
194\section3 Missing Arguments
195\qml
196import QtQuick
197
198Item {
199 signal helloWorld(x: QtObject, y: int)
200
201 onHelloWorld: function (y) {} // not ok: it seems that x was forgotten
202}
203
204\endqml
205To fix this warning, add the missing parameters or rename the first parameter:
206\qml
207import QtQuick
208
209Item {
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
222import QtQuick
223
224Item {
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
231To fix this warning, reorder the parameters in the correct order:
232\qml
233import QtQuick
234
235Item {
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*/