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
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?
15The \l{Qt Quick Compiler}{compiler} accesses a non-final property or method through an
16object 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
19In 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
21property of another type.
22
23\section2 Why is that bad?
24The compiler can't rely on the declared type of the member and has to treat it as \c var.
25The generated code is slower.
26
27This category is enabled automatically if the \l{compiler} category is enabled, unless it is
28explicitly enabled or disabled.
29
30\section2 Example
31\qml
32// Base.qml
33import QtQml
34
35QtObject {
36 property int value
37}
38\endqml
39
40\qml
41// Main.qml
42import QtQml
43
44QtObject {
45 property Base other
46 property int v: other.value
47}
48\endqml
49
50Marking \c value as final guarantees that it can't be shadowed and allows the compiler to rely
51on the type it sees. In QML, use the \c final keyword:
52
53\qml
54// Base.qml
55import QtQml
56
57QtObject {
58 final property int value
59}
60\endqml
61
62In C++, declare the property \c FINAL:
63
64\code
65Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged FINAL)
66\endcode
67
68Alternatively, access the member through an object referenced by \c id, an attached property
69or a singleton, where the exact type is known.
70*/