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