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();
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();
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
125QAbstractObjectRegistryRefPrivate::QAbstractObjectRegistryRefPrivate(QQmlEngine *engine)
126{
127 m_registry = QObjectRegistrySingleton::registryForEngine(engine);
128}
129
130QString QAbstractObjectRegistryRefPrivate::key() const
131{
132 return m_key;
133}
134
135QObjectRegistrySingleton *QAbstractObjectRegistryRefPrivate::registry() const
136{
137 return m_registry;
138}
139
140void QAbstractObjectRegistryRefPrivate::dataAppend(QQmlListProperty<QObject> *l, QObject *o)
141{
142 auto *self = static_cast<QAbstractObjectRegistryRef *>(l->object);
143 self->d_func()->m_data.append(o);
144
145 emit self->dataChanged();
146}
147
148qsizetype QAbstractObjectRegistryRefPrivate::dataCount(QQmlListProperty<QObject> *l)
149{
150 return static_cast<QAbstractObjectRegistryRef *>(l->object)->d_func()->m_data.size();
151}
152
153QObject *QAbstractObjectRegistryRefPrivate::dataAt(QQmlListProperty<QObject> *l, qsizetype i)
154{
155 return static_cast<QAbstractObjectRegistryRef *>(l->object)->d_func()->m_data.at(i);
156}
157
158void QAbstractObjectRegistryRefPrivate::dataClear(QQmlListProperty<QObject> *l)
159{
160 auto *self = static_cast<QAbstractObjectRegistryRef *>(l->object);
161
162 if (self->d_func()->m_data.isEmpty())
163 return;
164
165 self->d_func()->m_data.clear();
166
167 emit self->dataChanged();
168}
169
170void QAbstractObjectRegistryRefPrivate::dataReplace(QQmlListProperty<QObject> *l, qsizetype i, QObject *o)
171{
172 auto *self = static_cast<QAbstractObjectRegistryRef *>(l->object);
173
174 if (o == self->d_func()->m_data.at(i))
175 return;
176
177 self->d_func()->m_data.replace(i, o);
178
179 emit self->dataChanged();
180}
181
182void QAbstractObjectRegistryRefPrivate::dataRemoveLast(QQmlListProperty<QObject> *l)
183{
184 auto *self = static_cast<QAbstractObjectRegistryRef *>(l->object);
185
186 if (self->d_func()->m_data.isEmpty())
187 return;
188
189 self->d_func()->m_data.removeLast();
190
191 emit self->dataChanged();
192}
193
194QT_END_NAMESPACE
Combined button and popup list for selecting options.