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