#include <qpointer.h>
Inheritance diagram for QPointer< T >:


A guarded pointer, QPointer<T>, behaves like a normal C++ pointer {T *}, except that it is automatically set to 0 when the referenced object is destroyed (unlike normal C++ pointers, which become "dangling pointers" in such cases). T must be a subclass of QObject.
Guarded pointers are useful whenever you need to store a pointer to a QObject that 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.
Example:
snippets/pointer/pointer.cpp QPointer<QLabel> label setText( if (label) show()
If the QLabel is deleted in the meantime, the label variable will hold 0 instead of an invalid address, and the last line will never be executed.
The functions and operators available with a QPointer are the same as those available with a normal unguarded pointer, except the pointer arithmetic operators ({+}, {-}, {++}, and {--}), which are normally used only with arrays of objects.
Use QPointers like normal pointers and you will not need to read this class documentation.
For creating guarded pointers, you can construct or assign to them from a T* or from another guarded pointer of the same type. You can compare them with each other using operator==() and operator!=(), or test for 0 with isNull(). You can dereference them using either the *x or the x->member notation.
A guarded pointer will automatically cast to a T *, so you can freely mix guarded and unguarded pointers. This means that if you have a QPointer<QWidget>, you can pass it to a function that requires a QWidget *. For this reason, it is of little value to declare functions to take a QPointer as a parameter; just use normal pointers. Use a QPointer when you are storing a pointer over time.
Note that class T must inherit QObject, or a compilation or link error will result.
Definition at line 34 of file qpointer.h.
Public Member Functions | |
| QPointer () | |
| QPointer (T *p) | |
| QPointer (const QPointer< T > &p) | |
| ~QPointer () | |
| QPointer< T > & | operator= (const QPointer< T > &p) |
| QPointer< T > & | operator= (T *p) |
| bool | isNull () const |
| T * | operator-> () const |
| T & | operator * () const |
| operator T * () const | |
Private Attributes | |
| QObject * | o |
Constructs a 0 guarded pointer.
Definition at line 38 of file qpointer.h.
00038 : o(0) {}
Constructs a guarded pointer that points to same object that p points to.
Definition at line 39 of file qpointer.h.
00039 : o(p) 00040 { QMetaObject::addGuard(&o); }
Copies one guarded pointer from another. The constructed guarded pointer points to the same object that p points to (which may be 0).
Definition at line 41 of file qpointer.h.
00041 : o(p.o) 00042 { QMetaObject::addGuard(&o); }
Destroys the guarded pointer. Just like a normal pointer, destroying a guarded pointer does not destroy the object being pointed to.
Definition at line 43 of file qpointer.h.
00044 { QMetaObject::removeGuard(&o); }
Assignment operator. This guarded pointer will now point to the same object that p points to.
Definition at line 45 of file qpointer.h.
00046 { if (this != &p) QMetaObject::changeGuard(&o, p.o); return *this; }
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Assignment operator. This guarded pointer will now point to the same object that p points to.
Definition at line 47 of file qpointer.h.
00048 { if (o != p) QMetaObject::changeGuard(&o, p); return *this; }
| 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.
Definition at line 50 of file qpointer.h.
Referenced by QWidgetPrivate::close_helper(), Q3FileDialog::doMimeTypeLookup(), qdesigner_internal::ComboBoxTaskMenu::editItems(), qdesigner_internal::TreeWidgetTaskMenu::editItems(), qdesigner_internal::ListWidgetTaskMenu::editItems(), qdesigner_internal::TableWidgetTaskMenu::editItems(), qdesigner_internal::LabelTaskMenu::editText(), qdesigner_internal::ButtonTaskMenu::editText(), qdesigner_internal::LineEditTaskMenu::editText(), qdesigner_internal::TextEditTaskMenu::editText(), qdesigner_internal::ToolBarTaskMenu::editToolBar(), QMenu::exec(), qdesigner_internal::ConnectionEdit::findObjectsUnderMouse(), QTestEventLoop::instance(), QAccessibleObject::isValid(), QDBusConnectionPrivate::messageResultReceived(), QDBusInterfacePrivate::metacall(), qdesigner_internal::ConnectionEdit::mouseDoubleClickEvent(), qdesigner_internal::ConnectionEdit::mouseMoveEvent(), qdesigner_internal::ConnectionEdit::mousePressEvent(), qdesigner_internal::ConnectionEdit::mouseReleaseEvent(), TrPreviewTool::on_viewForms_doubleClicked(), qdesigner_internal::ConnectionEdit::paintEvent(), and TrWindow::previewForm().
00051 { return !o; }
| T * QPointer< T >::operator-> | ( | ) | const [inline] |
Overloaded arrow operator; implements pointer semantics. Just use this operator as you would with a normal C++ pointer.
Definition at line 53 of file qpointer.h.
00054 { return static_cast<T*>(const_cast<QObject*>(o)); }
| T & QPointer< T >::operator * | ( | ) | const [inline] |
Dereference operator; implements pointer semantics. Just use this operator as you would with a normal C++ pointer.
Definition at line 55 of file qpointer.h.
00056 { return *static_cast<T*>(const_cast<QObject*>(o)); }
| QPointer< T >::operator T * | ( | ) | const [inline] |
Cast operator; implements pointer semantics. Because of this function you can pass a QPointer<T> to a function where a T* is required.
Definition at line 57 of file qpointer.h.
00058 { return static_cast<T*>(const_cast<QObject*>(o)); }
Definition at line 36 of file qpointer.h.
1.5.1