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_p.h
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//
5// W A R N I N G
6// -------------
7//
8// This file is not part of the Qt API. It exists purely as an
9// implementation detail. This header file may change from version to
10// version without notice, or even be removed.
11//
12// We mean it.
13//
14
15#ifndef QOBJECTREGISTRYSINGLETON_P_H
16#define QOBJECTREGISTRYSINGLETON_P_H
17
18#include <QtQmlDesignSupport/qtqmldesignsupportexports.h>
19
20#include <QtCore/qhash.h>
21#include <QtCore/qobject.h>
22#include <QtCore/qset.h>
23#include <QtCore/qstring.h>
24#include <QtQml/qqml.h>
25#include <QtQml/private/qqmlguard_p.h>
26
27#include <memory>
28
29QT_BEGIN_NAMESPACE
30
31class QAbstractObjectRegistryRefPrivate;
32class QQmlEngine;
33
34class Q_QMLDESIGNSUPPORT_EXPORT QObjectRegistrySingleton : public QObject
35{
36 Q_OBJECT
37 QML_SINGLETON
38 QML_NAMED_ELEMENT(InternalObjectRegistry)
39
40public:
41 explicit QObjectRegistrySingleton(QObject *parent = nullptr);
42 ~QObjectRegistrySingleton();
43
44 void registerRef(QAbstractObjectRegistryRefPrivate *ref);
45 void deregisterRef(QAbstractObjectRegistryRefPrivate *ref);
46
47 void add(const QString &key, QObject *obj);
48 void remove(const QString &key, QObject *obj);
49 QSet<QObject*> objects(const QString &key) const;
50
51 static QObjectRegistrySingleton *registryForObject(QObject *obj);
52 static QObjectRegistrySingleton *registryForEngine(QQmlEngine *engine);
53
54private:
55 enum class Notification { ObjectAdded, ObjectRemoved };
56
57 void notifyRefs(const QString &key, QObject *obj, Notification notification);
58
59 class ObjectGuard : public QQmlGuard<QObject>
60 {
61 public:
62 ObjectGuard(QObjectRegistrySingleton *owner, const QString &key, QObject *obj)
63 : QQmlGuard<QObject>(&ObjectGuard::objectDestroyedImpl, obj)
64 , m_owner(owner)
65 , m_key(key)
66 , m_object(obj)
67 {
68 }
69
70 private:
71 static void objectDestroyedImpl(QQmlGuardImpl *guard)
72 {
73 ObjectGuard *self = static_cast<ObjectGuard *>(guard);
74 QObjectRegistrySingleton *owner = self->m_owner;
75 const QString key = self->m_key;
76 QObject *obj = self->m_object;
77
78 owner->remove(key, obj);
79 }
80
81 QObjectRegistrySingleton *m_owner = nullptr;
82 QString m_key;
83 QObject *m_object = nullptr;
84 };
85
86 QHash<QString, QHash<QObject *, ObjectGuard *>> m_objects;
87 QHash<QString, QSet<QAbstractObjectRegistryRefPrivate *>> m_refs;
88};
89
90QT_END_NAMESPACE
91
92#endif // QOBJECTREGISTRYSINGLETON_P_H
Combined button and popup list for selecting options.