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
missing-type.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-missing-type.html
6
\ingroup qmllint-warnings-and-errors
7
8
\title Missing type
9
\brief [missing-type] A type used in a binding or alias was not found.
10
11
\qmllintwarningcategory missing-type
12
13
\section1 Cannot deduce type of alias
14
15
\section2 What happened?
16
An alias property points to a property with a C++ type whose QML counterpart was not found. This can
17
be caused by importing a QML module which do not declare its QML dependencies on other modules.
18
19
\note If you are importing QML modules with external dependencies, verify that they are
20
actually installed, and that their modules end up in an
21
\l{Import Statements#qml-import-path}{import path}.
22
23
The warning might also indicate that the type of the property referenced by the alias does not have
24
a QML counterpart. The referenced property type might be missing the
25
QML_ELEMENT macro, for example. Refer to
26
\l{Defining QML Types from C++} or \l{Overview - QML and C++ Integration} in this case.
27
28
\section2 Why is this bad?
29
QML tooling is not able to find the QML counterpart of the C++ type: the
30
\l{Qt Quick Compiler}{compiler} can't compile this property alias to
31
C++ and \l{qmllint} as well as \l{\QMLLS}
32
can't analyze this property alias.
33
34
\section2 Example
35
Let our QML module have one C++ class with a property \c{myProperty}:
36
37
\code
38
#include <QQuickItem>
39
#include <QtQml/qqmlregistration.h>
40
#include <QObject>
41
42
class MyCppObject : public QObject
43
{
44
Q_OBJECT
45
QML_ELEMENT
46
public:
47
MyCppObject(QObject *parent = nullptr)
48
: QObject(parent)
49
{}
50
51
Q_PROPERTY(QQuickItem *myProperty READ myProperty WRITE setMyProperty NOTIFY notifyMyProperty)
52
QQuickItem *myProperty() { return m_myProperty; }
53
void setMyProperty(QQuickItem *item) { emit notifyMyProperty(); m_myProperty = item; }
54
55
private:
56
QQuickItem *m_myProperty;
57
58
signals:
59
void notifyMyProperty();
60
};
61
\endcode
62
63
with following \c{CMakeLists.txt}:
64
\badcode
65
project(mymodule VERSION 0.1 LANGUAGES CXX)
66
67
set(CMAKE_CXX_STANDARD_REQUIRED ON)
68
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
69
qt_standard_project_setup(REQUIRES 6.5)
70
71
qt_add_executable(appmymodule
72
main.cpp
73
)
74
75
qt_add_qml_module(appmymodule
76
URI mymodule
77
VERSION 1.0
78
QML_FILES Main.qml HelloWorld.qml
79
SOURCES mycppobject.cpp mycppobject.h
80
)
81
82
target_link_libraries(appmymodule
83
PRIVATE Qt6::Quick
84
)
85
\endcode
86
87
The C++ dependency \c{Quick} was declared, such that this class can compile and the QQuickItem
88
include can be found. Also, \c{mymodule} does not have any QML dependency on \l{Qt Quick}.
89
90
Now, let's try to use \c{myProperty} in an alias in QML. The program will run but QML tooling like
91
the \l{Qt Quick Compiler}{compiler}, for example, will complain about the usage of \c{myProperty}:
92
\qml
93
import mymodule
94
95
MyCppObject {
96
id: root
97
98
property alias myAlias: root.myProperty // not ok: Cannot deduce type of alias [missing-type]
99
}
100
\endqml
101
The reason for the warning message is that in the QML code, the type \c{QQuickItem} of
102
\c{myProperty} and its QML counterpart \c{Item} are not known, even if you have \c{import QtQuick}
103
in your QML file. This is because the same type can be exposed multiple times with different
104
attributes in different modules: \c{mymodule} actually has to be precise about the QML type of
105
\c{myProperty}.
106
107
To fix this warning, add the dependency in the \c{CMakeLists.txt}:
108
\badcode
109
qt_add_qml_module(mymodule
110
URI mymodule
111
...
112
# declarare QML dependency to QtQuick module
113
DEPENDENCIES QtQuick
114
...
115
)
116
\endcode
117
118
Now, the warning should be gone!
119
120
\b {See also} \l {Declaring module dependencies}.
121
122
\section1 No type found for property
123
124
\section2 What happened?
125
A binding was set on a property whose QML type was not found. This can be caused by a QML module
126
which does not declare its QML dependencies on other modules.
127
128
\note If you are importing QML modules with external dependencies, verify that they are
129
actually installed, and that their modules end up in an
130
\l{Import Statements#qml-import-path}{import path}.
131
132
The warning might also indicate that the type of the property does not have
133
a QML counterpart. The property type might be missing the
134
QML_ELEMENT macro, for example. Refer to
135
\l{Defining QML Types from C++} or \l{Overview - QML and C++ Integration} in this case.
136
137
\section2 Why is this bad?
138
QML tooling is not able to find the QML counterpart of the C++ type: the
139
\l{Qt Quick Compiler}{compiler} can't compile this property binding to
140
C++ and \l{qmllint} as well as \l{\QMLLS} can't analyze this property binding.
141
142
\section2 Example
143
Let our QML module have a C++ class with two properties, \c{myProperty} and \c{myProperty2}:
144
145
\code
146
#include <QQuickItem>
147
#include <QtQml/qqmlregistration.h>
148
#include <QObject>
149
150
class MyCppObject : public QObject
151
{
152
Q_OBJECT
153
QML_ELEMENT
154
public:
155
MyCppObject(QObject *parent = nullptr)
156
: QObject(parent)
157
{}
158
159
Q_PROPERTY(QQuickItem *myProperty READ myProperty WRITE setMyProperty NOTIFY notifyMyProperty)
160
QQuickItem *myProperty() { return m_myProperty; }
161
void setMyProperty(QQuickItem *item) { emit notifyMyProperty(); m_myProperty = item; }
162
163
Q_PROPERTY(QQuickItem *myProperty2 READ myProperty2 WRITE setMyProperty2 NOTIFY notifyMyProperty2)
164
QQuickItem *myProperty2() { return m_myProperty2; }
165
void setMyProperty2(QQuickItem *item) { emit notifyMyProperty2(); m_myProperty2 = item; }
166
167
private:
168
QQuickItem *m_myProperty;
169
QQuickItem *m_myProperty2;
170
171
signals:
172
void notifyMyProperty();
173
void notifyMyProperty2();
174
};
175
\endcode
176
177
with following \c{CMakeLists.txt}:
178
\badcode
179
project(mymodule VERSION 0.1 LANGUAGES CXX)
180
181
set(CMAKE_CXX_STANDARD_REQUIRED ON)
182
find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
183
qt_standard_project_setup(REQUIRES 6.5)
184
185
qt_add_executable(appmymodule
186
main.cpp
187
)
188
189
qt_add_qml_module(appmymodule
190
URI mymodule
191
VERSION 1.0
192
QML_FILES Main.qml HelloWorld.qml
193
SOURCES mycppobject.cpp mycppobject.h
194
)
195
196
target_link_libraries(appmymodule
197
PRIVATE Qt6::Quick
198
)
199
\endcode
200
201
The C++ dependency \c{Quick} was declared, such that this class can compile and the QQuickItem
202
include can be found. Also, \c{mymodule} does not have any QML dependency on \l{Qt Quick}.
203
204
Now, let's try to bind \c{myProperty2} to \c{myProperty} in an alias in QML. The program will run
205
but QML tooling like the \l{Qt Quick Compiler}{compiler}, for example, will complain about the
206
usage of \c{myProperty}:
207
208
\qml
209
import mymodule
210
211
MyCppObject {
212
id: root
213
214
myProperty: myProperty2 // not ok: No type found for property "myProperty". [missing-type]
215
}
216
\endqml
217
The reason for the warning message is that in the QML code, the type \c{QQuickItem} of \c{myProperty}
218
and its QML counterpart \c{Item} are not known: the dependency \c QtQuick of mymodule was not
219
declared in the \c{CMakeLists.txt}.
220
221
To fix this warning, add the dependency in the \c{CMakeLists.txt}:
222
\badcode
223
qt_add_qml_module(mymodule
224
URI mymodule
225
...
226
# declarare QML dependency to QtQuick module
227
DEPENDENCIES QtQuick
228
...
229
)
230
\endcode
231
232
Now, the warning should be gone!
233
234
\b {See also} \l {Declaring module dependencies}.
235
*/
qtdeclarative
src
qml
doc
src
qmllint
missing-type.qdoc
Generated on
for Qt by
1.14.0