如何确保这个小部件将被删除
在类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
发布评论