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
qabstractobjectregistryref.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 QAbstractObjectRegistryRef
12 \inmodule QtQmlDesignSupport
13 \ingroup qmldesignsupport
14 \brief The QAbstractObjectRegistryRef class is a base class for all
15 \QDesSup object reference classes.
16
17 This class can't be instantiated on its own, you must use one of the following derived
18 classes: QObjectRegistryRef or QMultiObjectRegistryRef
19
20 If multiple objects are registered with the same key, use the derived class
21 QMultiObjectRegistryRef. Typically this happens when delegate objects
22 instantiated by models are registered. If you know the key is used to register only a single
23 object, using the derived class QObjectRegistryRef is recommended.
24*/
25
26/*!
27 \qmltype AbstractObjectRegistryRef
28 \nativetype QAbstractObjectRegistryRef
29 \inqmlmodule QtQml.DesignSupport
30 \ingroup qmldesignsupport_qml
31 \brief Base type for all \QDesSup object reference types.
32
33 This type cannot be instantiated directly. Instead, use one of the following derived types:
34 ObjectRegistryRef or MultiObjectRegistryRef.
35
36 If multiple objects are registered with the same key, use the derived type
37 MultiObjectRegistryRef. Typically this happens when delegate objects
38 instantiated by models are registered. If you know the key is used to register only a single
39 object, using the derived type ObjectRegistryRef is recommended.
40*/
41
42/*!
43 \property QAbstractObjectRegistryRef::key
44 \brief The key of the registered object that this reference refers to.
45 \sa ObjectRegistry
46*/
47
48/*!
49 \qmlproperty string AbstractObjectRegistryRef::key
50 This property specifies the key of the registered object that this reference refers to.
51 \sa ObjectRegistry
52*/
53
54/*!
55 \property QAbstractObjectRegistryRef::data
56 \brief QML children of this type.
57 \internal
58*/
59
60
61QAbstractObjectRegistryRef::QAbstractObjectRegistryRef(QAbstractObjectRegistryRefPrivate &dd,
62 QObject *parent)
63 : QObject(dd, parent)
64{}
65
66QAbstractObjectRegistryRef::~QAbstractObjectRegistryRef()
67{
68 Q_D(QAbstractObjectRegistryRef);
69
70 if (d->m_registry)
71 d->m_registry->deregisterRef(d);
72}
73
74QString QAbstractObjectRegistryRef::key() const
75{
76 Q_D(const QAbstractObjectRegistryRef);
77
78 return d->m_key;
79}
80
81void QAbstractObjectRegistryRef::setKey(const QString &key)
82{
83 Q_D(QAbstractObjectRegistryRef);
84
85 if (key == d->m_key)
86 return;
87
88 if (!d->m_registry) {
89 d->m_registry = QObjectRegistrySingleton::registryForObject(this);
90 if (!d->m_registry) {
91 qWarning() << "Object registry could not be resolved for ("
92 << key
93 << ") when setting key to QAbstractObjectRegistryRef."
94 << " Most likely reason for this is that QML engine could not be resolved.";
95 d->m_key = key;
96 emit keyChanged(d->m_key);
97 return;
98 }
99 }
100
101 if (!d->m_key.isEmpty() && d->m_registry)
102 d->m_registry->deregisterRef(d);
103
104 d->m_key = key;
105
106 if (!d->m_key.isEmpty() && d->m_registry)
107 d->m_registry->registerRef(d);
108
109 d->handleInitialObjects();
110
111 emit keyChanged(d->m_key);
112}
113
114QQmlListProperty<QObject> QAbstractObjectRegistryRef::data()
115{
116 return { this, nullptr,
117 &QAbstractObjectRegistryRefPrivate::dataAppend,
118 &QAbstractObjectRegistryRefPrivate::dataCount,
119 &QAbstractObjectRegistryRefPrivate::dataAt,
120 &QAbstractObjectRegistryRefPrivate::dataClear,
121 &QAbstractObjectRegistryRefPrivate::dataReplace,
122 &QAbstractObjectRegistryRefPrivate::dataRemoveLast };
123}
124
125/*!
126 \reimp
127*/
128bool QAbstractObjectRegistryRef::event(QEvent *event)
129{
130 return QObject::event(event);
131}
132
133QAbstractObjectRegistryRefPrivate::QAbstractObjectRegistryRefPrivate(QQmlEngine *engine)
134{
135 m_registry = QObjectRegistrySingleton::registryForEngine(engine);
136}
137
138QString QAbstractObjectRegistryRefPrivate::key() const
139{
140 return m_key;
141}
142
143QObjectRegistrySingleton *QAbstractObjectRegistryRefPrivate::registry() const
144{
145 return m_registry;
146}
147
148void QAbstractObjectRegistryRefPrivate::dataAppend(QQmlListProperty<QObject> *l, QObject *o)
149{
150 auto *self = static_cast<QAbstractObjectRegistryRef *>(l->object);
151 self->d_func()->m_data.append(o);
152
153 emit self->dataChanged();
154}
155
156qsizetype QAbstractObjectRegistryRefPrivate::dataCount(QQmlListProperty<QObject> *l)
157{
158 return static_cast<QAbstractObjectRegistryRef *>(l->object)->d_func()->m_data.size();
159}
160
161QObject *QAbstractObjectRegistryRefPrivate::dataAt(QQmlListProperty<QObject> *l, qsizetype i)
162{
163 return static_cast<QAbstractObjectRegistryRef *>(l->object)->d_func()->m_data.at(i);
164}
165
166void QAbstractObjectRegistryRefPrivate::dataClear(QQmlListProperty<QObject> *l)
167{
168 auto *self = static_cast<QAbstractObjectRegistryRef *>(l->object);
169
170 if (self->d_func()->m_data.isEmpty())
171 return;
172
173 self->d_func()->m_data.clear();
174
175 emit self->dataChanged();
176}
177
178void QAbstractObjectRegistryRefPrivate::dataReplace(QQmlListProperty<QObject> *l, qsizetype i, QObject *o)
179{
180 auto *self = static_cast<QAbstractObjectRegistryRef *>(l->object);
181
182 if (o == self->d_func()->m_data.at(i))
183 return;
184
185 self->d_func()->m_data.replace(i, o);
186
187 emit self->dataChanged();
188}
189
190void QAbstractObjectRegistryRefPrivate::dataRemoveLast(QQmlListProperty<QObject> *l)
191{
192 auto *self = static_cast<QAbstractObjectRegistryRef *>(l->object);
193
194 if (self->d_func()->m_data.isEmpty())
195 return;
196
197 self->d_func()->m_data.removeLast();
198
199 emit self->dataChanged();
200}
201
202QT_END_NAMESPACE
Combined button and popup list for selecting options.