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?
16You initialized a propagating
17\l{Attached Properties and Attached Signal Handlers}{attached type}
18multiple times.
19
20\note This mostly happens for attached types that inherit from
21\l{QQuickAttachedPropertyPropagator}.
22
23\section2 Why is this bad?
24Propagating attached objects consume memory for each instantiation but only need to be initialized once.
25
26\section2 Example
27\qml
28import QtQuick
29import QtQuick.Templates as T
30import QtQuick.Controls.Material // contains the Material attached type
31
32T.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
45To fix this warning, query the attached type from the parent:
46\qml
47import QtQuick
48import QtQuick.Templates as T
49import QtQuick.Controls.Material // contains the Material attached type
50
51T.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*/