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