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 時。
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 .
構造守衛指針采用值
nullptr
.
另請參閱 isNull ().
構造守衛指針指嚮同一對象 p 所指嚮。
銷毀守衛指針。就像正常指針,銷毀守衛指針 not 銷毀所指嚮的對象。
清零此 QPointer 對象。
另請參閱 isNull ().
返迴指嚮被守衛對象的指針。
[since 6.0]
T
*QPointer::
get
() const
如同 data ()。此函數為兼容 STL (標準模闆庫) 提供。
該函數在 Qt 6.0 引入。
返迴
true
if the referenced object has been destroyed or if there is no referenced object; otherwise returns
false
.
交換內容對於此 QPointer 采用內容源於 other 。此操作很快且從不失敗。
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 所指嚮。
不相等操作符。返迴
true
若守衛指針
p1
and
p2
未指嚮同一對象,否則返迴
false
.
不相等操作符。返迴
true
if
o
和守衛指針
p
未指嚮同一對象,否則返迴
false
.
不相等操作符。返迴
true
if
o
和守衛指針
p
未指嚮同一對象,否則返迴
false
.
不相等操作符。返迴
true
if the pointer guarded by
lhs
is a valid (ie not
nullptr
) pointer, otherwise returns
false
.
不相等操作符。返迴
true
if the pointer guarded by
rhs
is a valid (ie not
nullptr
) pointer, otherwise returns
false
.
相等運算符。返迴
true
若守衛指針
p1
and
p2
指嚮同一對象,否則返迴
false
.
相等運算符。返迴
true
if
o
和守衛指針
p
指嚮同一對象,否則返迴
false
.
相等運算符。返迴
true
if
o
和守衛指針
p
指嚮同一對象,否則返迴
false
.
相等運算符。返迴
true
if the pointer guarded by
lhs
is
nullptr
,否則返迴
false
.
相等運算符。返迴
true
if the pointer guarded by
rhs
is
nullptr
,否則返迴
false
.