
We bake cookies in your browser for a better experience. Using this site means that you consent.Read More
TheQPointer class is a template class that provides guarded pointers toQObject.More...
| Header: | #include <QPointer> |
| QPointer() | |
| QPointer(T * p) | |
| QPointer(const QPointer<T> & p) | |
| ~QPointer() | |
| T * | data() const |
| bool | isNull() const |
| operator T *() const | |
| T & | operator*() const |
| T * | operator->() const |
| QPointer<T> & | operator=(const QPointer<T> & p) |
| QPointer<T> & | operator=(T * p) |
| bool | operator!=(const T * o, const QPointer<T> & p) |
| bool | operator!=(const QPointer<T> & p, const T * o) |
| bool | operator!=(T * o, const QPointer<T> & p) |
| bool | operator!=(const QPointer<T> & p, T * o) |
| bool | operator!=(const QPointer<T> & p1, const QPointer<T> & p2) |
| bool | operator==(const T * o, const QPointer<T> & p) |
| bool | operator==(const QPointer<T> & p, const T * o) |
| bool | operator==(T * o, const QPointer<T> & p) |
| bool | operator==(const QPointer<T> & p, T * o) |
| bool | operator==(const QPointer<T> & p1, const QPointer<T> & p2) |
TheQPointer class is a template class that provides guarded pointers toQObject.
A guarded pointer,QPointer<T>, behaves like a normal C++ pointerT *, 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 ofQObject.
Guarded pointers are useful whenever you need to store a pointer to aQObject 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.
Qt also providesQSharedPointer, an implementation of a reference-counted shared pointer object, which can be used to maintain a collection of references to an individual pointer.
Example:
If theQLabel is deleted in the meantime, thelabel variable will hold 0 instead of an invalid address, and the last line will never be executed.
The functions and operators available with aQPointer 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 withisNull(). You can dereference them using either the*x or thex->member notation.
A guarded pointer will automatically cast to aT *, so you can freely mix guarded and unguarded pointers. This means that if you have aQPointer<QWidget>, you can pass it to a function that requires aQWidget *. For this reason, it is of little value to declare functions to take aQPointer as a parameter; just use normal pointers. Use aQPointer when you are storing a pointer over time.
Note that classT must inheritQObject, or a compilation or link error will result.
See alsoQSharedPointer,QWeakPointer,QObject, andQObjectCleanupHandler.
Constructs a 0 guarded pointer.
See alsoisNull().
Constructs a guarded pointer that points to same object thatp points to.
Copies one guarded pointer from another. The constructed guarded pointer points to the same object thatp points to (which may be 0).
Destroys the guarded pointer. Just like a normal pointer, destroying a guarded pointer doesnot destroy the object being pointed to.
Returns the pointer to the object being guarded.
This function was introduced in Qt 4.4.
Returnstrue if the referenced object has been destroyed or if there is no referenced object; otherwise returns false.
Cast operator; implements pointer semantics. Because of this function you can pass aQPointer<T> to a function where a T* is required.
Dereference operator; implements pointer semantics. Just use this operator as you would with a normal C++ pointer.
Overloaded arrow operator; implements pointer semantics. Just use this operator as you would with a normal C++ pointer.
Assignment operator. This guarded pointer will now point to the same object thatp points to.
Assignment operator. This guarded pointer will now point to the same object thatp points to.
Inequality operator. Returns true ifo and the guarded pointerp are not pointing to the same object, otherwise returns false.
Inequality operator. Returns true ifo and the guarded pointerp are not pointing to the same object, otherwise returns false.
Inequality operator. Returns true ifo and the guarded pointerp are not pointing to the same object, otherwise returns false.
Inequality operator. Returns true ifo and the guarded pointerp are not pointing to the same object, otherwise returns false.
Inequality operator. Returns true if the guarded pointersp1 andp2 are not pointing to the same object, otherwise returns false.
Equality operator. Returns true ifo and the guarded pointerp are pointing to the same object, otherwise returns false.
Equality operator. Returns true ifo and the guarded pointerp are pointing to the same object, otherwise returns false.
Equality operator. Returns true ifo and the guarded pointerp are pointing to the same object, otherwise returns false.
Equality operator. Returns true ifo and the guarded pointerp are pointing to the same object, otherwise returns false.
Equality operator. Returns true if the guarded pointersp1 andp2 are pointing to the same object, otherwise returns false.
© 2016 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of theGNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.