QWeakPointer Class

The QWeakPointer class holds a weak reference to a shared pointer. More...

Header: #include <QWeakPointer>
qmake: QT += core
Since: Qt 4.5

Note: All functions in this class are reentrant.

Public Functions

QWeakPointer()
QWeakPointer(const QWeakPointer<T> &other)
QWeakPointer(const QSharedPointer<T> &other)
QWeakPointer(const QObject *other)
~QWeakPointer()
void clear()
T *data() const
bool isNull() const
QSharedPointer<T> lock() const
void swap(QWeakPointer<T> &other)
QSharedPointer<T> toStrongRef() const
operator bool() const
bool operator!() const
QWeakPointer<T> &operator=(const QWeakPointer<T> &other)
QWeakPointer<T> &operator=(const QSharedPointer<T> &other)
QWeakPointer<T> &operator=(const QObject *other)

Detailed Description

The QWeakPointer class holds a weak reference to a shared pointer.

The QWeakPointer is an automatic weak reference to a pointer in C++. It cannot be used to dereference the pointer directly, but it can be used to verify if the pointer has been deleted or not in another context.

QWeakPointer objects can only be created by assignment from a QSharedPointer.

It's important to note that QWeakPointer provides no automatic casting operators to prevent mistakes from happening. Even though QWeakPointer tracks a pointer, it should not be considered a pointer itself, since it doesn't guarantee that the pointed object remains valid.

Therefore, to access the pointer that QWeakPointer is tracking, you must first promote it to QSharedPointer and verify if the resulting object is null or not. QSharedPointer guarantees that the object isn't deleted, so if you obtain a non-null object, you may use the pointer. See QWeakPointer::toStrongRef() for an example.

See also QSharedPointer and QScopedPointer.

Member Function Documentation

QWeakPointer::QWeakPointer()

Default constructs an instance of QWeakPointer.

QWeakPointer::QWeakPointer(const QWeakPointer<T> &other)

Copy constructor.

QWeakPointer::QWeakPointer(const QSharedPointer<T> &other)

Default constructs an instance of QWeakPointer.

QWeakPointer::QWeakPointer(const QObject *other)

Default constructs an instance of QWeakPointer.

QWeakPointer::~QWeakPointer()

Destroys the instance of QWeakPointer.

void QWeakPointer::clear()

T *QWeakPointer::data() const

bool QWeakPointer::isNull() const

QSharedPointer<T> QWeakPointer::lock() const

void QWeakPointer::swap(QWeakPointer<T> &other)

QSharedPointer<T> QWeakPointer::toStrongRef() const

QWeakPointer::operator bool() const

bool QWeakPointer::operator!() const

QWeakPointer<T> &QWeakPointer::operator=(const QWeakPointer<T> &other)

Copy-assignment operator.

QWeakPointer<T> &QWeakPointer::operator=(const QSharedPointer<T> &other)

QWeakPointer<T> &QWeakPointer::operator=(const QObject *other)