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
qobjectcleanuphandler.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 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// Qt-Security score:significant reason:default
4
6
8
9/*!
10 \class QObjectCleanupHandler
11 \inmodule QtCore
12 \brief The QObjectCleanupHandler class watches the lifetime of multiple QObjects.
13
14 \ingroup objectmodel
15
16 A QObjectCleanupHandler is useful whenever you need to know when a
17 number of \l{QObject}s that are owned by someone else have been
18 deleted. This is important, for example, when referencing memory
19 in an application that has been allocated in a shared library.
20
21 To keep track of some \l{QObject}s, create a
22 QObjectCleanupHandler, and add() the objects you are interested
23 in. If you are no longer interested in tracking a particular
24 object, use remove() to remove it from the cleanup handler. If an
25 object being tracked by the cleanup handler gets deleted by
26 someone else it will automatically be removed from the cleanup
27 handler. You can delete all the objects in the cleanup handler
28 with clear(), or by destroying the cleanup handler. isEmpty()
29 returns \c true if the QObjectCleanupHandler has no objects to keep
30 track of.
31
32 \sa QPointer
33*/
34
35/*!
36 Constructs an empty QObjectCleanupHandler.
37*/
38QObjectCleanupHandler::QObjectCleanupHandler()
39{
40}
41
42/*!
43 Destroys the cleanup handler. All objects in this cleanup handler
44 will be deleted.
45
46 \sa clear()
47*/
48QObjectCleanupHandler::~QObjectCleanupHandler()
49{
50 clear();
51}
52
53/*!
54 Adds \a object to this cleanup handler and returns the pointer to
55 the object.
56
57 \sa remove()
58*/
59QObject *QObjectCleanupHandler::add(QObject *object)
60{
61 if (!object)
62 return nullptr;
63
64 connect(object, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(QObject*)));
65 cleanupObjects.insert(0, object);
66 return object;
67}
68
69/*!
70 Removes the \a object from this cleanup handler. The object will
71 not be destroyed.
72
73 \sa add()
74*/
75void QObjectCleanupHandler::remove(QObject *object)
76{
77 qsizetype index;
78 if ((index = cleanupObjects.indexOf(object)) != -1) {
79 cleanupObjects.removeAt(index);
80 disconnect(object, SIGNAL(destroyed(QObject*)), this, SLOT(objectDestroyed(QObject*)));
81 }
82}
83
84/*!
85 Returns \c true if this cleanup handler is empty or if all objects in
86 this cleanup handler have been destroyed; otherwise return false.
87
88 \sa add(), remove(), clear()
89*/
90bool QObjectCleanupHandler::isEmpty() const
91{
92 return cleanupObjects.isEmpty();
93}
94
95/*!
96 Deletes all objects in this cleanup handler. The cleanup handler
97 becomes empty.
98
99 \sa isEmpty()
100*/
101void QObjectCleanupHandler::clear()
102{
103 while (!cleanupObjects.isEmpty())
104 delete cleanupObjects.takeFirst();
105}
106
107void QObjectCleanupHandler::objectDestroyed(QObject *object)
108{
109 remove(object);
110}
111
112QT_END_NAMESPACE
113
114#include "moc_qobjectcleanuphandler.cpp"
Combined button and popup list for selecting options.