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