简体   繁体   English

将Qt对象与std :: shared_ptr一起使用

[英]using Qt objects with std::shared_ptr

I'm trying to update a small utility application to a more modern C++ fashion, but I'm having problems using some Qt objects with std::shared_ptr, especially those that receive some other QWidget as a constructor argument. 我正在尝试将一个小实用程序应用程序更新为更现代的C ++方式,但是我在使用std :: shared_ptr的一些Qt对象时遇到了问题,特别是那些接收其他QWidget作为构造函数参数的那些。

For example: 例如:

private:
    std::shared_ptr<QWidget> centralwidget;
    std::shared_ptr<QVBoxLayout> verticalLayout;

public:
    void setupUi(QMainWindow *MainWindow) // this pointer is a .get() from a shared_ptr
    {
        centralwidget = std::make_shared<QWidget>(new QWidget(MainWindow)); // compiles fine
        verticalLayout = std::make_shared<QVBoxLayout>(new QVBoxLayout(centralwidget.get())); // does not compile
    }

The compile error is: 编译错误是:

Error 1 error C2664: 'QVBoxLayout::QVBoxLayout(QWidget *)' : cannot convert parameter 1 from 'QVBoxLayout *' to 'QWidget *' e:\\microsoft visual studio 11.0\\vc\\include\\memory 855 错误1错误C2664:'QVBoxLayout :: QVBoxLayout(QWidget *)':无法将参数1从'QVBoxLayout *'转换为'QWidget *'e:\\ microsoft visual studio 11.0 \\ vc \\ include \\ memory 855

I cant seem to understand this error, I'm not converting anything, I'm just trying to create a QVBoxLayout object and passing a QWidget as its parent (like i would do with raw pointers). 我似乎无法理解这个错误,我没有转换任何东西,我只是想创建一个QVBoxLayout对象并传递一个QWidget作为其父(就像我会用原始指针)。

In general, I try to avoid using shared_ptr for Qt GUI objects, since Qt already provides its own memory management mechanism. 一般来说,我尽量避免使用shared_ptr用于Qt GUI对象,因为Qt已经提供了自己的内存管理机制。 Each QObject possibly has a parent, and when this parent dies he deletes all of its children. 每个QObject可能都有一个父对象,当这个父对象死亡时,他会删除它的所有子对象。 A shared_pointer is not required here and doesn't give you any added value: you could perfectly use a raw pointer without creating a memory leak. 这里不需要shared_pointer,也没有给你任何附加值:你可以完美地使用原始指针而不会产生内存泄漏。

Generally speaking, if the QObject's parent dies before the last shared_ptr instance is deleted, you'll quickly get into troubles since the object will be deleted a second time when the last shared_ptr will be destroyed. 一般来说,如果QObject的父级在删除最后一个shared_ptr实例之前就死了,那么你很快就会遇到麻烦,因为当最后一个shared_ptr被销毁时,该对象将被第二次删除。 That's not the case here, but be careful :) 这不是这里的情况,但要小心:)

The arguments to std::make_shared are passed to the constructor of the class you're instantiating. std::make_shared的参数将传递给您要实例化的类的构造函数。 So basically what you are doing is equivalent to: 所以基本上你所做的相当于:

new QVBoxLayout(new QVBoxLayout(centralwidget.get()))

I think what you are trying to do is : 我想你要做的是:

centralwidget = std::make_shared<QWidget>(MainWindow);
verticalLayout = std::make_shared<QVBoxLayout>(centralwidget.get());

Have a look at the documentation of std::make_shared (for example here ). 看看std::make_shared的文档(例如这里 )。 The whole point of this function is to allocate the reference count near the object instance in memory, so you have to let it do the allocation for you. 这个函数的重点是在内存中的对象实例附近分配引用计数,所以你必须让它为你做分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM