【QT】Qt智能指针QPointer、QSharedPointer、QWeakPointer、QScopedPointer

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<int> pI2 = pI1;
pI1.clear();
// pI2 is still pointing to pI, so it is not deleted
pI2.clear();
// No shared pointers anymore, pI is deleted

PS:QSharedPointer 是线程安全的,因此即使有多个线程同时修改 QSharedPointer 对象也不需要加锁。虽然 QSharedPointer 是线程安全的,但是 QSharedPointer 指向的内存区域可不一定是线程安全的。所以多个线程同时修改 QSharedPointer 指向的数据时还要应该考虑加锁。

QWeakPointer

Can hold a weak reference to a shared pointer. It will not prevent the object from being destroyed, and is simply reset. Equivalent to std::weak_ptr, where lock is equivalent to toStrongRef.
可以保存对共享指针的弱引用。它不会阻止对象被破坏,而只是重置。相当于std::weak_ptr,其中lock相当于toStrongRef

int *pI = new int;
QSharedPointer<int> pI1(pI);
QWeakPointer<int> pI2 = pI1;
pI1.clear();
// No shared pointers anymore, pI is deleted
//
// To use the shared pointer, we must "lock" it for use:
QSharedPointer<int> pI2_locked = pI2.toStrongRef();
Q_ASSERT(pI2_locked.isNull());

QScopedPointer

This is just a helper class that will delete the referenced object when the pointer goes out of scope. Thus, binds a dynamically allocated object to a variable scope.
这只是一个辅助类,当指针超出范围时,它将删除引用的对象。因此,将动态分配的对象绑定到变​​量范围。

手动管理堆分配的对象既困难又容易出错,常见的结果是代码泄漏内存,难以维护。QScopedPointer是一个小型实用程序类,它通过将基于堆栈的内存所有权分配给堆分配(通常称为资源获取即初始化(resource acquisition is initialization:RAII)来大大简化这一过程。

QScopedPointer保证当当前作用域消失时,所指向的对象将被删除。

template <typename T, typename Cleanup>
class QScopedPointer

MyClass *foo() {
    QScopedPointer<MyClass> myItem(new MyClass);
    // Some logic
    if (some condition) {
        return nullptr; // myItem will be deleted here
    }
    return myItem.take(); // Release item from scoped pointer and return it
}

Example

class UISvgIcon::Pimpl
{
public:
	Pimpl(const QString &svgPath);
	~Pimpl();
	
public:
	QScopedPointer<QDomDocument> mSvgDoc_;
	QScopedPointer<QSvgRenderer> pSvgRender_;
	QScopedPointer<QPixmap> pPixmap_;
};

UISvgIcon::Pimpl::Pimpl(const QString &svgPath)
{
	mSvgDoc_.reset(new QDomDocument());

	pSvgRender_.reset(new QSvgRenderer(mSvgDoc_->toByteArray()));

	auto tDpi = UPGLUtil::getScreen()->logicalDotsPerInch();

	pPixmap_.reset(new QPixmap(SVGProperty(mSvgDoc_->documentElement(), "svg", "width").toInt() * tDpi,
	                           SVGProperty(mSvgDoc_->documentElement(), "svg", "height").toInt() * tDpi));
}

参考文章

  1. Qt智能指针QPointer、QSharedPointer、QScopedPointer
  2. What is the difference between QPointer, QSharedPointer and QWeakPointer classes in Qt?

相关推荐

  1. boost智能指针

    2024-07-10 08:32:06       41 阅读
  2. 智能指针unique_ptr

    2024-07-10 08:32:06       47 阅读
  3. C++ 智能指针

    2024-07-10 08:32:06       35 阅读

最近更新

  1. MATLAB6:M文件和控制流

    2024-07-10 08:32:06       0 阅读
  2. early-stopping pytorch refs

    2024-07-10 08:32:06       0 阅读
  3. C++案例三:猜数字游戏

    2024-07-10 08:32:06       0 阅读
  4. 构建高可用应用的设计模式与实践

    2024-07-10 08:32:06       0 阅读
  5. MySQL简介

    2024-07-10 08:32:06       0 阅读

热门阅读

  1. Electron 简单搭建项目

    2024-07-10 08:32:06       5 阅读
  2. adb 常用的命令总结

    2024-07-10 08:32:06       8 阅读
  3. gcc: options: -specs

    2024-07-10 08:32:06       6 阅读
  4. Python题解Leetcode Hot 100之栈和堆

    2024-07-10 08:32:06       6 阅读
  5. docker容器如何与本地配置文件关联

    2024-07-10 08:32:06       6 阅读
  6. SQL 字段类型-上

    2024-07-10 08:32:06       9 阅读
  7. C++ 入门04:数组与字符串

    2024-07-10 08:32:06       3 阅读
  8. 简谈设计模式之原型模式

    2024-07-10 08:32:06       8 阅读
  9. GPT带我学-设计模式-13策略模式

    2024-07-10 08:32:06       9 阅读
  10. 写一个字符设备的驱动步骤

    2024-07-10 08:32:06       4 阅读
  11. Transformer和Bert的原理是什么

    2024-07-10 08:32:06       4 阅读