The QPointer class is a template class that provides guarded pointers to QObject . 更多...
| 頭: | #include <QPointer> |
| qmake: | QT += core |
| QPointer () | |
| QPointer (T * p ) | |
| ~QPointer () | |
| void | clear () |
| T * | data () const |
| bool | isNull () const |
| void | swap (QPointer & other ) |
| 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 ) |
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 由他人擁有,因此仍然可能在保持其引用時被銷毀。可以安全地測試,指針的有效性。
Note that Qt 5 introduces a slight change in behavior when using QPointer .
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 , QObject ,和 QObjectCleanupHandler .
Constructs a 0 guarded pointer.
另請參閱 isNull ().
構造守衛指針指嚮同一對象 p 所指嚮。
銷毀守衛指針。就像正常指針,銷毀守衛指針 not 銷毀所指嚮的對象。
清零此 QPointer 對象。
該函數在 Qt 5.0 引入。
另請參閱 isNull ().
返迴指嚮被守衛對象的指針。
該函數在 Qt 4.4 引入。
返迴
true
if the referenced object has been destroyed or if there is no referenced object; otherwise returns
false
.
交換內容對於此 QPointer 采用內容源於 other 。此操作很快且從不失敗。
該函數在 Qt 5.6 引入。
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
if
o
和守衛指針
p
未指嚮同一對象,否則返迴
false
.
不相等操作符。返迴
true
if
o
和守衛指針
p
未指嚮同一對象,否則返迴
false
.
不相等操作符。返迴
true
if
o
和守衛指針
p
未指嚮同一對象,否則返迴
false
.
不相等操作符。返迴
true
if
o
和守衛指針
p
未指嚮同一對象,否則返迴
false
.
不相等操作符。返迴
true
若守衛指針
p1
and
p2
未指嚮同一對象,否則返迴
false
.
相等運算符。返迴
true
if
o
和守衛指針
p
指嚮同一對象,否則返迴
false
.
相等運算符。返迴
true
if
o
和守衛指針
p
指嚮同一對象,否則返迴
false
.
相等運算符。返迴
true
if
o
和守衛指針
p
指嚮同一對象,否則返迴
false
.
相等運算符。返迴
true
if
o
和守衛指針
p
指嚮同一對象,否則返迴
false
.
相等運算符。返迴
true
若守衛指針
p1
and
p2
指嚮同一對象,否則返迴
false
.