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
158/*!
159 \reimp
160*/
161bool QMultiObjectRegistryRef::event(QEvent *event)
162{
163 return QAbstractObjectRegistryRef::event(event);
164}
165
166void QMultiObjectRegistryRefPrivate::handleInitialObjects()
167{
168 Q_Q(QMultiObjectRegistryRef);
169
170 if (!registry())
171 return;
172
173 QList<QObject *> oldObjects = m_objects;
174 QList<QObject *> newObjects = registry()->objects(key()).values();
175
176 if (oldObjects == newObjects)
177 return;
178
179 m_objects = newObjects;
180
181 for (const auto obj : std::as_const(oldObjects))
182 newObjects.removeOne(obj);
183 for (const auto obj : std::as_const(m_objects))
184 oldObjects.removeOne(obj);
185
186 for (const auto obj : std::as_const(oldObjects))
187 emit q->objectRemoved(obj);
188
189 for (const auto obj : std::as_const(newObjects))
190 emit q->objectAdded(obj);
191
192 emit q->objectsChanged();
193}
194
195QMultiObjectRegistryRefPrivate::QMultiObjectRegistryRefPrivate(QQmlEngine *engine)
196 : QAbstractObjectRegistryRefPrivate(engine)
197{}
198
199void QMultiObjectRegistryRefPrivate::handleObjectAdded(QObject *obj)
200{
201 Q_Q(QMultiObjectRegistryRef);
202
203 m_objects.append(obj);
204
205 emit q->objectAdded(obj);
206 emit q->objectsChanged();
207}
208
209void QMultiObjectRegistryRefPrivate::handleObjectRemoved(QObject *obj)
210{
211 Q_Q(QMultiObjectRegistryRef);
212
213 m_objects.removeOne(obj);
214
215 emit q->objectRemoved(obj);
216 emit q->objectsChanged();
217}
218
219qsizetype QMultiObjectRegistryRefPrivate::objectsCount(QQmlListProperty<QObject> *l)
220{
221 return static_cast<QMultiObjectRegistryRef *>(l->object)->d_func()->m_objects.size();
222}
223
224QObject *QMultiObjectRegistryRefPrivate::objectsAt(QQmlListProperty<QObject> *l, qsizetype i)
225{
226 return static_cast<QMultiObjectRegistryRef *>(l->object)->d_func()->m_objects.at(i);
227}
228
229QT_END_NAMESPACE
Combined button and popup list for selecting options.