6.0.0-rc4Source

QObjectCleanupHandler Class

The QObjectCleanupHandler class watches the lifetime of multiple QObjects. More…

Header#include <QObjectCleanupHandler>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
InheritsQObject

List of all members, including inherited members

Public Functions

Detailed Description

A cleanup handler deletes the objects that were added to it when it is destroyed or cleared, and forgets objects that are deleted by someone else first, so it never deletes an object twice. It is useful to tie the lifetime of several unrelated objects to one owner:

QObjectCleanupHandler cleaners;
QTimer *timer = cleaners.add(new QTimer);
QFile *file = cleaners.add(new QFile("log.txt"));
// both are deleted when `cleaners` goes out of scope

Objects are deleted in the reverse order in which they were added.

See also QPointer and QObject::destroyed().

Differences from Qt

None.

Member Function Documentation

QObjectCleanupHandler::QObjectCleanupHandler()

Constructs an empty cleanup handler.

[override] QObjectCleanupHandler::~QObjectCleanupHandler()

Destroys the cleanup handler, deleting all objects that were added to it.

See also clear().

QObject * QObjectCleanupHandler::add(QObject *object)

Adds object to the handler and returns it. The handler deletes the object when it is cleared or destroyed, unless the object is deleted before that. Adding a null pointer does nothing and returns nullptr.

See also remove().

void QObjectCleanupHandler::clear()

Deletes all objects in the handler, in the reverse order in which they were added, and leaves it empty.

See also isEmpty().

bool QObjectCleanupHandler::isEmpty() const

Returns true if the handler holds no objects.

See also clear().

void QObjectCleanupHandler::remove(QObject *object)

Removes object from the handler, which then no longer deletes it.

See also add().