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