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