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