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
qmultiobjectregistryref.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/qobjectregistrysingleton_p.h>
7
9
10/*!
11 \class QMultiObjectRegistryRef
12 \inmodule QtQmlDesignSupport
13 \ingroup qmldesignsupport
14 \brief This class is used to access objects registered with ObjectRegistry QML type from
15 C++ code.
16
17 Multiple objects registered with the same key can be accessed via this reference.
18
19 Example usage:
20 \snippet doc_src_objectregistry.cpp 3
21 \codeline
22 \snippet doc_src_objectregistry.cpp 5
23
24 \sa ObjectRegistry, QAbstractObjectRegistryRef::key
25*/
26
27/*!
28 \qmltype MultiObjectRegistryRef
29 \nativetype QObjectRegistryRef
30 \inqmlmodule QtQml.DesignSupport
31 \ingroup qmldesignsupport_qml
32 \inherits AbstractObjectRegistryRef
33 \brief This type is used to access objects registered with ObjectRegistry type.
34
35 Multiple objects registered with the same key can be accessed via this reference.
36
37 Example usage:
38 \snippet doc_src_objectregistry.cpp 3
39 \codeline
40 \snippet doc_src_objectregistry.cpp 4
41
42 \sa ObjectRegistry, AbstractObjectRegistryRef::key
43*/
44
45/*!
46 \property QMultiObjectRegistryRef::objects
47
48 This property provides the objects referenced by this instance.
49 The order of the objects in this list is not guaranteed to match the order of their
50 registration.
51
52 \sa objectsList(), ObjectRegistry, AbstractObjectRegistryRef::key
53*/
54
55/*!
56 \qmlproperty list<QtObject> MultiObjectRegistryRef::objects
57
58 This property provides the objects referenced by this instance.
59 The order of the objects in this list is not guaranteed to match the order of their
60 registration.
61
62 \sa ObjectRegistry, AbstractObjectRegistryRef::key
63*/
64
65/*!
66 Constructs QMultiObjectRegistryRef instance with \a parent.
67
68 \note Use this constructor only if the correct QML engine resolves automatically for this
69 instance when the key is set. This is typically not the case when constructing an instance
70 of this class from C++.
71 It is recommended to use one of the constructors that take a QML engine pointer as a parameter.
72
73 \sa qmlEngine(), QAbstractObjectRegistryRef::key
74*/
75QMultiObjectRegistryRef::QMultiObjectRegistryRef(QObject *parent)
76 : QAbstractObjectRegistryRef(*new QMultiObjectRegistryRefPrivate(), parent)
77{}
78
79/*!
80 Constructs QMultiObjectRegistryRef instance with QML \a engine and \a parent.
81 All registrations are scoped to the specified QML engine.
82
83 \sa QAbstractObjectRegistryRef::key
84*/
85QMultiObjectRegistryRef::QMultiObjectRegistryRef(QQmlEngine *engine, QObject *parent)
86 : QAbstractObjectRegistryRef(*new QMultiObjectRegistryRefPrivate(engine), parent)
87{}
88
89/*!
90 Constructs QMultiObjectRegistryRef instance with QML \a engine, \a key and \a parent.
91 All registrations are scoped to the specified QML engine.
92
93 \sa QAbstractObjectRegistryRef::key
94*/
95QMultiObjectRegistryRef::QMultiObjectRegistryRef(QQmlEngine *engine, const QString &key, QObject *parent)
96 : QAbstractObjectRegistryRef(*new QMultiObjectRegistryRefPrivate(engine), parent)
97{
98 setKey(key);
99}
100
101QMultiObjectRegistryRef::~QMultiObjectRegistryRef() = default;
102
103QQmlListProperty<QObject> QMultiObjectRegistryRef::objects()
104{
105 return { this, nullptr,
106 &QMultiObjectRegistryRefPrivate::objectsCount,
107 &QMultiObjectRegistryRefPrivate::objectsAt };
108}
109
110/*!
111 \return the list of registered objects matching the key of this instance.
112
113 This is a method to get the list of registered objects as a QList.
114
115 The order of the objects in this list is not guaranteed to match the order of their
116 registration.
117
118 \sa QAbstractObjectRegistryRef::key, objects
119*/
120QList<QObject *> QMultiObjectRegistryRef::objectsList()
121{
122 Q_D(const QMultiObjectRegistryRef);
123
124 return d->m_objects;
125}
126
127/*!
128 \fn void QMultiObjectRegistryRef::objectAdded(QObject *obj)
129
130 This signal is emitted when a new object, \a obj, has been registered with the key of this
131 reference.
132*/
133
134/*!
135 \qmlsignal MultiObjectRegistryRef::objectAdded(QtObject obj)
136
137 This signal is emitted when a new object, \a obj, has been registered with the key of this
138 reference.
139*/
140
141/*!
142 \fn void QMultiObjectRegistryRef::objectRemoved(QObject *obj)
143
144 This signal is emitted when an object, \a obj, registration has been removed with the key of this
145 reference.
146*/
147
148/*!
149 \qmlsignal MultiObjectRegistryRef::objectRemoved(QtObject obj)
150
151 This signal is emitted when an object, \a obj, registration has been removed with the key of this
152 reference.
153
154 \note If the registration removal was triggered by the object deletion, the \a obj parameter
155 will be null as QML is not able to resolve an object being deleted.
156*/
157
158void QMultiObjectRegistryRefPrivate::handleInitialObjects()
159{
160 Q_Q(QMultiObjectRegistryRef);
161
162 if (!registry())
163 return;
164
165 QList<QObject *> oldObjects = m_objects;
166 QList<QObject *> newObjects = registry()->objects(key()).values();
167
168 if (oldObjects == newObjects)
169 return;
170
171 m_objects = newObjects;
172
173 for (const auto obj : std::as_const(oldObjects))
174 newObjects.removeOne(obj);
175 for (const auto obj : std::as_const(m_objects))
176 oldObjects.removeOne(obj);
177
178 for (const auto obj : std::as_const(oldObjects))
179 emit q->objectRemoved(obj);
180
181 for (const auto obj : std::as_const(newObjects))
182 emit q->objectAdded(obj);
183
184 emit q->objectsChanged();
185}
186
187QMultiObjectRegistryRefPrivate::QMultiObjectRegistryRefPrivate(QQmlEngine *engine)
188 : QAbstractObjectRegistryRefPrivate(engine)
189{}
190
191void QMultiObjectRegistryRefPrivate::handleObjectAdded(QObject *obj)
192{
193 Q_Q(QMultiObjectRegistryRef);
194
195 m_objects.append(obj);
196
197 emit q->objectAdded(obj);
198 emit q->objectsChanged();
199}
200
201void QMultiObjectRegistryRefPrivate::handleObjectRemoved(QObject *obj)
202{
203 Q_Q(QMultiObjectRegistryRef);
204
205 m_objects.removeOne(obj);
206
207 emit q->objectRemoved(obj);
208 emit q->objectsChanged();
209}
210
211qsizetype QMultiObjectRegistryRefPrivate::objectsCount(QQmlListProperty<QObject> *l)
212{
213 return static_cast<QMultiObjectRegistryRef *>(l->object)->d_func()->m_objects.size();
214}
215
216QObject *QMultiObjectRegistryRefPrivate::objectsAt(QQmlListProperty<QObject> *l, qsizetype i)
217{
218 return static_cast<QMultiObjectRegistryRef *>(l->object)->d_func()->m_objects.at(i);
219}
220
221QT_END_NAMESPACE
Combined button and popup list for selecting options.