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