析构函数在Qt4

编程入门 行业动态 更新时间:2024-10-23 01:39:24
本文介绍了析构函数在Qt4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我很困惑在Qt4中使用析构函数,希望你们可以帮助我。 当我有一个这样的方法(Des是一个类):

void Widget :: create(){ Des * test = new Des; test-> show(); }

如何确保这个小部件将被删除

在类Des中我有这样的:

Des :: Des() { QPushButton * push = new QPushButton(neu); QHBoxLayout * layout = new QHBoxLayout; layout-> addWidget(push); setLayout(layout); }

在哪里和如何删除* push和*应该在析构函数Des ::〜Des()?

解决方案

另一个选项使用 deleteLater ()或父母,是使用小部件的关闭时删除功能。在这种情况下,Qt会在显示完成后删除窗口小部件。

Des * test = new Des; test-> setAttribute(Qt :: WA_DeleteOnClose); test-> show();

我喜欢使用它与Qt保持的对象树,所以我设置delete-关闭窗口,并且窗口中的所有窗口小部件都指定了正确的父级,因此它们也都将被删除。

I'm very confused about using destructors in Qt4 and hope, you guys can help me. When I have a method like this (with "Des" is a class):

void Widget::create() { Des *test = new Des; test->show(); }

how can I make sure that this widget is going to be deleted after it was closed?

And in class "Des" i have this:

Des::Des() { QPushButton *push = new QPushButton("neu"); QHBoxLayout *layout = new QHBoxLayout; layout->addWidget(push); setLayout(layout); }

where and how do I have to delete *push and *layout? what should be in the destructor Des::~Des() ?

解决方案

Another option to using deleteLater(), or parents, is to use the delete-on-close functionality for widgets. In this case, Qt will delete the widget when it is done being displayed.

Des *test = new Des; test->setAttribute( Qt::WA_DeleteOnClose ); test->show();

I like to use it with the object tree that Qt keeps, so that I set delete-on-close for the window, and all widgets in the window have a proper parent specified, so they all get deleted as well.

更多推荐

析构函数在Qt4

本文发布于:2023-10-09 18:26:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1476438.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!