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-property.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 property
8
\brief [missing-property] Binding a value to a non-existing property.
9
10
\qmllintwarningcategory missing-property
11
12
\section1 Can't assign to non-existent default property
13
14
\section2 What happened?
15
You assigned an object to a non-existing default property.
16
17
\section2 Why is this bad?
18
The QML engine can't assign this object at runtime.
19
20
\section2 Example
21
\qml
22
import QtQuick
23
24
Item {
25
component MyType: QtObject { property Item myItem; }
26
27
MyType {
28
Item {}
29
}
30
}
31
32
\endqml
33
To fix this warning, specify the property you want to bind to or, if you are the author
34
of the type, mark a property as default:
35
\qml
36
import QtQuick
37
38
Item {
39
component MyType: QtObject { property Item myItem; }
40
41
MyType {
42
myItem: Item {}
43
}
44
45
component AlternativeMyType: QtObject { default property Item myItem; }
46
47
AlternativeMyType {
48
Item {} // bound to myItem via default property
49
}
50
}
51
\endqml
52
53
\section1 Property does not exist
54
55
\section2 What happened?
56
You assigned an expression to a non-existing property.
57
58
\section2 Why is this bad?
59
The QML engine can't assign this expression at runtime.
60
61
\section2 Example
62
\qml
63
import QtQuick
64
65
Item {
66
property int myInt
67
myItn: 42
68
}
69
70
\endqml
71
To fix this warning, remove the binding or correct a possible typo:
72
\qml
73
import QtQuick
74
75
Item {
76
property int myInt
77
myInt: 42
78
}
79
80
\endqml
81
82
\section1 Member not found on type
83
84
\section2 What happened?
85
You accessed a member in a field member expression that can't be found by QML tooling.
86
A field member expression is an expression of the form \c {someId.someProperty}.
87
88
The member might not exist at all, for example when you made a typo. Or it might exist
89
at runtime (because it exists on a specific sub-type that is always present), but not
90
on the statically declared type.
91
92
\section2 Why is this bad?
93
The QML tooling can't find this member, meaning that features like go-to-definition and
94
autocomplete won't work.
95
At runtime, there might be an error or you might get an incorrect result if the member
96
really does not exist.
97
There won't be an error if the member actually always exist at runtime, however in that
98
case there can still be a performance cost due to missed optimizations.
99
100
\section2 Example
101
102
\section3 Property does not actually exist
103
\qml
104
import QtQuick
105
106
Item {
107
id: self
108
property int myInt
109
property int myInt2: 1 + self.myItn
110
}
111
112
\endqml
113
To fix this warning, remove the binding or correct a possible typo:
114
\qml
115
import QtQuick
116
117
Item {
118
id: self
119
property int myInt
120
property int myInt2: 1 + self.myInt
121
}
122
\endqml
123
124
\section3 Property exists, but type is not precise enough
125
\qml
126
import QtQuick
127
import QtQuick.Controls.Basic
128
129
Item {
130
component Message : Item {
131
required property string sender
132
required property string text
133
}
134
ListView {
135
id: messageView
136
delegate: Message {}
137
}
138
139
Button {
140
text: "Reply to %1".arg(messageView.currentItem.sender) // not ok
141
}
142
}
143
\endqml
144
145
To fix this warning, \l{Type annotations and assertions}{cast the object} you
146
read from to the more specific type:
147
148
\qml
149
import QtQuick
150
import QtQuick.Controls.Basic
151
152
Item {
153
component Message : Item {
154
required property string sender
155
required property string text
156
}
157
ListView {
158
id: messageView
159
delegate: Message {}
160
}
161
162
Button {
163
text: "Reply to %1".arg((messageView.currentItem as Message).sender) // now ok
164
}
165
}
166
\endqml
167
168
In the example, the declared type of \c{messageView}'s
169
\l{ListView::}{currentItem} property is \l Item, which has no \c sender
170
property: \c sender is defined only in \c Message. Here, we know however that
171
\c currentItem always contains a \c Message, so we can cast \c currentItem to
172
\c Message using a \l{Type annotations and assertions}{type assertion}.
173
174
*/
qtdeclarative
src
qml
doc
src
qmllint
missing-property.qdoc
Generated on
for Qt by
1.16.1