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?
16You assigned an object to a non-existing default property.
17
18\section2 Why is this bad?
19The QML engine can't assign this object at runtime.
20
21\section2 Example
22\qml
23import QtQuick
24
25Item {
26 component MyType: QtObject { property Item myItem; }
27
28 MyType {
29 Item {}
30 }
31}
32
33\endqml
34To fix this warning, specify the property you want to bind to or, if you are the author
35of the type, mark a property as default:
36\qml
37import QtQuick
38
39Item {
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?
57You assigned an expression to a non-existing property.
58
59\section2 Why is this bad?
60The QML engine can't assign this expression at runtime.
61
62\section2 Example
63\qml
64import QtQuick
65
66Item {
67 property int myInt
68 myItn: 42
69}
70
71\endqml
72To fix this warning, remove the binding or correct a possible typo:
73\qml
74import QtQuick
75
76Item {
77 property int myInt
78 myInt: 42
79}
80
81\endqml
82
83\section1 Member not found on type
84
85\section2 What happened?
86You accessed a member in a field member expression that can't be found by QML tooling.
87A field member expression is an expression of the form \c {someId.someProperty}.
88
89The member might not exist at all, for example when you made a typo. Or it might exist
90at runtime (because it exists on a specific sub-type that is always present), but not
91on the statically declared type.
92
93\section2 Why is this bad?
94The QML tooling can't find this member, meaning that features like go-to-definition and
95autocomplete won't work.
96At runtime, there might be an error or you might get an incorrect result if the member
97really does not exist.
98There won't be an error if the member actually always exist at runtime, however in that
99case 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
105import QtQuick
106
107Item {
108 id: self
109 property int myInt
110 property int myInt2: 1 + self.myItn
111}
112
113\endqml
114To fix this warning, remove the binding or correct a possible typo:
115\qml
116import QtQuick
117
118Item {
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
127import QtQuick
128import QtQuick.Controls.Basic
129
130Item {
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
146To fix this warning, \l{Type annotations and assertions}{cast the object} you
147read from to the more specific type:
148
149\qml
150import QtQuick
151import QtQuick.Controls.Basic
152
153Item {
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
169In the example, the declared type of \c{messageView}'s
170\l{ListView::}{currentItem} property is \l Item, which has no \c sender
171property: \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*/