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
QObject *QObjectRegistryRef::object()
const
100
{
101
Q_D(
const
QObjectRegistryRef);
102
103
return
d->m_object;
104
}
105
106
void
QObjectRegistryRefPrivate::handleInitialObjects()
107
{
108
if
(!registry())
109
return
;
110
111
QList<QObject *> objects = registry()->objects(key()).values();
112
113
if
(objects.size() > 1)
114
printMultiWarning();
115
116
QObject *newObject =
nullptr
;
117
118
if
(!objects.isEmpty())
119
newObject = *objects.cbegin();
120
121
setObject(newObject);
122
}
123
124
void
QObjectRegistryRefPrivate::handleObjectAdded(QObject *obj)
125
{
126
if
(m_object && m_object != obj) {
127
printMultiWarning();
128
return
;
129
}
130
setObject(obj);
131
}
132
133
void
QObjectRegistryRefPrivate::handleObjectRemoved(QObject *obj)
134
{
135
if
(m_object != obj)
136
return
;
137
setObject(
nullptr
);
138
}
139
140
QObjectRegistryRefPrivate::QObjectRegistryRefPrivate(QQmlEngine *engine)
141
: QAbstractObjectRegistryRefPrivate(engine)
142
{}
143
144
void
QObjectRegistryRefPrivate::setObject(QObject *obj)
145
{
146
Q_Q(QObjectRegistryRef);
147
148
if
(obj == m_object)
149
return
;
150
151
m_object = obj;
152
emit q->objectChanged();
153
}
154
155
void
QObjectRegistryRefPrivate::printMultiWarning()
const
156
{
157
Q_Q(
const
QObjectRegistryRef);
158
159
qmlWarning(q) <<
"ObjectRegistryRef found multiple objects registered with the same key ("
160
<< q->key()
161
<<
"). MultiObjectRegistryRef should be used instead."
;
162
}
163
164
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qrandomaccessasyncfile_darwin.mm:17
qtdeclarative
src
qmldesignsupport
qobjectregistryref.cpp
Generated on
for Qt by
1.16.1