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