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