QWeakPointer 类

The QWeakPointer class holds a weak reference to a shared pointer 更多...

头: #include <QWeakPointer>
qmake: QT += core
Since: Qt 4.5

注意: 此类的所有函数 可重入 .

公共函数

QWeakPointer ()
QWeakPointer (const QWeakPointer<T> & other )
QWeakPointer (const QSharedPointer<T> & other )
~QWeakPointer ()
void clear ()
T * data () const
bool isNull () const
QSharedPointer<T> lock () const
void swap (QWeakPointer<T> & other )
QSharedPointer<T> toStrongRef () const
operator bool () const
bool operator! () const
QWeakPointer<T> & operator= (const QWeakPointer<T> & other )
QWeakPointer<T> & operator= (const QSharedPointer<T> & other )
QSharedPointer<X> qSharedPointerCast (const QWeakPointer<T> & other )
QSharedPointer<X> qSharedPointerConstCast (const QWeakPointer<T> & other )
QSharedPointer<X> qSharedPointerDynamicCast (const QWeakPointer<T> & other )
QSharedPointer<X> qSharedPointerObjectCast (const QWeakPointer<T> & other )
QWeakPointer<X> qWeakPointerCast (const QWeakPointer<T> & other )
bool operator!= (const QSharedPointer<T> & ptr1 , const QWeakPointer<X> & ptr2 )
bool operator!= (const QWeakPointer<T> & lhs , std::nullptr_t )
bool operator!= ( std::nullptr_t , const QWeakPointer<T> & rhs )
bool operator!= (const QWeakPointer<T> & ptr1 , const QSharedPointer<X> & ptr2 )
bool operator== (const QSharedPointer<T> & ptr1 , const QWeakPointer<X> & ptr2 )
bool operator== (const QWeakPointer<T> & ptr1 , const QSharedPointer<X> & ptr2 )
bool operator== (const QWeakPointer<T> & lhs , std::nullptr_t )
bool operator== ( std::nullptr_t , const QWeakPointer<T> & rhs )

详细描述

The QWeakPointer class holds a weak reference to a shared pointer

The QWeakPointer is an automatic weak reference to a pointer in C++. It cannot be used to dereference the pointer directly, but it can be used to verify if the pointer has been deleted or not in another context.

QWeakPointer objects can only be created by assignment from a QSharedPointer .

It's important to note that QWeakPointer provides no automatic casting operators to prevent mistakes from happening. Even though QWeakPointer tracks a pointer, it should not be considered a pointer itself, since it doesn't guarantee that the pointed object remains valid.

Therefore, to access the pointer that QWeakPointer is tracking, you must first promote it to QSharedPointer and verify if the resulting object is null or not. QSharedPointer guarantees that the object isn't deleted, so if you obtain a non-null object, you may use the pointer. See QWeakPointer::toStrongRef () 范例。

QWeakPointer 还提供 QWeakPointer::data () method that returns the tracked pointer without ensuring that it remains valid. This function is provided if you can guarantee by external means that the object will not get deleted (or if you only need the pointer value) and the cost of creating a QSharedPointer 使用 toStrongRef () is too high.

另请参阅 QSharedPointer and QScopedPointer .

成员函数文档编制

QWeakPointer:: QWeakPointer ()

创建 QWeakPointer that points to nothing.

QWeakPointer:: QWeakPointer (const QWeakPointer < T > & other )

创建 QWeakPointer that holds a weak reference to the pointer referenced by other .

T is a derived type of the template parameter of this class, QWeakPointer will perform an automatic cast. Otherwise, you will get a compiler error.

QWeakPointer:: QWeakPointer (const QSharedPointer < T > & other )

创建 QWeakPointer that holds a weak reference to the pointer referenced by other .

T is a derived type of the template parameter of this class, QWeakPointer will perform an automatic cast. Otherwise, you will get a compiler error.

QWeakPointer:: ~QWeakPointer ()

销毁此 QWeakPointer object. The pointer referenced by this object will not be deleted.

void QWeakPointer:: clear ()

清零此 QWeakPointer object, dropping the reference that it may have had to the pointer.

T *QWeakPointer:: data () const

Returns the value of the pointer being tracked by this QWeakPointer , without ensuring that it cannot get deleted. To have that guarantee, use toStrongRef (), which returns a QSharedPointer object. If this function can determine that the pointer has already been deleted, it returns 0.

It is ok to obtain the value of the pointer and using that value itself, like for example in debugging statements:

qDebug("Tracking %p", weakref.data());
					

However, dereferencing the pointer is only allowed if you can guarantee by external means that the pointer does not get deleted. For example, if you can be certain that no other thread can delete it, nor the functions that you may call.

If that is the case, then the following code is valid:

// this pointer cannot be used in another thread
// so other threads cannot delete it
QWeakPointer<int> weakref = obtainReference();
Object *obj = weakref.data();
if (obj) {
    // if the pointer wasn't deleted yet, we know it can't get
    // deleted by our own code here nor the functions we call
    otherFunction(obj);
}
					

Use this function with care.

该函数在 Qt 4.6 引入。

另请参阅 isNull () 和 toStrongRef ().

bool QWeakPointer:: isNull () const

返回 true if this object is holding a reference to a null pointer.

Note that, due to the nature of weak references, the pointer that QWeakPointer references can become null at any moment, so the value returned from this function can change from false to true from one call to the next.

QSharedPointer < T > QWeakPointer:: lock () const

如同 toStrongRef ().

This function is provided for API compatibility with std::weak_ptr.

该函数在 Qt 5.4 引入。

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

Swaps this weak pointer instance with other 。此函数非常快且从不失败。

该函数在 Qt 5.4 引入。

QSharedPointer < T > QWeakPointer:: toStrongRef () const

Promotes this weak reference to a strong one and returns a QSharedPointer object holding that reference. When promoting to QSharedPointer , this function verifies if the object has been deleted already or not. If it hasn't, this function increases the reference count to the shared object, thus ensuring that it will not get deleted.

Since this function can fail to obtain a valid strong reference to the shared object, you should always verify if the conversion succeeded, by calling QSharedPointer::isNull () on the returned object.

For example, the following code promotes a QWeakPointer that was held to a strong reference and, if it succeeded, it prints the value of the integer that was held:

QWeakPointer<int> weakref;
// ...
QSharedPointer<int> strong = weakref.toStrongRef();
if (strong)
    qDebug() << "The value is:" << *strong;
else
    qDebug() << "The value has already been deleted";
					

另请参阅 QSharedPointer::QSharedPointer ().

QWeakPointer:: operator bool () const

返回 true if this object is not null. This function is suitable for use in if-constructs , like:

if (weakref) { ... }
					

Note that, due to the nature of weak references, the pointer that QWeakPointer references can become null at any moment, so the value returned from this function can change from true to false from one call to the next.

另请参阅 isNull ().

bool QWeakPointer:: operator! () const

返回 true if this object is null. This function is suitable for use in if-constructs , like:

if (!weakref) { ... }
					

Note that, due to the nature of weak references, the pointer that QWeakPointer references can become null at any moment, so the value returned from this function can change from false to true from one call to the next.

另请参阅 isNull ().

QWeakPointer < T > &QWeakPointer:: operator= (const QWeakPointer < T > & other )

Makes this object share other 's pointer. The current pointer reference is discarded but is not deleted.

T is a derived type of the template parameter of this class, QWeakPointer will perform an automatic cast. Otherwise, you will get a compiler error.

QWeakPointer < T > &QWeakPointer:: operator= (const QSharedPointer < T > & other )

Makes this object share other 's pointer. The current pointer reference is discarded but is not deleted.

T is a derived type of the template parameter of this class, QWeakPointer will perform an automatic cast. Otherwise, you will get a compiler error.

相关非成员

QSharedPointer < X > qSharedPointerCast (const QWeakPointer < T > & other )

Returns a shared pointer to the pointer held by other , cast to type X . The types T and X must belong to one hierarchy for the static_cast to succeed.

The other object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function returns a null QSharedPointer .

注意, X must have the same cv-qualifiers ( const and volatile ) that T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.

另请参阅 QWeakPointer::toStrongRef (), qSharedPointerDynamicCast (),和 qSharedPointerConstCast ().

QSharedPointer < X > qSharedPointerConstCast (const QWeakPointer < T > & other )

Returns a shared pointer to the pointer held by other , cast to type X . The types T and X must belong to one hierarchy for the const_cast to succeed. The const and volatile differences between T and X 被忽略。

The other object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function returns a null QSharedPointer .

另请参阅 QWeakPointer::toStrongRef (), qSharedPointerCast (),和 qSharedPointerDynamicCast ().

QSharedPointer < X > qSharedPointerDynamicCast (const QWeakPointer < T > & other )

Returns a shared pointer to the pointer held by other , using a dynamic cast to type X to obtain an internal pointer of the appropriate type. If the dynamic_cast fails, the object returned will be null.

The other object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function also returns a null QSharedPointer .

注意, X must have the same cv-qualifiers ( const and volatile ) that T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.

另请参阅 QWeakPointer::toStrongRef (), qSharedPointerCast (),和 qSharedPointerConstCast ().

QSharedPointer < X > qSharedPointerObjectCast (const QWeakPointer < T > & other )

The qSharedPointerObjectCast function is for casting a shared pointer.

Returns a shared pointer to the pointer held by other , using a qobject_cast () to type X to obtain an internal pointer of the appropriate type. If the qobject_cast fails, the object returned will be null.

The other object is converted first to a strong reference. If that conversion fails (because the object it's pointing to has already been deleted), this function also returns a null QSharedPointer .

注意, X must have the same cv-qualifiers ( const and volatile ) that T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.

该函数在 Qt 4.6 引入。

另请参阅 QWeakPointer::toStrongRef (), qSharedPointerCast (),和 qSharedPointerConstCast ().

QWeakPointer < X > qWeakPointerCast (const QWeakPointer < T > & other )

Returns a weak pointer to the pointer held by other , cast to type X . The types T and X must belong to one hierarchy for the static_cast to succeed.

注意, X must have the same cv-qualifiers ( const and volatile ) that T has, or the code will fail to compile. Use qSharedPointerConstCast to cast away the constness.

bool operator!= (const QSharedPointer < T > & ptr1 , const QWeakPointer < X > & ptr2 )

返回 true if the pointer referenced by ptr1 is not the same pointer as that referenced by ptr2 .

ptr2 's template parameter is different from ptr1 's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2 's template parameter is not a base or a derived type from ptr1 's, you will get a compiler error.

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

返回 true if the pointer referenced by lhs is a valid (i.e. non-null) pointer.

该函数在 Qt 5.8 引入。

另请参阅 QWeakPointer::isNull ().

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

返回 true if the pointer referenced by rhs is a valid (i.e. non-null) pointer.

该函数在 Qt 5.8 引入。

另请参阅 QWeakPointer::isNull ().

bool operator!= (const QWeakPointer < T > & ptr1 , const QSharedPointer < X > & ptr2 )

返回 true if the pointer referenced by ptr1 is not the same pointer as that referenced by ptr2 .

ptr2 's template parameter is different from ptr1 's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2 's template parameter is not a base or a derived type from ptr1 's, you will get a compiler error.

bool operator== (const QSharedPointer < T > & ptr1 , const QWeakPointer < X > & ptr2 )

返回 true if the pointer referenced by ptr1 is the same pointer as that referenced by ptr2 .

ptr2 's template parameter is different from ptr1 's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2 's template parameter is not a base or a derived type from ptr1 's, you will get a compiler error.

bool operator== (const QWeakPointer < T > & ptr1 , const QSharedPointer < X > & ptr2 )

返回 true if the pointer referenced by ptr1 is the same pointer as that referenced by ptr2 .

ptr2 's template parameter is different from ptr1 's, QSharedPointer will attempt to perform an automatic static_cast to ensure that the pointers being compared are equal. If ptr2 's template parameter is not a base or a derived type from ptr1 's, you will get a compiler error.

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

返回 true if the pointer referenced by lhs is a null pointer.

该函数在 Qt 5.8 引入。

另请参阅 QWeakPointer::isNull ().

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

返回 true if the pointer referenced by rhs is a null pointer.

该函数在 Qt 5.8 引入。

另请参阅 QWeakPointer::isNull ().