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
9#include <QtQml/qqmlengine.h>
10
12
13/*!
14 \qmltype ObjectRegistry
15 \inqmlmodule QtQml.DesignSupport
16 \ingroup qmldesignsupport_qml
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
52QObjectRegistryAttachedType::QObjectRegistryAttachedType(QObject *parent)
53 : QObject(parent)
54{
55 m_registry = QObjectRegistrySingleton::registryForObject(parent);
56}
57
58QObjectRegistryAttachedType::~QObjectRegistryAttachedType()
59{
60 if (m_registry)
61 m_registry->remove(m_key, parent());
62}
63
64QString QObjectRegistryAttachedType::key() const
65{
66 return m_key;
67}
68
69void QObjectRegistryAttachedType::setKey(const QString &key)
70{
71 if (m_key == key)
72 return;
73
74 if (m_qmlSetupInProgress) {
75 m_key = key;
76 emit keyChanged();
77 return;
78 }
79
80 if (m_registry)
81 m_registry->remove(m_key, parent());
82
83 m_key = key;
84
85 if (m_registry)
86 m_registry->add(m_key, parent());
87
88 emit keyChanged();
89}
90
91void QObjectRegistryAttachedType::classBegin()
92{
93 m_qmlSetupInProgress = true;
94}
95
96void QObjectRegistryAttachedType::componentComplete()
97{
98 m_qmlSetupInProgress = false;
99
100 if (m_registry)
101 m_registry->add(m_key, parent());
102}
103
104QObjectRegistry::QObjectRegistry(QObject *parent)
105 : QObject(parent)
106{
107}
108
109QObjectRegistry::~QObjectRegistry()
110{
111 if (m_registry)
112 m_registry->remove(m_key, m_target);
113}
114
115QObjectRegistryAttachedType *QObjectRegistry::qmlAttachedProperties(QObject *object)
116{
117 return new QObjectRegistryAttachedType(object);
118}
119
120QString QObjectRegistry::key() const
121{
122 return m_key;
123}
124
125void QObjectRegistry::setKey(const QString &key)
126{
127 if (m_key == key)
128 return;
129
130 if (!m_target) {
131 m_key = key;
132 emit keyChanged();
133 return; // Postpone actual handling until we have both key and target
134 }
135
136 if (m_registry)
137 m_registry->remove(m_key, m_target);
138
139 m_key = key;
140
141 if (m_registry)
142 m_registry->add(m_key, m_target);
143
144 emit keyChanged();
145}
146
147QObject *QObjectRegistry::target() const
148{
149 return m_target;
150}
151
152void QObjectRegistry::setTarget(QObject *target)
153{
154 if (m_target == target)
155 return;
156
157 if (m_key.isEmpty()) {
158 m_target = target;
159 emit targetChanged();
160 return; // Postpone actual handling until we have both key and target
161 }
162
163 if (!m_registry)
164 m_registry = QObjectRegistrySingleton::registryForObject(this);
165
166 if (m_registry)
167 m_registry->remove(m_key, m_target);
168
169 m_target = target;
170
171 if (m_registry)
172 m_registry->add(m_key, m_target);
173
174 emit targetChanged();
175}
176
177QT_END_NAMESPACE
Combined button and popup list for selecting options.