简体   繁体   English

QT4内存管理

[英]QT4 Memory Management

I come from a fairly strong C background, and have a rather solid foundation in C++. 我来自一个相当强大的C背景,并且在C ++中有相当坚实的基础。 More recently I've been working with C# and other higher level languages. 最近我一直在使用C#和其他更高级别的语言。 A project I'm looking at working on could really benefit from using QT4, but I have some questions on memory management I can't seem to understand. 我正在研究的项目可以从使用QT4中获益,但我对内存管理有一些问题,我似乎无法理解。 I've read the QT4 documentation and it hasn't helped me much. 我已经阅读了QT4文档,但对我没什么帮助。 So that is why I'm here. 所以这就是我在这里的原因。

1) Okay so to start with, I understand that the easiest way to use QT4 objects is to declare them locally: 1)好的,首先,我了解使用QT4对象的最简单方法是在本地声明它们:

void MyFunc()
{
     QString foo;
     // do stuff to foo

}

This is simple enough, I can take that object, and pass it around and know that when it goes out of scope it will be destroyed. 这很简单,我可以拿走那个对象,并传递它,并知道当它超出范围时它将被销毁。 But here's my question. 但这是我的问题。

1)If I create a QList and add objects to it, and then the QList goes out of scope, will it try to deallocate the child objects? 1)如果我创建一个QList并向其添加对象,然后QList超出范围,它是否会尝试释放子对象?

2)If a QT4 routine returns a pointer to an object, am I then responsible for the de-allocation of that object? 2)如果QT4例程返回指向对象的指针,那么我是否负责取消该对象的分配?

3)If I create a subclass of a QWidget, and add it to a QWindow, how do I insure that when the QWindow is destroyed, that it takes my widget with it? 3)如果我创建QWidget的子类,并将其添加到QWindow,我如何确保在QWindow被销毁时,它需要我的小部件?

Thanks for the help. 谢谢您的帮助。

If I create a QList and add objects to it, and then the QList goes out of scope, will it try to deallocate the child objects? 如果我创建一个QList并向其添加对象,然后QList超出范围,它是否会尝试释放子对象?

QList is just like std::list. QList就像std :: list一样。 It will destroy the contained objects when it is destroyed. 它会在被破坏时销毁所包含的对象。

If a Qt4 routine returns a pointer to an object, am I then responsible for the de-allocation of that object? 如果Qt4​​例程返回指向对象的指针,那么我是否负责该对象的取消分配?

Usually no, the docs should specify what happens. 通常不会,文档应该指明发生了什么。 An exception are the take* functions (eg: QTableWidget::takeItem). take *函数是一个例外(例如:QTableWidget :: takeItem)。

If I create a subclass of a QWidget, and add it to a QWindow, how do I insure that when the QWindow is destroyed, that it takes my widget with it? 如果我创建一个QWidget的子类,并将其添加到QWindow,我如何确保在QWindow被销毁时,它需要我的小部件呢?

It depends on how you create the subclass object. 这取决于您如何创建子类对象。

  • You could add it as a member of the window widget (there is no QWindow, by the way) and it will be destroyed just like any member variable. 您可以将其添加为窗口小部件的成员(顺便说一下,没有QWindow),它将像任何成员变量一样被销毁。
  • You could allocate it with new and pass it the window as parent and it will be deleted thanks to the Qt object tree implementation (as cake mentioned) 您可以使用new分配它,并将窗口作为父窗口传递给它,由于Qt对象树的实现,它将被删除(如提到的蛋糕)
  • You could do the memory management yourself. 你可以自己做内存管理。

When a QWidget (or any QObject) is destroyed, it will remove itself from its parent's to-delete list so you can delete it yourself and not worry about double deletes. 当一个QWidget(或任何QObject)被销毁时,它会从其父级的删除列表中删除它,这样你就可以自己删除它而不用担心双重删除。

You might want to start here: Object Trees as this explains the parent/child relationship Qt uses (and all the other links given so far are out of date or 3rd party). 你可能想从这里开始: 对象树,因为这解释了Qt使用的父/子关系(到目前为止给出的所有其他链接都是过时的或第三方)。

1)If I create a QList and add objects to it, and then the QList goes out of scope, will it try to deallocate the child objects? 1)如果我创建一个QList并向其添加对象,然后QList超出范围,它是否会尝试释放子对象?

Yes, it behaves just like std::list. 是的,它的行为就像std :: list一样。 Just like std::list, it will only deallocate not delete. 就像std :: list一样,它只会释放不删除。 This is where some of the Qt pointer classes come in handy 这是一些Qt指针类派上用场的地方

2)If a QT4 routine returns a pointer to an object, am I then responsible for the de-allocation of that object? 2)如果QT4例程返回指向对象的指针,那么我是否负责取消该对象的分配?

As rpg said, usually not, but if you are the documentation will say so. 正如RPG所说,通常不会,但如果你是文件就会这么说。

3)If I create a subclass of a QWidget, and add it to a QWindow, how do I insure that when the QWindow is destroyed, that it takes my widget with it? 3)如果我创建QWidget的子类,并将其添加到QWindow,我如何确保在QWindow被销毁时,它需要我的小部件?

When you create your subclass, be sure to use the parent parameter in the ctor. 创建子类时,请确保在ctor中使用parent参数。 This way the object will be deleted by Qt. 这样,Qt就会删除该对象。 If needed, you can set the parent after with QObject::setParent ( QObject * parent ) . 如果需要,可以使用QObject :: setParent(QObject * parent)设置父项

The answer is the Parent/Child object relationship that exist in Qt. 答案是Qt中存在的父/子对象关系。 When an parent object goes out of scope, or is destroyed by any other means, Qt ensures that all it's child objects are destroyed to. 当父对象超出范围,或被任何其他方式销毁时,Qt确保将其所有子对象销毁。

More about this behavior can be found here -> http://doc.trolltech.com/4.4/objecttrees.html 有关此行为的更多信息,请访问此处 - > http://doc.trolltech.com/4.4/objecttrees.html

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

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