The QPointer class is a template class that provides guarded pointers to QObject . 更多...
| 頭: | #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 ) |
The QPointer class is a template class that provides guarded pointers to QObject .
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
必須是子類化的
QObject
.
守衛指針很有用,每當需要存儲的指針指嚮 QObject 由他人擁有,因此仍然可能在保持其引用時被銷毀。可以安全地測試,指針的有效性。
Qt 還提供 QSharedPointer ,計數引用共享指針對象的實現,可以用於維護單個指針的引用集閤。
範例:
QPointer<QLabel> label = new QLabel;
label->setText("&Status:");
...
if (label)
label->show();
若
QLabel
被同時刪除,
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 (
+
,
-
,
++
,和
--
), which are normally used only with arrays of objects.
使用 QPointer 像正常指針,且不需要閱讀此類文檔編製。
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
或
x->member
錶示法。
守衛指針將自動鑄造成
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.
注意,類
T
必須繼承
QObject
,否則會導緻編譯 (或鏈接) 錯誤。
另請參閱 QSharedPointer , QWeakPointer , QObject ,和 QObjectCleanupHandler .
Constructs a 0 guarded pointer.
另請參閱 isNull ().
Constructs a guarded pointer that points to same object that p 所指嚮。
Copies one guarded pointer from another. The constructed guarded pointer points to the same object that p points to (which may be 0).
銷毀守衛指針。就像正常指針,銷毀守衛指針 not 銷毀所指嚮的對象。
返迴指嚮被守衛對象的指針。
該函數在 Qt 4.4 引入。
返迴
true
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 a QPointer <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.
賦值運算符。此守衛指針現在將指嚮同一對象 p 所指嚮。
賦值運算符。此守衛指針現在將指嚮同一對象 p 所指嚮。
Inequality operator. Returns true if o 和守衛指針 p are not pointing to the same object, otherwise returns false.
Inequality operator. Returns true if o 和守衛指針 p are not pointing to the same object, otherwise returns false.
Inequality operator. Returns true if o 和守衛指針 p are not pointing to the same object, otherwise returns false.
Inequality operator. Returns true if o 和守衛指針 p are not pointing to the same object, otherwise returns false.
Inequality operator. Returns true if the guarded pointers p1 and p2 are not pointing to the same object, otherwise returns false.
Equality operator. Returns true if o 和守衛指針 p are pointing to the same object, otherwise returns false.
Equality operator. Returns true if o 和守衛指針 p are pointing to the same object, otherwise returns false.
Equality operator. Returns true if o 和守衛指針 p are pointing to the same object, otherwise returns false.
Equality operator. Returns true if o 和守衛指針 p are pointing to the same object, otherwise returns false.
Equality operator. Returns true if the guarded pointers p1 and p2 are pointing to the same object, otherwise returns false.