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