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