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
attachedtypes.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\page qtqml-typesystem-attachedtypes.html
6\title QML Attached Types
7\brief Using attached properties and signals in QML
8
9Attached properties and signals are a mechanism for objects to be annotated with extra
10properties or signals that are not part of their base type. Attached properties are
11accessed through a namespaced syntax that identifies the type providing the attached
12properties.
13
14\section1 Overview
15
16Some types have properties or signals that can be \e{attached} to other objects, providing
17additional functionality without requiring inheritance. An attached property or signal is
18accessed by prefixing the property or signal name with the name of the attaching type.
19
20For example, the \l ListView type has an attached property called \c isCurrentItem that
21can be accessed on any item within the ListView's delegate:
22
23\qml
24ListView {
25 width: 240
26 height: 320
27 model: 3
28 delegate: Rectangle {
29 width: 100
30 height: 30
31 color: ListView.isCurrentItem ? "red" : "yellow"
32 }
33}
34\endqml
35
36Here, \c{ListView.isCurrentItem} is an attached property that is made available to each
37delegate item by the ListView.
38
39\section1 Attached Properties
40
41Attached properties allow you to add properties to objects that are otherwise defined
42by a base type. The property values are stored externally to the object and accessed
43through the attaching type's name.
44
45Common examples include:
46\list
47\li \l{ListView} attached properties (\c isCurrentItem, \c view)
48\li \l{GridView} attached properties
49\li \l{Keys} attached properties for keyboard event handling
50\li \l{Component} attached properties (\c onCompleted, \c onDestruction)
51\endlist
52
53\section1 Attached Signals
54
55Similar to attached properties, attached signals allow types to provide additional
56signal handlers to objects. These are commonly used for lifecycle events.
57
58For example, the Component type provides attached signals:
59
60\qml
61Rectangle {
62 Component.onCompleted: {
63 console.log("Rectangle creation completed")
64 }
65
66 Component.onDestruction: {
67 console.log("Rectangle is being destroyed")
68 }
69}
70\endqml
71
72\section1 Creating Attached Properties in C++
73
74To create attached properties for your own types from C++, you need to:
75
76\list 1
77\li Create an attached properties class that inherits from QObject
78\li Implement the attached properties in this class
79\li Add the \c QML_ATTACHED macro to your main type
80\li Implement a static \c qmlAttachedProperties() method
81\endlist
82
83Example:
84
85\code
86class MyAttachedType : public QObject
87{
88 Q_OBJECT
89 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
90 QML_ANONYMOUS
91
92public:
93 MyAttachedType(QObject *parent = nullptr) : QObject(parent) {}
94
95 int value() const { return m_value; }
96 void setValue(int value)
97 {
98 if (m_value != value) {
99 m_value = value;
100 emit valueChanged();
101 }
102 }
103
104signals:
105 void valueChanged();
106
107private:
108 int m_value = 0;
109};
110
111class MyType : public QObject
112{
113 Q_OBJECT
114 QML_ELEMENT
115 QML_ATTACHED(MyAttachedType)
116
117public:
118 static MyAttachedType *qmlAttachedProperties(QObject *object)
119 {
120 return new MyAttachedType(object);
121 }
122};
123\endcode
124
125Usage in QML:
126
127\qml
128Item {
129 MyType.value: 42
130}
131\endqml
132
133If the attaching type (\c{MyType} in this case) does nothing but attach
134properties to other objects, you can phrase this as a single class:
135
136\code
137class MyType : public QObject
138{
139 Q_OBJECT
140 Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
141 QML_ELEMENT
142 QML_ATTACHED(MyType)
143
144public:
145 static MyType *qmlAttachedProperties(QObject *object)
146 {
147 return new MyType(object);
148 }
149
150 MyType(QObject *parent = nullptr) : QObject(parent) {}
151
152 int value() const { return m_value; }
153 void setValue(int value)
154 {
155 if (m_value != value) {
156 m_value = value;
157 emit valueChanged();
158 }
159 }
160
161signals:
162 void valueChanged();
163
164private:
165 int m_value = 0;
166};
167\endcode
168
169\section1 When to Use Attached Properties
170
171Attached properties are useful when:
172\list
173\li You want to add context-specific information to items (like ListView's \c isCurrentItem)
174\li Properties logically belong to a container or context rather than the item itself
175\li You want to avoid cluttering base types with specialized properties
176\endlist
177
178\sa {QML Object Types}, {The QML Type System}, {Defining QML Types from C++}
179*/