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
qobjectregistryref.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
8#include <QtQml/qqmlinfo.h>
9
11
12/*!
13 \class QObjectRegistryRef
14 \inmodule QtQmlDesignSupport
15 \ingroup qmldesignsupport
16 \brief This class is used to access an object registered with ObjectRegistry QML type from
17 C++ code.
18
19 Example usage:
20 \snippet doc_src_objectregistry.cpp 0
21 \codeline
22 \snippet doc_src_objectregistry.cpp 2
23
24 \sa ObjectRegistry, QAbstractObjectRegistryRef::key
25*/
26
27/*!
28 \qmltype ObjectRegistryRef
29 \nativetype QObjectRegistryRef
30 \inqmlmodule QtQml.DesignSupport
31 \ingroup qmldesignsupport_qml
32 \inherits AbstractObjectRegistryRef
33 \brief This type is used to access an object registered with ObjectRegistry type.
34
35 Example usage:
36 \snippet doc_src_objectregistry.cpp 0
37 \codeline
38 \snippet doc_src_objectregistry.cpp 1
39
40 \sa ObjectRegistry, AbstractObjectRegistryRef::key
41*/
42
43/*!
44 \property QObjectRegistryRef::object
45 \brief The object referenced by this instance.
46
47 If multiple objects are registered with the same key, the object referenced by this instance
48 is an unspecified one of among them. Use QMultiObjectRegistryRef instead in that case.
49
50 \sa ObjectRegistry, QAbstractObjectRegistryRef::key
51*/
52
53/*!
54 \qmlproperty QtObject ObjectRegistryRef::object
55
56 This property specifies the object referenced by this instance.
57
58 If multiple objects are registered with the same key, this instance references an unspecified
59 object from that group. In this case, use QMultiObjectRegistryRef.
60
61 \sa ObjectRegistry, AbstractObjectRegistryRef::key
62*/
63
64/*!
65 Constructs QObjectRegistryRef instance with \a parent.
66
67 \note Use this constructor only if the correct QML engine resolves automatically when the key
68 is set. This generally does not happen when creating an instance of this class from C++.
69 It is recommended to use one of the constructors that take a QML engine pointer as a parameter.
70
71 \sa qmlEngine(), QAbstractObjectRegistryRef::key
72*/
73QObjectRegistryRef::QObjectRegistryRef(QObject *parent)
74 : QAbstractObjectRegistryRef(*new QObjectRegistryRefPrivate(), parent)
75{}
76
77/*!
78 Constructs QObjectRegistryRef instance with QML \a engine and \a parent.
79 All registrations are scoped to the specified QML engine.
80
81 \sa QAbstractObjectRegistryRef::key
82*/
83QObjectRegistryRef::QObjectRegistryRef(QQmlEngine *engine, QObject *parent)
84 : QAbstractObjectRegistryRef(*new QObjectRegistryRefPrivate(engine), parent)
85{}
86
87/*!
88 Constructs QObjectRegistryRef instance with QML \a engine, \a key and \a parent.
89 All registrations are scoped to the specified QML engine.
90
91 \sa QAbstractObjectRegistryRef::key
92*/
93QObjectRegistryRef::QObjectRegistryRef(QQmlEngine *engine, const QString &key, QObject *parent)
94 : QAbstractObjectRegistryRef(*new QObjectRegistryRefPrivate(engine), parent)
95{
96 setKey(key);
97}
98
99QObjectRegistryRef::~QObjectRegistryRef() = default;
100
101QObject *QObjectRegistryRef::object() const
102{
103 Q_D(const QObjectRegistryRef);
104
105 return d->m_object;
106}
107
108/*!
109 \reimp
110*/
111bool QObjectRegistryRef::event(QEvent *event)
112{
113 return QAbstractObjectRegistryRef::event(event);
114}
115
116void QObjectRegistryRefPrivate::handleInitialObjects()
117{
118 if (!registry())
119 return;
120
121 QList<QObject *> objects = registry()->objects(key()).values();
122
123 if (objects.size() > 1)
124 printMultiWarning();
125
126 QObject *newObject = nullptr;
127
128 if (!objects.isEmpty())
129 newObject = *objects.cbegin();
130
131 setObject(newObject);
132}
133
134void QObjectRegistryRefPrivate::handleObjectAdded(QObject *obj)
135{
136 if (m_object && m_object != obj) {
137 printMultiWarning();
138 return;
139 }
140 setObject(obj);
141}
142
143void QObjectRegistryRefPrivate::handleObjectRemoved(QObject *obj)
144{
145 if (m_object != obj)
146 return;
147 setObject(nullptr);
148}
149
150QObjectRegistryRefPrivate::QObjectRegistryRefPrivate(QQmlEngine *engine)
151 : QAbstractObjectRegistryRefPrivate(engine)
152{}
153
154void QObjectRegistryRefPrivate::setObject(QObject *obj)
155{
156 Q_Q(QObjectRegistryRef);
157
158 if (obj == m_object)
159 return;
160
161 m_object = obj;
162 emit q->objectChanged(m_object);
163}
164
165void QObjectRegistryRefPrivate::printMultiWarning() const
166{
167 Q_Q(const QObjectRegistryRef);
168
169 qmlWarning(q) << "ObjectRegistryRef found multiple objects registered with the same key ("
170 << q->key()
171 << "). MultiObjectRegistryRef should be used instead.";
172}
173
174QT_END_NAMESPACE
Combined button and popup list for selecting options.