加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

qt 程序关闭后 但进程列表显示仍存在!

(2010-06-17 09:15:25)
标签:

杂谈


QWidget *window = new QWidget;
    window->showNormal();
    window->setAttribute( Qt::WA_DeleteOnClose, true ); // 参考文档里说设置了这一项属性后
                                                           // 在close()隐藏窗口的同时会销毁窗口
    window->close();

可是程序运行后为什么任务管理器的进程里还有呢???请赐教~如何修改~~~
在windows+Eclipse+CDT+QT下。。。

完整代码:
#include <QApplication>
#include <QWidget>
int main(int argc, char *argv[])
{
    QApplication app(argc, argv);
                     QWidget *window = new QWidget;
    window->showNormal();
    window->setAttribute( Qt::WA_DeleteOnClose, true );
    window->close();
    return app.exec();
}

-------------------------------------------------------

下面是关于 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 可以不用关闭,程序运行结束的时候会自动关闭

复制代码
  1. #include <QApplication>
  2. #include <QWidget>
  3. int main(int argc, char *argv[])
  4. {
  5.     QApplication app(argc, argv);
  6.     QWidget *window = new QWidget;
  7.     window->showNormal();
  8.     return app.exec();
  9. }

---------------------------------------------

下面是关于 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.
.......

调用close()是想让窗口自动关闭并退出,而不需要手动关闭窗口。
我也发现了,手动的话进程是退出的。这部分文档也看了。。。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有