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?
16An alias property points to a property with a C++ type whose QML counterpart was not found. This can
17be 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
20actually installed, and that their modules end up in an
21\l{Import Statements#qml-import-path}{import path}.
22
23The warning might also indicate that the type of the property referenced by the alias does not have
24a QML counterpart. The referenced property type might be missing the
25QML_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?
29QML 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
31C++ and \l{qmllint} as well as \l{\QMLLS}
32can't analyze this property alias.
33
34\section2 Example
35Let 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
42class MyCppObject : public QObject
43{
44 Q_OBJECT
45 QML_ELEMENT
46public:
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
55private:
56 QQuickItem *m_myProperty;
57
58signals:
59 void notifyMyProperty();
60};
61\endcode
62
63with following \c{CMakeLists.txt}:
64\badcode
65project(mymodule VERSION 0.1 LANGUAGES CXX)
66
67set(CMAKE_CXX_STANDARD_REQUIRED ON)
68find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
69qt_standard_project_setup(REQUIRES 6.5)
70
71qt_add_executable(appmymodule
72 main.cpp
73)
74
75qt_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
82target_link_libraries(appmymodule
83 PRIVATE Qt6::Quick
84)
85\endcode
86
87The C++ dependency \c{Quick} was declared, such that this class can compile and the QQuickItem
88include can be found. Also, \c{mymodule} does not have any QML dependency on \l{Qt Quick}.
89
90Now, let's try to use \c{myProperty} in an alias in QML. The program will run but QML tooling like
91the \l{Qt Quick Compiler}{compiler}, for example, will complain about the usage of \c{myProperty}:
92\qml
93import mymodule
94
95MyCppObject {
96 id: root
97
98 property alias myAlias: root.myProperty // not ok: Cannot deduce type of alias [missing-type]
99}
100\endqml
101The 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}
103in your QML file. This is because the same type can be exposed multiple times with different
104attributes in different modules: \c{mymodule} actually has to be precise about the QML type of
105\c{myProperty}.
106
107To fix this warning, add the dependency in the \c{CMakeLists.txt}:
108\badcode
109qt_add_qml_module(mymodule
110 URI mymodule
111 ...
112 # declarare QML dependency to QtQuick module
113 DEPENDENCIES QtQuick
114 ...
115)
116\endcode
117
118Now, 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?
125A binding was set on a property whose QML type was not found. This can be caused by a QML module
126which 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
129actually installed, and that their modules end up in an
130\l{Import Statements#qml-import-path}{import path}.
131
132The warning might also indicate that the type of the property does not have
133a QML counterpart. The property type might be missing the
134QML_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?
138QML 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
140C++ and \l{qmllint} as well as \l{\QMLLS} can't analyze this property binding.
141
142\section2 Example
143Let 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
150class MyCppObject : public QObject
151{
152 Q_OBJECT
153 QML_ELEMENT
154public:
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
167private:
168 QQuickItem *m_myProperty;
169 QQuickItem *m_myProperty2;
170
171signals:
172 void notifyMyProperty();
173 void notifyMyProperty2();
174};
175\endcode
176
177with following \c{CMakeLists.txt}:
178\badcode
179project(mymodule VERSION 0.1 LANGUAGES CXX)
180
181set(CMAKE_CXX_STANDARD_REQUIRED ON)
182find_package(Qt6 6.5 REQUIRED COMPONENTS Quick)
183qt_standard_project_setup(REQUIRES 6.5)
184
185qt_add_executable(appmymodule
186 main.cpp
187)
188
189qt_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
196target_link_libraries(appmymodule
197 PRIVATE Qt6::Quick
198)
199\endcode
200
201The C++ dependency \c{Quick} was declared, such that this class can compile and the QQuickItem
202include can be found. Also, \c{mymodule} does not have any QML dependency on \l{Qt Quick}.
203
204Now, let's try to bind \c{myProperty2} to \c{myProperty} in an alias in QML. The program will run
205but QML tooling like the \l{Qt Quick Compiler}{compiler}, for example, will complain about the
206usage of \c{myProperty}:
207
208\qml
209import mymodule
210
211MyCppObject {
212 id: root
213
214 myProperty: myProperty2 // not ok: No type found for property "myProperty". [missing-type]
215}
216\endqml
217The reason for the warning message is that in the QML code, the type \c{QQuickItem} of \c{myProperty}
218and its QML counterpart \c{Item} are not known: the dependency \c QtQuick of mymodule was not
219declared in the \c{CMakeLists.txt}.
220
221To fix this warning, add the dependency in the \c{CMakeLists.txt}:
222\badcode
223qt_add_qml_module(mymodule
224 URI mymodule
225 ...
226 # declarare QML dependency to QtQuick module
227 DEPENDENCIES QtQuick
228 ...
229)
230\endcode
231
232Now, the warning should be gone!
233
234\b {See also} \l {Declaring module dependencies}.
235*/