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
attached-property-reuse.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-attached-property-reuse.html
6
\ingroup qmllint-warnings-and-errors
7
8
\title Attached property reuse
9
\brief [attached-property-reuse] Attached type was initialized multiple times.
10
11
\qmllintwarningcategory attached-property-reuse
12
13
\section1 Using attached type already initialized in a parent scope
14
15
\section2 What happened?
16
You initialized a propagating
17
\l{Attached Properties and Attached Signal Handlers}{attached type}
18
multiple times.
19
20
\note This mostly happens for attached types that inherit from
21
\l{QQuickAttachedPropertyPropagator}.
22
23
\section2 Why is this bad?
24
Propagating attached objects consume memory for each instantiation but only need to be initialized once.
25
26
\section2 Example
27
\qml
28
import QtQuick
29
import QtQuick.Templates as T
30
import QtQuick.Controls.Material // contains the Material attached type
31
32
T.ToolBar {
33
id: control
34
35
// first instantiation of Material's attached property
36
property color c: Material.toolBarColor
37
38
background: Rectangle {
39
// second instantiation of Material's attached property, wrong!
40
color: Material.toolBarColor
41
}
42
}
43
44
\endqml
45
To fix this warning, query the attached type from the parent:
46
\qml
47
import QtQuick
48
import QtQuick.Templates as T
49
import QtQuick.Controls.Material // contains the Material attached type
50
51
T.ToolBar {
52
id: control
53
54
// first instantiation of Material's attached property
55
property color c: Material.toolBarColor
56
57
background: Rectangle {
58
// use control's attached property, correct!
59
color: control.Material.toolBarColor
60
}
61
}
62
\endqml
63
*/
qtdeclarative
src
qml
doc
src
qmllint
attached-property-reuse.qdoc
Generated on
for Qt by
1.14.0