QPointer 類

模闆 <typename T> 類 QPointer

QPointer 類是提供守衛指針的模闆類,指嚮 QObject . 更多...

頭: #include <QPointer>
qmake: QT += core

公共函數

QPointer (T * p )
QPointer ()
~QPointer ()
void clear ()
T * data () const
bool isNull () const
void swap (QPointer<T> & other )
T * operator T * () const
T & operator* () const
T * operator-> () const
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 )

詳細描述

守衛指針 QPointer<T>,行為像正常 C++ 指針 T * ,除瞭它會被自動清零,當銷毀引用對象時 (不像正常 C++ 指針,在這種情況下,會變成 "懸空指針")。 T 必須是子類化的 QObject .

守衛指針很有用,每當需要存儲的指針指嚮 QObject 由他人擁有,因此仍然可能在保持其引用時被銷毀。可以安全地測試,指針的有效性。

注意,Qt 5 引入瞭輕微的行為變化,當使用 QPointer 時。

  • 當使用 QPointer 在 QWidget (或子類化的 QWidget ),先前,QPointer 的清零是通過 QWidget 析構函數。現在,QPointer 的清零是通過 QObject 析構函數 (由於這是在 QWeakPointer 對象被清零時)。追蹤 Widget 的任何 QPointer 都會 NOT 被清零先於 QWidget 析構函數銷毀用於追蹤 Widget 的子級。

Qt 還提供 QSharedPointer ,計數引用共享指針對象的實現,可以用於維護單個指針的引用集閤。

範例:

    QPointer<QLabel> label = new QLabel;
    label->setText("&Status:");
    ...
    if (label)
        label->show();
					

QLabel 被同時刪除, label 變量將保持 nullptr 而不是無效地址,且從不會執行最後一行。

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 nullptr 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 , QObject ,和 QObjectCleanupHandler .

成員函數文檔編製

QPointer:: QPointer ( T * p )

構造守衛指針指嚮同一對象 p 所指嚮。

QPointer:: QPointer ()

構造守衛指針采用值 nullptr .

另請參閱 isNull ().

QPointer:: ~QPointer ()

銷毀守衛指針。就像正常指針,銷毀守衛指針 not 銷毀所指嚮的對象。

void QPointer:: clear ()

清零此 QPointer 對象。

該函數在 Qt 5.0 引入。

另請參閱 isNull ().

T *QPointer:: data () const

返迴指嚮被守衛對象的指針。

該函數在 Qt 4.4 引入。

bool QPointer:: isNull () const

返迴 true if the referenced object has been destroyed or if there is no referenced object; otherwise returns false .

void QPointer:: swap ( QPointer < T > & other )

交換內容對於此 QPointer 采用內容源於 other 。此操作很快且從不失敗。

該函數在 Qt 5.6 引入。

T *QPointer:: operator T * () const

Cast operator; implements pointer semantics. Because of this function you can pass a QPointer <T> to a function where a T* is required.

T &QPointer:: operator* () const

Dereference operator; implements pointer semantics. Just use this operator as you would with a normal C++ pointer.

T *QPointer:: operator-> () const

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

QPointer < T > &QPointer:: operator= ( T * p )

賦值運算符。此守衛指針現在將指嚮同一對象 p 所指嚮。

相關非成員

template <typename T> bool operator!= (const T * o , const QPointer < T > & p )

不相等操作符。返迴 true if o 和守衛指針 p 未指嚮同一對象,否則返迴 false .

template <typename T> bool operator!= (const QPointer < T > & p , const T * o )

不相等操作符。返迴 true if o 和守衛指針 p 未指嚮同一對象,否則返迴 false .

template <typename T> bool operator!= ( T * o , const QPointer < T > & p )

不相等操作符。返迴 true if o 和守衛指針 p 未指嚮同一對象,否則返迴 false .

template <typename T> bool operator!= (const QPointer < T > & p , T * o )

不相等操作符。返迴 true if o 和守衛指針 p 未指嚮同一對象,否則返迴 false .

template <typename T> bool operator!= (const QPointer < T > & p1 , const QPointer < T > & p2 )

不相等操作符。返迴 true 若守衛指針 p1 and p2 未指嚮同一對象,否則返迴 false .

template <typename T> bool operator== (const T * o , const QPointer < T > & p )

相等運算符。返迴 true if o 和守衛指針 p 指嚮同一對象,否則返迴 false .

template <typename T> bool operator== (const QPointer < T > & p , const T * o )

相等運算符。返迴 true if o 和守衛指針 p 指嚮同一對象,否則返迴 false .

template <typename T> bool operator== ( T * o , const QPointer < T > & p )

相等運算符。返迴 true if o 和守衛指針 p 指嚮同一對象,否則返迴 false .

template <typename T> bool operator== (const QPointer < T > & p , T * o )

相等運算符。返迴 true if o 和守衛指針 p 指嚮同一對象,否則返迴 false .

template <typename T> bool operator== (const QPointer < T > & p1 , const QPointer < T > & p2 )

相等運算符。返迴 true 若守衛指針 p1 and p2 指嚮同一對象,否則返迴 false .