QPointer Class
template <typename T> class QPointer
The QPointer class is a template class that provides guarded pointers to QObject. More…
| Header | #include <QPointer> |
|---|---|
| CMake | target_link_libraries(app PRIVATE Nt::Core) |
| Module | Nt Core C++ Classes |
List of all members, including inherited members
Public Functions
| QPointer() | |
| QPointer(T *p) | |
| QPointer(const QPointer &) | |
| QPointer & | operator=(const QPointer &) |
| QPointer & | operator=(T *p) |
| QPointer(const QPointer<X> &other) | |
| void | swap(QPointer &other) |
| T * | data() const |
| T * | get() const |
| T * | operator->() const |
| T & | operator*() const |
| operator T*() const | |
| bool | isNull() const |
| void | clear() |
Detailed Description
A QPointer<T> behaves like a normal T * but becomes nullptr automatically when the QObject it points to is destroyed. T must derive from QObject.
QPointer<QTimer> timer = new QTimer;
delete timer.data();
Q_ASSERT(timer.isNull());
See also QObject and QSharedPointer.
Differences from Qt
The guard is held through a shared control block, so a QPointer may outlive the object but is not thread-safe against concurrent destruction. Comparisons with other guarded pointers, raw pointers and nullptr go through the conversion to T *, so they compare the current data().
Member Function Documentation
QPointer::QPointer()
Constructs a null guarded pointer.
QPointer::QPointer(T *p)
Constructs a guarded pointer that tracks p.
QPointer::QPointer(const QPointer &)
Constructs a copy of another guarded pointer, tracking the same object.
template <typename X, typename = std::enable_if_t<std::is_convertible_v<X *, T *> && !std::is_same_v<X, T>>>QPointer::QPointer(const QPointer<X> &other)
Constructs a guarded pointer that tracks the object of other, whose type X * must convert implicitly to T *.
This function was introduced in 6.6.
void QPointer::clear()
Makes the pointer null.
T * QPointer::data() const
Returns the guarded object, or nullptr if it has been destroyed or the pointer is null.
T * QPointer::get() const
Returns the guarded object. This is the same as data().
bool QPointer::isNull() const
Returns true if the guarded object is null or has been destroyed; otherwise returns false.
QPointer::operator T*() const
Returns the guarded object. This allows a QPointer to be used where a T * is expected.
T & QPointer::operator*() const
Returns a reference to the guarded object. The pointer must not be null.
T * QPointer::operator->() const
Returns the guarded object, for member access.
QPointer & QPointer::operator=(const QPointer &)
Assigns another guarded pointer to this one and returns a reference to this pointer.
QPointer & QPointer::operator=(T *p)
This is an overloaded function.
Makes this pointer track p and returns a reference to this pointer.
void QPointer::swap(QPointer &other)
Swaps this guarded pointer with other.