当 QApplication quit() 时,它会自动释放它的所有子部件吗?
我想知道像下面这样的类,是否需要手动调用 delete mainLayout
?
class Dummy : public QWidget {
public:
Dummy() { mainLayout = new QHBoxLayout(); setLayout(mainLayout); }
~Dummy() { delete mainLayout; }
private:
QHBoxLayout *mainLayout;
}
QApplication 会自动释放其所有子部件吗?
I'm wondering for a class like the following , is that necessary to call delete mainLayout
manually ?
class Dummy : public QWidget {
public:
Dummy() { mainLayout = new QHBoxLayout(); setLayout(mainLayout); }
~Dummy() { delete mainLayout; }
private:
QHBoxLayout *mainLayout;
}
Would QApplication release all its child widgets automatically ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当调用 Dummy 对象析构函数时,它的基类 QWidget 析构函数也将被调用(由 C++ 实现),并且 QWidget 析构函数负责删除其父对象是该 Dummy 对象的所有小部件。也就是说,这个 Dummy 对象的每个子对象都会被自动删除。
然后递归,因此所有子项的子项都被删除。
因此,当关闭 Qt 应用程序时,您需要手动删除的唯一 QWidget(实际上是 QObject)是那些父级为 0 的 QWidget,即顶级 QWidget。然后他们的析构函数将自动确保他们的所有孩子都被摧毁。
正如 Qt 命名空间页面中所述,还有标志 Qt::WA_DeleteOnClose 。这:
不过,我认为这个标志并不常用。因此,一个好的通用规则就是确保在应用程序关闭时删除顶级小部件。
When the Dummy object destructor is called, its base class QWidget destructor will also be called (by C++), and the QWidget destructor takes care of deleting any widgets whose parent is this Dummy object. That is, each child of this Dummy object is automatically deleted.
This then recurses, so the children of all the children are deleted.
So, when shutting down a Qt application, the only QWidgets (well, actually QObjects) you need to manually delete are those whose parent is 0, i.e. the top-level ones. Their destructors will then automatically ensure all their children are destroyed.
As documented in the Qt namespace page, there is also the flag Qt::WA_DeleteOnClose. This:
I think that this flag is not commonly used, though. So a good general rule is just to make sure you delete your top-level widgets when the application shuts down.