QPointer 類

模闆 <typename T> 類 QPointer

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

頭: #include <QPointer>
CMake: find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
qmake: QT += core

公共函數

QPointer ()
QPointer (T * p )
~QPointer ()
void clear ()
T * data () const
T * get () 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 QPointer<T> & p1 , const QPointer<X> & p2 )
bool operator!= (const QPointer<T> & p , X * o )
bool operator!= (X * o , const QPointer<T> & p )
bool operator!= (const QPointer<T> & lhs , std::nullptr_t)
bool operator!= (std::nullptr_t, const QPointer<T> & rhs )
bool operator== (const QPointer<T> & p1 , const QPointer<X> & p2 )
bool operator== (const QPointer<T> & p , X * o )
bool operator== (X * o , const QPointer<T> & p )
bool operator== (const QPointer<T> & lhs , std::nullptr_t)
bool operator== (std::nullptr_t, const QPointer<T> & rhs )

詳細描述

守衛指針 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 ()

構造守衛指針采用值 nullptr .

另請參閱 isNull ().

QPointer:: QPointer ( T * p )

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

QPointer:: ~QPointer ()

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

void QPointer:: clear ()

清零此 QPointer 對象。

另請參閱 isNull ().

T *QPointer:: data () const

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

[since 6.0] T *QPointer:: get () const

如同 data ()。此函數為兼容 STL (標準模闆庫) 提供。

該函數在 Qt 6.0 引入。

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 。此操作很快且從不失敗。

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 X> bool operator!= (const QPointer < T > & p1 , const QPointer < X > & p2 )

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

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

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

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

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

bool operator!= (const QPointer < T > & lhs , std::nullptr_t )

不相等操作符。返迴 true if the pointer guarded by lhs is a valid (ie not nullptr ) pointer, otherwise returns false .

bool operator!= ( std::nullptr_t , const QPointer < T > & rhs )

不相等操作符。返迴 true if the pointer guarded by rhs is a valid (ie not nullptr ) pointer, otherwise returns false .

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

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

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

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

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

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

bool operator== (const QPointer < T > & lhs , std::nullptr_t )

相等運算符。返迴 true if the pointer guarded by lhs is nullptr ,否則返迴 false .

bool operator== ( std::nullptr_t , const QPointer < T > & rhs )

相等運算符。返迴 true if the pointer guarded by rhs is nullptr ,否則返迴 false .