CopperSpice API  1.7.2
QPointer< T > Class Template Reference

Templated class which provides smart pointers for QObject. More...

Public Methods

 QPointer ()
 
 QPointer (T *ptr)
 
 ~QPointer () = default
 
void clear ()
 
T * data () const
 
bool isNull () const
 
 operator T * () const
 
T & operator* () const
 
T * operator-> () const
 
QPointer< T > & operator= (T *ptr)
 

Related Functions

These are not member functions

bool operator!= (const QPointer< T > &ptr1, const QPointer< T > &ptr2)
 
bool operator!= (const QPointer< T > &ptr1, const T *ptr2)
 
bool operator!= (const QPointer< T > &ptr1, T *ptr2)
 
bool operator!= (const T *ptr1, const QPointer< T > &ptr2)
 
bool operator!= (T *ptr1, const QPointer< T > &ptr2)
 
bool operator== (const QPointer< T > &ptr1, const QPointer< T > &ptr2)
 
bool operator== (const QPointer< T > &ptr1, const T *ptr2)
 
bool operator== (const QPointer< T > &ptr1, T *ptr2)
 
bool operator== (const T *ptr1, const QPointer< T > &a ptr2)
 
bool operator== (T *ptr1, const QPointer< T > &ptr2)
 

Detailed Description

template<class T>
class QPointer< T >

The QPointer class is a templated class which provides a smart pointer for QObject. QPointer<T> behaves like a normal C++ pointer except it is automatically set to nullptr when the object it points to is destroyed. The T must inherit from QObject.

Smart pointers are useful when you need to store a pointer to a QObject which is owned by someone else and therefore might be destroyed while you still hold a reference to it. You can safely test the pointer for validity.

CopperSpice also provides QSharedPointer which is a reference-counted shared pointer.

label->setText("&Status:");
if (label) {
label->show();
}

If the QLabel is deleted, label will be null instead of an invalid address and the show() method will never be called.

The functions and operators available with a QPointer are the same as those available with a normal pointer. However, pointer arithmetic operators (+, -, ++, and ) are not allowed.

You can construct or assign a QPointer from a T* or from another QPointer of the same type. You can compare QPointers with each other using operator==() and operator!=(). You can also test for isNull() to ensure it points to a valid address. To dereference, use either the *x or the x->member notation.

A QPointer will automatically cast to a T * so you can freely mix QPointer with raw pointers. If you have a QPointer<QWidget> you can pass it to a function which requires a QWidget *.

Comparison

If a QPointer was pointing to a QWidget (or a subclass of QWidget), the QPointer was nulled by the QWidget destructor. QObject and QWidget were changed so the QPointer is now nulled by the QObject destructor.

In CopperSpice QPointer and QWeakPointer were changed and are now equally efficient. QPointer was changed to use methods in QWeakPointer which are for internal use only. The old guard mechanism in QObject has been removed.

  • QPointer has changed to use QWeakPointer which is more efficient and faster
  • QPointer is currently not thread safe


The following table contains a list of the smart pointers currently available in CopperSpice.

Category Classes Description
Shared Pointers QSharedPointer, QWeakPointer Thread safe pointers, they behave like std::shared_ptr and std::weak_ptr
Scoped Pointers QScopedPointer, QScopedArrayPointer Pointer takes ownership of an object and ensures the object is properly deleted at the end of the pointer's scope
Scoped pointers can not be copied
Scoped Pointer QPatternist::AutoPtr AutoPtr in an internal class based on std::auto_ptr
Non-Standard QSharedDataPointer, QExplicitlySharedDataPointer Pointer makes a copy of the data when changed on a write
Non-Standard QPointer Pointer becomes null when the QObject instance is destroyed


See also
QSharedPointer, QWeakPointer, QObject, QObjectCleanupHandler

Constructor & Destructor Documentation

template<class T >
QPointer< T >::QPointer ( )
inline

Constructs a null pointer.

See also
isNull()
template<class T >
QPointer< T >::QPointer ( T *  ptr)
inline

Constructs a pointer which points to the same object that ptr points to.

template<class T >
QPointer< T >::~QPointer ( )
default

Destroys the pointer.

Method Documentation

template<class T >
void QPointer< T >::clear ( )
inline

Clears this QPointer object.

See also
isNull()
template<class T >
T * QPointer< T >::data ( ) const
inline

Returns a pointer to the object.

template<class T >
bool QPointer< T >::isNull ( ) const
inline

Returns true if the referenced object has been destroyed or if there is no referenced object, otherwise returns false.

template<class T >
QPointer< T >::operator T * ( ) const
inline

Cast operator. Implements pointer semantics. This method allows you to pass a QPointer<T&gt, to a function where a T* is required.

template<class T >
T & QPointer< T >::operator* ( ) const
inline

Dereference operator. Implements pointer semantics. Use this operator as you would with a normal C++ pointer.

template<class T >
T * QPointer< T >::operator-> ( ) const
inline

Overloaded arrow operator, implements pointer semantics. Use use this operator as you would with a normal C++ pointer.

template<class T >
QPointer< T > & QPointer< T >::operator= ( T *  ptr)
inline

Assignment operator. This pointer will now point to the same object which ptr points to.

Friends And Related Function Documentation

bool operator!= ( const QPointer< T > &  ptr1,
const QPointer< T > &  ptr2 
)
related

Inequality operator. Returns true if the guarded pointers ptr1 and ptr2 are not pointing to the same object, otherwise returns false.

bool operator!= ( const QPointer< T > &  ptr1,
const T *  ptr2 
)
related

Inequality operator. Returns true if ptr2 and the guarded pointer ptr1 are not pointing to the same object, otherwise returns false.

bool operator!= ( const QPointer< T > &  ptr1,
T *  ptr2 
)
related

Inequality operator. Returns true if ptr2 and the guarded pointer ptr1 are not pointing to the same object, otherwise returns false.

bool operator!= ( const T *  ptr1,
const QPointer< T > &  ptr2 
)
related

Inequality operator. Returns true if ptr1 and the guarded pointer ptr2 are not pointing to the same object, otherwise returns false.

bool operator!= ( T *  ptr1,
const QPointer< T > &  ptr2 
)
related

Inequality operator. Returns true if ptr1 and the guarded pointer ptr2 are not pointing to the same object, otherwise returns false.

bool operator== ( const QPointer< T > &  ptr1,
const QPointer< T > &  ptr2 
)
related

Equality operator. Returns true if the guarded pointers ptr1 and ptr2 are pointing to the same object, otherwise returns false.

bool operator== ( const QPointer< T > &  ptr1,
const T *  ptr2 
)
related

Equality operator. Returns true if ptr2 and the guarded pointer ptr1 are pointing to the same object, otherwise returns false.

bool operator== ( const QPointer< T > &  ptr1,
T *  ptr2 
)
related

Equality operator. Returns true if ptr2 and the guarded pointer ptr1 are pointing to the same object, otherwise returns false.

bool operator== ( const T *  ptr1,
const QPointer< T > &a  ptr2 
)
related

Equality operator. Returns true if ptr1 and the guarded pointer ptr2 are pointing to the same object, otherwise returns false.

bool operator== ( T *  ptr1,
const QPointer< T > &  ptr2 
)
related

Equality operator. Returns true if ptr1 and the guarded pointer ptr2 are pointing to the same object, otherwise returns false.