QT 中关闭窗口后 进程仍然存在

来源:互联网 发布:java默认构造器 编辑:程序博客网 时间:2024/04/28 12:07

 

本意是这个widget closed后程序应该正常退出

 

但实际上进程依然运行。

 

Qt中对app的退出默认为最后一个窗口关闭时自动退出

 

可以通过setQuitOnLastWindowClosed来更改

 

关于 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.

红色部分,当最后一个窗口关闭的时候会产生 QApplication::lastWindowClosed() 信号,而正是这个信号使程序退出。
但是在调用 windows.close() 的时候,程序并没有进入消息循环,而操作系统是消息响应的,
QApplication::lastWindowClosed() 信号所对应的槽应该不会被执行。
而等程序进入app.exec() 之后,永远没有 quit() 执行,相当于进入的死循环

而上面的代码为什么要在那个地方调用 close() 方法? (没有理由 纯属无聊)在进入app.exec() 之前,创建的 Widget 可以不用关闭,程序运行结束的时候会自动关闭

原创粉丝点击