QPointer 類是提供守衛指針的模闆類,指嚮 QObject . 更多...
| 頭: |
#include <QPointer>
|
| CMake: |
find_package(Qt6 REQUIRED COMPONENTS Core)
target_link_libraries(mytarget PRIVATE Qt6::Core)
|
| qmake: |
QT += core
|
此類 相等可比較 .
此類 相等可比較 with QPointer<X>, X*, and std::nullptr_t.
Where X and T are compatible types, which means that they are either the same (except for their cv-qualifiers), or one is a base type of the other.
| QPointer () | |
(從 6.6 起)
|
QPointer (QPointer<X> && other ) |
| QPointer (T * p ) | |
(從 6.6 起)
|
QPointer (const QPointer<X> & other ) |
| QPointer (std::nullptr_t) | |
| ~QPointer () | |
| void | clear () |
| T * | data () const |
(從 6.0 起)
T *
|
get () const |
| bool | isNull () const |
| void | swap (QPointer<T> & other ) |
| T * | operator T * () const |
| T & | operator* () const |
| T * | operator-> () const |
(since 6.6.1)
QPointer<T> &
|
operator= (QPointer<X> && other ) |
| QPointer<T> & | operator= (T * p ) |
(從 6.6 起)
QPointer<T> &
|
operator= (const QPointer<X> & other ) |
| bool | operator!= (X *const & lhs , const QPointer<T> & rhs ) |
| bool | operator!= (const QPointer<T> & lhs , X *const & rhs ) |
| bool | operator!= (const QPointer<T> & lhs , const QPointer<X> & rhs ) |
| bool | operator!= (const QPointer<T> & lhs , const std::nullptr_t & rhs ) |
| bool | operator!= (const std::nullptr_t & lhs , const QPointer<T> & rhs ) |
| bool | operator== (X *const & lhs , const QPointer<T> & rhs ) |
| bool | operator== (const QPointer<T> & lhs , X *const & rhs ) |
| bool | operator== (const QPointer<T> & lhs , const QPointer<X> & rhs ) |
| bool | operator== (const QPointer<T> & lhs , const std::nullptr_t & rhs ) |
| bool | operator== (const std::nullptr_t & lhs , 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 .
[noexcept]
QPointer::
QPointer
()
[constexpr noexcept]
QPointer::
QPointer
(
std::nullptr_t
)
構造守衛指針采用值
nullptr
.
另請參閱 isNull ().
[noexcept, since 6.6]
template <typename X, QPointer<T>::if_convertible<X> = true> QPointer::
QPointer
(
QPointer
<
X
> &&
other
)
[noexcept, since 6.6]
template <typename X, QPointer<T>::if_convertible<X> = true> QPointer::
QPointer
(const
QPointer
<
X
> &
other
)
Conversion constructor. Constructs a new QPointer by moving or copying from other .
The moved-from QPointer is reset to nullptr.
注意:
These constructors participate in overload resolution only if
X*
is convertible to
T*
.
該函數在 Qt 6.6 引入。
構造守衛指針指嚮同一對象 p 所指嚮。
銷毀守衛指針。就像正常指針,銷毀守衛指針 not 銷毀所指嚮的對象。
[noexcept]
void
QPointer::
clear
()
清零此 QPointer 對象。
另請參閱 isNull ().
[noexcept]
T
*QPointer::
data
() const
返迴指嚮被守衛對象的指針。
[noexcept, since 6.0]
T
*QPointer::
get
() const
如同 data ()。此函數為兼容 STL (標準模闆庫) 提供。
該函數在 Qt 6.0 引入。
[noexcept]
bool
QPointer::
isNull
() const
返迴
true
if the referenced object has been destroyed or if there is no referenced object; otherwise returns
false
.
[noexcept]
void
QPointer::
swap
(
QPointer
<
T
> &
other
)
Swaps this pointer with other 。此操作很快且從不失敗。
[noexcept]
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.
[noexcept]
T
&QPointer::
operator*
() const
Dereference operator; implements pointer semantics. Just use this operator as you would with a normal C++ pointer.
[noexcept]
T
*QPointer::
operator->
() const
Overloaded arrow operator; implements pointer semantics. Just use this operator as you would with a normal C++ pointer.
[noexcept, since 6.6.1]
template <typename X, QPointer<T>::if_convertible<X> = true>
QPointer
<
T
> &QPointer::
operator=
(
QPointer
<
X
> &&
other
)
Conversion move-assignment operator. Makes this guarded pointer guard the same object guarded by other and resets other to nullptr.
注意:
This operator participates in overload resolution only if
X*
is convertible to
T*
.
This function was introduced in Qt 6.6.1.
賦值運算符。此守衛指針現在將指嚮同一對象 p 所指嚮。
[noexcept, since 6.6]
template <typename X, QPointer<T>::if_convertible<X> = true>
QPointer
<
T
> &QPointer::
operator=
(const
QPointer
<
X
> &
other
)
Conversion assignment operator. Makes this guarded pointer guard the same object guarded by other .
注意:
This operator participates in overload resolution only if
X*
is convertible to
T*
.
該函數在 Qt 6.6 引入。
[noexcept]
template <typename X>
bool
operator!=
(
X
*const &
lhs
, const
QPointer
<
T
> &
rhs
)
不相等操作符。返迴
true
if
lhs
和守衛指針
rhs
未指嚮同一對象,否則返迴
false
.
[noexcept]
template <typename X>
bool
operator!=
(const
QPointer
<
T
> &
lhs
,
X
*const &
rhs
)
不相等操作符。返迴
true
if
rhs
和守衛指針
lhs
未指嚮同一對象,否則返迴
false
.
[noexcept(...)]
template <typename X>
bool
operator!=
(const
QPointer
<
T
> &
lhs
, const
QPointer
<
X
> &
rhs
)
不相等操作符。返迴
true
若守衛指針
lhs
and
rhs
未指嚮同一對象,否則返迴
false
.
注意:
This function is noexcept when
true
is
true
.
[noexcept]
bool
operator!=
(const
QPointer
<
T
> &
lhs
, const
std::nullptr_t
&
rhs
)
不相等操作符。返迴
true
if the pointer guarded by
lhs
is a valid (ie not
nullptr
) pointer, otherwise returns
false
.
[noexcept]
bool
operator!=
(const
std::nullptr_t
&
lhs
, const
QPointer
<
T
> &
rhs
)
不相等操作符。返迴
true
if the pointer guarded by
rhs
is a valid (ie not
nullptr
) pointer, otherwise returns
false
.
[noexcept]
template <typename X>
bool
operator==
(
X
*const &
lhs
, const
QPointer
<
T
> &
rhs
)
相等運算符。返迴
true
if
lhs
和守衛指針
rhs
指嚮同一對象,否則返迴
false
.
[noexcept]
template <typename X>
bool
operator==
(const
QPointer
<
T
> &
lhs
,
X
*const &
rhs
)
相等運算符。返迴
true
if
rhs
和守衛指針
lhs
指嚮同一對象,否則返迴
false
.
[noexcept(...)]
template <typename X>
bool
operator==
(const
QPointer
<
T
> &
lhs
, const
QPointer
<
X
> &
rhs
)
相等運算符。返迴
true
若守衛指針
lhs
and
rhs
指嚮同一對象,否則返迴
false
.
注意:
This function is noexcept when
true
is
true
.
[noexcept]
bool
operator==
(const
QPointer
<
T
> &
lhs
, const
std::nullptr_t
&
rhs
)
相等運算符。返迴
true
if the pointer guarded by
lhs
is
nullptr
,否則返迴
false
.
[noexcept]
bool
operator==
(const
std::nullptr_t
&
lhs
, const
QPointer
<
T
> &
rhs
)
相等運算符。返迴
true
if the pointer guarded by
rhs
is
nullptr
,否則返迴
false
.