QT close 没有调用析构?

franzhong 2020-12-23 01:51:50

Login::Login(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::Login)
{
ui->setupUi(this);
。。。
}
Login::~Login()
{
qDebug()<< "inner destrucor...";
delete ui;

}
Login::Jump123(){
//Mainpage *mp = new... // 另一个界面
//mp.show(); // 大概这意思
this->close();
qDebug()<< "inner Jump...";
}

运行后,页面跳转问题的,但是没有进入login的析构函数,只有关掉窗口才能调用,那close与hide有啥区别,甚至用了this->destory();同样没有销毁
另外,这个close还是QWidget的一个槽,页面跳转,怎么销毁当前页面,做到真正的跳转?
[slot] bool QWidget::close()
Closes this widget. Returns true if the widget was closed; otherwise returns false.
First it sends the widget a QCloseEvent. The widget is hidden if it accepts the close event. If it ignores the event, nothing happens. The default implementation of QWidget::closeEvent() accepts the close event.
If the widget has the Qt::WA_DeleteOnClose flag, the widget is also deleted. A close events is delivered to the widget no matter if the widget is visible or not.
The QApplication::lastWindowClosed() signal is emitted when the last visible primary window (i.e. window with no parent) with the Qt::WA_QuitOnClose attribute set is closed. By default this attribute is set for all widgets except transient windows such as splash screens, tool windows, and popup menus.
...全文
3314 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
龟斯特布鲁克 2022-02-28
  • 打赏
  • 举报
回复

加入setAttribute(Qt::WA_DeleteOnClose); 再调用close 就会释放new出来的对象,如果对象创建在栈上 setAttribute(Qt::WA_DeleteOnClose); 调用close 会出现异常

donwmufromdying 2020-12-24
  • 打赏
  • 举报
回复
本身close就不是说析构,这也是有原因的,比如你一个对话框里有一些成员变量保存着一些对话框中录入的数据,你对话框close后你还要访问这些数据,要是一close就析构了,你拿不拿得到数据了?如下场景

AAA::getSomeParam {
    MyDialog *dlg=new MyDialog(this);
    dlg->show();
    dlg->exec();
    dlg->close(); //一般我们没有这一句。因为对话框关闭基本上都是你点关闭来处理的
    QString param1=dlg->getParam();
    delete dlg;
}
franzhong 2020-12-24
  • 打赏
  • 举报
回复
十分感谢三位的回答,对我很有帮助
mideum 2020-12-24
  • 打赏
  • 举报
回复
close本来就不销毁的,它跟hide的区别你贴出的文档里不都说了嘛
如果你想要close以后自动销毁就用上面说的DeleteOnClose属性
lwydezh 2020-12-23
  • 打赏
  • 举报
回复
你好,我试一下加了setAttribute(Qt::WA_DeleteOnClose);可以
#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    close();
}

16,816

社区成员

发帖
与我相关
我的任务
社区描述
Qt 是一个跨平台应用程序框架。通过使用 Qt,您可以一次性开发应用程序和用户界面,然后将其部署到多个桌面和嵌入式操作系统,而无需重复编写源代码。
社区管理员
  • Qt
  • 亭台六七座
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧