Obsolete Members for QWeakPointer
类QWeakPointer 的以下成员已被弃用。提供这些成员是为了保持旧源代码的正常运行。我们强烈建议不要在新代码中使用它们。
公共函数
(deprecated) | QWeakPointer(const QObject *other) |
(deprecated) T * | data() const |
(deprecated) QWeakPointer<T> & | operator=(const QObject *other) |
成员函数文档
[deprecated]
QWeakPointer::QWeakPointer(const QObject *other)
该函数已被弃用。我们强烈建议不要在新代码中使用它。
创建一个 QWeakPointer,直接持有对QObject other 的弱引用。只有当模板类型T
是QObject 或派生自 时,此构造函数才可用(否则会导致编译错误)。
您可以对任何QObject 使用此构造函数,即使它们不是用QSharedPointer 创建的。
请注意,以这种方式在任意 QObjects 上创建的 QWeakPointers 通常无法推广到QSharedPointer 。
另请参阅 QSharedPointer 和QPointer 。
[deprecated]
T *QWeakPointer::data() const
该函数已被弃用。我们强烈建议不要在新代码中使用该函数。
请使用toStrongRef() 代替,并在返回的QSharedPointer 上使用 data() 。
返回QWeakPointer 正在跟踪的指针的值,但不确保该指针不会被删除。要获得这种保证,请使用toStrongRef() 返回QSharedPointer 对象。如果该函数可以确定指针已被删除,则返回nullptr
。
获取指针的值并使用该值本身是可以的,例如在调试语句中:
qDebug("Tracking %p", weakref.data());
但是,只有在通过外部方法保证指针不会被删除的情况下,才允许取消引用指针。例如,如果你能确定没有其他线程可以删除指针,也没有你可能调用的函数可以删除指针。
如果是这种情况,那么下面的代码就是有效的:
// 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); }
谨慎使用此函数。
另请参阅 isNull() 和toStrongRef()。
[deprecated]
QWeakPointer<T> &QWeakPointer::operator=(const QObject *other)
该函数已被弃用。我们强烈建议不要在新代码中使用它。
使QWeakPointer 直接持有对QObject other 的弱引用。只有当模板类型T
是QObject 或派生自 时,该函数才可用。
另请参见 QPointer 。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.