6.0.0-rc4Source

QWeakPointer Class

template <typename T> class QWeakPointer

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

Header#include <QWeakPointer>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes
Inheritsstd::weak_ptr

List of all members, including inherited members

Public Functions

QWeakPointer()
QWeakPointer(const QSharedPointer<T> &s)
QWeakPointer(const Base &b)
QWeakPointer(const QWeakPointer<X> &o)
operator bool() const
booloperator!() const
boolisNull() const
voidclear()
QSharedPointer<T>toStrongRef() const
QSharedPointer<T>lock() const
voidswap(QWeakPointer &other)

Detailed Description

A QWeakPointer<T> observes an object owned by QSharedPointer objects without keeping it alive. Call toStrongRef() to obtain a QSharedPointer, which is null if the object has been destroyed. QWeakPointer inherits from std::weak_ptr<T>.

See also QSharedPointer.

Differences from Qt

QWeakPointer is a thin subclass of std::weak_ptr<T>. It cannot observe a QObject that is not owned by a QSharedPointer, and has no data(). Comparisons lock the pointer, so a pointer whose object was destroyed compares equal to nullptr.

Member Function Documentation

QWeakPointer::QWeakPointer()

Constructs a null weak pointer.

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

Constructs a weak pointer that observes the object held by s.

QWeakPointer::QWeakPointer(const Base &b)

This is an overloaded function.

Constructs a weak pointer from the std::weak_ptr b.

template <typename X, typename = std::enable_if_t<std::is_convertible_v<X *, T *>>>QWeakPointer::QWeakPointer(const QWeakPointer<X> &o)

This is an overloaded function.

Constructs a weak pointer from o, whose pointer type X * must be implicitly convertible to T *.

void QWeakPointer::clear()

Makes the weak pointer null.

bool QWeakPointer::isNull() const

Returns true if the observed object has been destroyed or the pointer is null; otherwise returns false.

QSharedPointer<T> QWeakPointer::lock() const

Same as toStrongRef().

[explicit] QWeakPointer::operator bool() const

Returns true if the observed object still exists; otherwise returns false.

See also isNull().

bool QWeakPointer::operator!() const

Returns true if the observed object has been destroyed or the pointer is null; otherwise returns false.

See also isNull().

void QWeakPointer::swap(QWeakPointer &other)

Swaps this weak pointer with other.

QSharedPointer<T> QWeakPointer::toStrongRef() const

Returns a QSharedPointer to the observed object, or a null pointer if it has been destroyed.

See also QSharedPointer::toWeakRef().