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