QPointer
QPointer can only point to QObject instances. It will be automatically set to nullptr if the pointed to object is destroyed. It is a weak pointer specialized for QObject.
QPointer只能指向QObject实例。如果指向的对象被销毁,它将自动设置为 nullptr。它是一个专门用于QObject的弱指针。
template <typename T>
class QPointer
QObject *obj = new QObject;
QPointer<QObject> pObj(obj);
delete obj;
Q_ASSERT(pObj.isNull()); // pObj will be nullptr now
PS:请注意,类T必须继承自QObject,否则将导致编译或链接错误。
QSharedPointer
A reference-counted pointer. The actual object will only be deleted, when all shared pointers are destroyed. Equivalent to std::shared_ptr.
引用计数指针。只有当所有共享指针都被销毁时,实际对象才会被删除。相当于 std::shared_ptr。
template <typename T>
class QSharedPointer
int *pI = new int;
QSharedPointer<int> pI1(pI);
QSharedPointer