Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
shadowable-member.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2026 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 Shadowable member
8
\brief [shadowable-member] A member accessed by the compiler might be shadowed in a derived type.
9
10
\qmllintwarningcategory shadowable-member
11
12
\section1 Member can be shadowed
13
14
\section2 What happened?
15
The \l{Qt Quick Compiler}{compiler} accesses a non-final property or method through an
16
object whose exact type isn't known at compile time. A derived type might redeclare
17
("shadow") the member with a different type, and the QML engine would pick up that variant.
18
19
In the example below, the lookup for \c value on \c other can't be fully optimized because
20
\c other might hold an object of a type deriving from \c Base that shadows \c value with a
21
property of another type.
22
23
\section2 Why is that bad?
24
The compiler can't rely on the declared type of the member and has to treat it as \c var.
25
The generated code is slower.
26
27
This category is enabled automatically if the \l{compiler} category is enabled, unless it is
28
explicitly enabled or disabled.
29
30
\section2 Example
31
\qml
32
// Base.qml
33
import QtQml
34
35
QtObject {
36
property int value
37
}
38
\endqml
39
40
\qml
41
// Main.qml
42
import QtQml
43
44
QtObject {
45
property Base other
46
property int v: other.value
47
}
48
\endqml
49
50
Marking \c value as final guarantees that it can't be shadowed and allows the compiler to rely
51
on the type it sees. In QML, use the \c final keyword:
52
53
\qml
54
// Base.qml
55
import QtQml
56
57
QtObject {
58
final property int value
59
}
60
\endqml
61
62
In C++, declare the property \c FINAL:
63
64
\code
65
Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged FINAL)
66
\endcode
67
68
Alternatively, access the member through an object referenced by \c id, an attached property
69
or a singleton, where the exact type is known.
70
*/
qtdeclarative
src
qml
doc
src
qmllint
shadowable-member.qdoc
Generated on
for Qt by
1.18.0