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
incompatible-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-incompatible-type.html
6\ingroup qmllint-warnings-and-errors
7
8\title Incompatible type
9\brief [incompatible-type] An object, value, or expression is used on an incompatible type.
10
11\qmllintwarningcategory incompatible-type
12
13\section1 Cannot assign to default property of incompatible type
14
15\section2 What happened?
16You assigned an object to a
17\l{qtqml-syntax-objectattributes.html#default-properties}{default property}
18of an incompatible type.
19
20\section2 Why is this bad?
21The QML engine will not be able to assign the object at runtime.
22
23\section2 Example
24\qml
25import QtQuick
26
27Item {
28 component MyType: QtObject {
29 default property list<Item> myDefaultProperty
30 }
31
32 MyType {
33 QtObject {} // note: QtObject does not inherit from Item
34 }
35}
36
37\endqml
38To fix this warning, bind a compatible type to the property or, if
39you are the author of the default property, change the type in
40the definition:
41\qml
42import QtQuick
43
44Item {
45 component MyType: QtObject {
46 default property list<Item> myDefaultProperty
47 }
48
49 MyType {
50 Item {}
51 }
52
53 component AlternativeMyType: QtObject {
54 default property list<QtObject> myDefaultProperty
55 }
56
57 AlternativeMyType {
58 QtObject {} // is ok for AlternativeMyType
59 }
60}
61
62\endqml
63
64\section1 On-binding for property has wrong type
65
66\section2 What happened?
67You used an invalid
68\l{qtqml-cppintegration-definetypes.html#property-modifier-types}{property modifier type}.
69
70\section2 Why is this bad?
71The QML engine will not be able to use the property modifier type at runtime.
72
73\section2 Example
74\qml
75import QtQuick
76
77Item {
78 property int xxx
79 Item on xxx { ... }
80}
81
82\endqml
83To fix this warning, remove the \c on or use a valid
84\l{qtqml-cppintegration-definetypes.html#property-modifier-types}{property modifier type}:
85\qml
86import QtQuick
87
88Item {
89 property int xxx
90 Item { ... }
91
92 // Alternative: use a valid property modifier type
93 NumberAnimation on xxx { ... }
94}
95
96\endqml
97
98\section1 Construction from string is deprecated; Use structured value type construction instead
99
100\section2 What happened?
101You constructed a QML_STRUCTURED_VALUE using a string.
102
103\section2 Why is this bad?
104This is deprecated and prone to typos.
105
106\section2 Example
107\qml
108import QtQuick
109
110Item {
111 property point p: "5, 6"
112}
113
114\endqml
115To fix this warning, populate the structured value type as explained in the
116QML_STRUCTURED_VALUE description instead of binding a string to the property:
117
118\qml
119import QtQuick
120
121Item {
122 property point p: ({ x: 5, y: 6 })
123}
124
125\endqml
126
127\section1 Function without return type annotation returns
128
129\section2 What happened?
130You returned a value from a function without return type annotation.
131
132\section2 Why is this bad?
133You annotated the function to not return anything so the function should not return anything.
134The QML tooling will not be able to process the method and the QML engine will ignore the
135returned value in a future Qt version.
136
137\section2 Example
138\qml
139import QtQuick
140
141Item {
142 function f(x: int) {
143 ...
144 return x
145 }
146}
147
148\endqml
149To fix this warning, adapt the function signature to the new return type or remove the return value:
150\qml
151import QtQuick
152
153Item {
154 function f(x: int): int {
155 ...
156 return x
157 }
158 function alternativeF(x: int) {
159 ...
160 return
161 }
162}
163
164\endqml
165
166\section1 Cannot assign binding/object/literal
167
168\section2 What happened?
169You bound an object, literal, or expression to a property of an incompatible type.
170
171\section2 Why is this bad?
172The QML engine will not be able to assign the object, literal, or expression at runtime.
173
174\section2 Example
175\qml
176import QtQuick
177
178Item {
179 property date xxx: 42
180}
181
182\endqml
183To fix this warning, bind an object, value, or expression of a compatible type:
184\qml
185import QtQuick
186
187Item {
188 property date xxx: new Date()
189}
190
191\endqml
192
193*/