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
9
Attached properties and signals are a mechanism for objects to be annotated with extra
10
properties or signals that are not part of their base type. Attached properties are
11
accessed through a namespaced syntax that identifies the type providing the attached
12
properties.
13
14
\section1 Overview
15
16
Some types have properties or signals that can be \e{attached} to other objects, providing
17
additional functionality without requiring inheritance. An attached property or signal is
18
accessed by prefixing the property or signal name with the name of the attaching type.
19
20
For example, the \l ListView type has an attached property called \c isCurrentItem that
21
can be accessed on any item within the ListView's delegate:
22
23
\qml
24
ListView {
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
36
Here, \c{ListView.isCurrentItem} is an attached property that is made available to each
37
delegate item by the ListView.
38
39
\section1 Attached Properties
40
41
Attached properties allow you to add properties to objects that are otherwise defined
42
by a base type. The property values are stored externally to the object and accessed
43
through the attaching type's name.
44
45
Common 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
55
Similar to attached properties, attached signals allow types to provide additional
56
signal handlers to objects. These are commonly used for lifecycle events.
57
58
For example, the Component type provides attached signals:
59
60
\qml
61
Rectangle {
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
74
To 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
83
Example:
84
85
\code
86
class MyAttachedType : public QObject
87
{
88
Q_OBJECT
89
Q_PROPERTY(int value READ value WRITE setValue NOTIFY valueChanged)
90
QML_ANONYMOUS
91
92
public:
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
104
signals:
105
void valueChanged();
106
107
private:
108
int m_value = 0;
109
};
110
111
class MyType : public QObject
112
{
113
Q_OBJECT
114
QML_ELEMENT
115
QML_ATTACHED(MyAttachedType)
116
117
public:
118
static MyAttachedType *qmlAttachedProperties(QObject *object)
119
{
120
return new MyAttachedType(object);
121
}
122
};
123
\endcode
124
125
Usage in QML:
126
127
\qml
128
Item {
129
MyType.value: 42
130
}
131
\endqml
132
133
If the attaching type (\c{MyType} in this case) does nothing but attach
134
properties to other objects, you can phrase this as a single class:
135
136
\code
137
class 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
144
public:
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
161
signals:
162
void valueChanged();
163
164
private:
165
int m_value = 0;
166
};
167
\endcode
168
169
\section1 When to Use Attached Properties
170
171
Attached 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
*/
qtdeclarative
src
qml
doc
src
qmllanguageref
typesystem
attachedtypes.qdoc
Generated on
for Qt by
1.16.1