• 8392阅读
  • 15回复

程序运行后任务管理器的进程并没有结束!!! [复制链接]

上一主题 下一主题
离线yangfanxing
 
只看楼主 正序阅读 楼主  发表于: 2009-08-23
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();
}
PHPWind好恶心。。。不想看这种界面。。。
离线hendry
只看该作者 15楼 发表于: 2009-08-25
... 已经有人回答了
离线hendry
只看该作者 14楼 发表于: 2009-08-25
你只是把窗口close掉了
APP还在跑
QT里面有个设置当最后一个窗口关闭的时候 应用程序退出
默认就已经是这个设置了
所以 你肯定还有窗口没有close
离线yangfanxing
只看该作者 13楼 发表于: 2009-08-25
引用第11楼wader于2009-08-25 00:05发表的  :
下面是关于 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()是想让窗口自动关闭并退出,而不需要手动关闭窗口。
我也发现了,手动的话进程是退出的。这部分文档也看了。。。
PHPWind好恶心。。。不想看这种界面。。。
离线rqzrqh

只看该作者 12楼 发表于: 2009-08-25
引用第11楼wader于2009-08-25 00:05发表的  :
下面是关于 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.
.......

学习了
离线wader
只看该作者 11楼 发表于: 2009-08-25
下面是关于 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. }
[ 此帖被wader在2009-08-25 00:26重新编辑 ]
离线yangfanxing
只看该作者 10楼 发表于: 2009-08-24
引用第9楼rqzrqh于2009-08-24 21:18发表的  :
呼叫版主

同呼叫~~~呵呵~
PHPWind好恶心。。。不想看这种界面。。。
离线rqzrqh

只看该作者 9楼 发表于: 2009-08-24
呼叫版主
离线donixli1314

只看该作者 8楼 发表于: 2009-08-24
我试了下也是不行
离线rqzrqh

只看该作者 7楼 发表于: 2009-08-24
顶起。版版也来看看是什么原因
离线yangfanxing
只看该作者 6楼 发表于: 2009-08-24
引用第1楼xiongyu于2009-08-23 21:06发表的  :
销毁窗口不代表退出程序
quit()  才是从进程里退出哦


还要调用quit()么???
我看别人写的或者是Demo里边的没有这个提法,进程也退得好好的,自己试试怎么就不行了呢~~~
PHPWind好恶心。。。不想看这种界面。。。
离线rqzrqh

只看该作者 5楼 发表于: 2009-08-24
应该是程序还没进入事件循环之前就已经把对象销毁了,但是之后才进入app.exec(),虽然没有对象可供管理。app的退出机制应该是最终调用app的quit(),但是当在事件循环之前调用是失败的。如果在进入事件循环之后再调用close(),我想应该是可以退出程序的。
奇怪的是我在close()后面又加了一条show语句,居然没报段错误,而且调试信息显示close()之后window的内容没变,也就是说连调用close()也失败了?
[ 此帖被rqzrqh在2009-08-24 09:05重新编辑 ]
离线rqzrqh

只看该作者 4楼 发表于: 2009-08-24
引用第3楼xiongyu于2009-08-24 05:49发表的  :
以前学GTK的时候就是这样..
当window destroy的时候是不显示窗口而已,还是没有quit的
gtk+和这个不一样,gtk+调用gtk_main_quit才着正退出
离线xiongyu

只看该作者 3楼 发表于: 2009-08-24
以前学GTK的时候就是这样..

当window destroy的时候是不显示窗口而已,还是没有quit的
为自己而已努力吧... 我的博客 http://xycode.org
离线rqzrqh

只看该作者 2楼 发表于: 2009-08-23
确实很奇怪。关注中。有空自己试试
离线xiongyu

只看该作者 1楼 发表于: 2009-08-23
销毁窗口不代表退出程序

quit()  才是从进程里退出哦
为自己而已努力吧... 我的博客 http://xycode.org
快速回复
限100 字节
 
上一个 下一个