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
qobjectregistry.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5
6#include <private/qabstractobjectregistryref_p.h>
7#include <private/qobjectregistrysingleton_p.h>
8#include <private/qqmldata_p.h>
9
10#include <QtQml/qqmlengine.h>
11
13
14/*!
15 \qmltype ObjectRegistry
16 \inqmlmodule QtQml.DesignSupport
17 \ingroup qmldesignsupport_qml
18 \brief This type is used to register objects for later access based on a key string.
19
20 The object registry provides a QML engine-wide reference for objects. You register each object
21 with a string key, which you can use later to retrieve the object through one of the following
22 reference types: ObjectRegistryRef and MultiObjectRegistryRef. C++ classes are also provided
23 for accessing registered objects: QObjectRegistryRef and QMultiObjectRegistryRef.
24
25 The same key can be used to register multiple objects. A typical use case for this is
26 registering a list or repeater delegate.
27
28 The registered object is automatically removed from the registry on either object
29 destruction or ObjectRegistry instance destruction. Changing the target object or the key
30 removes the old combination from the registry.
31
32 Do not register the same object and key combination more than once. Only one registration per
33 combination is stored.
34
35 The registration can be done either with attached property on the registered object or
36 using separate ObjectRegistry object with target and key:
37
38 \snippet doc_src_objectregistry.cpp 6
39
40 \sa ObjectRegistryRef, MultiObjectRegistryRef, QObjectRegistryRef, QMultiObjectRegistryRef
41*/
42
43/*!
44 \qmlproperty string ObjectRegistry::key
45 This property specifies the key of the registered object.
46*/
47
48/*!
49 \qmlproperty QtObject ObjectRegistry::target
50 This property specifies the target object to register.
51
52 If the target object is destroyed, this property is reset to null.
53*/
54
55QObjectRegistryAttachedType::QObjectRegistryAttachedType(QObject *parent)
56 : QObject(parent)
57{
58 m_registry = QObjectRegistrySingleton::registryForObject(parent);
59}
60
61QObjectRegistryAttachedType::~QObjectRegistryAttachedType()
62{
63 if (m_registry)
64 m_registry->remove(m_key, parent());
65}
66
67QString QObjectRegistryAttachedType::key() const
68{
69 return m_key;
70}
71
72void QObjectRegistryAttachedType::setKey(const QString &key)
73{
74 if (m_key == key)
75 return;
76
77 if (m_qmlSetupInProgress) {
78 m_key = key;
79 emit keyChanged();
80 return;
81 }
82
83 if (m_registry)
84 m_registry->remove(m_key, parent());
85
86 m_key = key;
87
88 if (m_registry)
89 m_registry->add(m_key, parent());
90
91 emit keyChanged();
92}
93
94void QObjectRegistryAttachedType::classBegin()
95{
96 m_qmlSetupInProgress = true;
97}
98
99void QObjectRegistryAttachedType::componentComplete()
100{
101 m_qmlSetupInProgress = false;
102
103 if (m_registry)
104 m_registry->add(m_key, parent());
105}
106
107QObjectRegistry::QObjectRegistry(QObject *parent)
108 : QObject(parent)
109{
110}
111
112QObjectRegistry::~QObjectRegistry()
113{
114 if (m_registry)
115 m_registry->remove(m_key, m_target);
116}
117
118QObjectRegistryAttachedType *QObjectRegistry::qmlAttachedProperties(QObject *object)
119{
120 return new QObjectRegistryAttachedType(object);
121}
122
123QString QObjectRegistry::key() const
124{
125 return m_key;
126}
127
128void QObjectRegistry::setKey(const QString &key)
129{
130 if (m_key == key)
131 return;
132
133 if (!m_target) {
134 m_key = key;
135 emit keyChanged();
136 return; // Postpone actual handling until we have both key and target
137 }
138
139 if (!m_registry)
140 m_registry = QObjectRegistrySingleton::registryForObject(this);
141
142 if (m_registry)
143 m_registry->remove(m_key, m_target);
144
145 m_key = key;
146
147 if (m_registry)
148 m_registry->add(m_key, m_target);
149
150 emit keyChanged();
151}
152
153QObject *QObjectRegistry::target() const
154{
155 return m_target.object();
156}
157
158void QObjectRegistry::setTarget(QObject *target)
159{
160 if (QQmlData::wasDeleted(target))
161 target = nullptr;
162
163 if (m_target == target)
164 return;
165
166 if (m_key.isEmpty()) {
167 m_target.setObject(target);
168 emit targetChanged();
169 return; // Postpone actual handling until we have both key and target
170 }
171
172 if (!m_registry)
173 m_registry = QObjectRegistrySingleton::registryForObject(this);
174
175 if (m_registry)
176 m_registry->remove(m_key, m_target);
177
178 m_target.setObject(target);
179
180 if (m_registry)
181 m_registry->add(m_key, m_target);
182
183 emit targetChanged();
184}
185
186QT_END_NAMESPACE
Combined button and popup list for selecting options.