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