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
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
\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
*/
73
QObjectRegistryRef::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
*/
83
QObjectRegistryRef::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
*/
93
QObjectRegistryRef::QObjectRegistryRef(QQmlEngine *engine,
const
QString &key, QObject *parent)
94
: QAbstractObjectRegistryRef(*
new
QObjectRegistryRefPrivate(engine), parent)
95
{
96
setKey(key);
97
}
98
99
QObjectRegistryRef::~QObjectRegistryRef() =
default
;
100
101
QObject *QObjectRegistryRef::object()
const
102
{
103
Q_D(
const
QObjectRegistryRef);
104
105
return
d->m_object;
106
}
107
108
void
QObjectRegistryRefPrivate::handleInitialObjects()
109
{
110
if
(!registry())
111
return
;
112
113
QList<QObject *> objects = registry()->objects(key()).values();
114
115
if
(objects.size() > 1)
116
printMultiWarning();
117
118
QObject *newObject =
nullptr
;
119
120
if
(!objects.isEmpty())
121
newObject = *objects.cbegin();
122
123
setObject(newObject);
124
}
125
126
void
QObjectRegistryRefPrivate::handleObjectAdded(QObject *obj)
127
{
128
if
(m_object && m_object != obj) {
129
printMultiWarning();
130
return
;
131
}
132
setObject(obj);
133
}
134
135
void
QObjectRegistryRefPrivate::handleObjectRemoved(QObject *obj)
136
{
137
if
(m_object != obj)
138
return
;
139
setObject(
nullptr
);
140
}
141
142
QObjectRegistryRefPrivate::QObjectRegistryRefPrivate(QQmlEngine *engine)
143
: QAbstractObjectRegistryRefPrivate(engine)
144
{}
145
146
void
QObjectRegistryRefPrivate::setObject(QObject *obj)
147
{
148
Q_Q(QObjectRegistryRef);
149
150
if
(obj == m_object)
151
return
;
152
153
m_object = obj;
154
emit q->objectChanged();
155
}
156
157
void
QObjectRegistryRefPrivate::printMultiWarning()
const
158
{
159
Q_Q(
const
QObjectRegistryRef);
160
161
qmlWarning(q) <<
"ObjectRegistryRef found multiple objects registered with the same key ("
162
<< q->key()
163
<<
"). MultiObjectRegistryRef should be used instead."
;
164
}
165
166
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.16.1