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
qobjectregistrysingleton.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/qabstractobjectregistryref_p.h>
7
8#include <QtQml/qqmlengine.h>
9
11
12QObjectRegistrySingleton::QObjectRegistrySingleton(QObject *parent)
13 : QObject(parent)
14{
15}
16
17void QObjectRegistrySingleton::add(const QString &key, QObject *obj)
18{
19 if (key.isEmpty() || !obj)
20 return;
21
22 auto &objSet = m_objects[key];
23 if (!objSet.contains(obj)) {
24 objSet.insert(obj);
25 const auto refs = m_refs.value(key);
26 for (const auto &ref : refs)
27 ref->handleObjectAdded(obj);
28 }
29}
30
31void QObjectRegistrySingleton::remove(const QString &key, QObject *obj)
32{
33 if (key.isEmpty() || !obj)
34 return;
35
36 auto &objSet = m_objects[key];
37 bool notifyListeners = objSet.remove(obj);
38 if (objSet.isEmpty())
39 m_objects.remove(key);
40 if (notifyListeners) {
41 const auto refs = m_refs.value(key);
42 for (const auto &ref : refs)
43 ref->handleObjectRemoved(obj);
44 }
45}
46
47QSet<QObject*> QObjectRegistrySingleton::objects(const QString &key) const
48{
49 return m_objects.value(key);
50}
51
52void QObjectRegistrySingleton::registerRef(QAbstractObjectRegistryRefPrivate *ref)
53{
54 if (!ref)
55 return;
56
57 m_refs[ref->key()].insert(ref);
58}
59
60void QObjectRegistrySingleton::deregisterRef(QAbstractObjectRegistryRefPrivate *ref)
61{
62 if (!ref)
63 return;
64
65 if (m_refs.contains(ref->key())) {
66 m_refs[ref->key()].remove(ref);
67 if (m_refs[ref->key()].isEmpty())
68 m_refs.remove(ref->key());
69 }
70}
71
72QObjectRegistrySingleton *QObjectRegistrySingleton::registryForObject(QObject *obj)
73{
74 return registryForEngine(qmlEngine(obj));
75}
76
77QObjectRegistrySingleton *QObjectRegistrySingleton::registryForEngine(QQmlEngine *engine)
78{
79 if (engine) {
80 static int typeId = qmlTypeId("QtQml.DesignSupport",
81 QT_VERSION_MAJOR,
82 QT_VERSION_MINOR,
83 "InternalObjectRegistry");
84
85 return engine->singletonInstance<QObjectRegistrySingleton *>(typeId);
86 }
87 return nullptr;
88}
89
90QT_END_NAMESPACE
Combined button and popup list for selecting options.