下面是关于 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 可以不用关闭,程序运行结束的时候会自动关闭
- #include <QApplication>
- #include <QWidget>
- int main(int argc, char *argv[])
- {
- QApplication app(argc, argv);
- QWidget *window = new QWidget;
- window->showNormal();
- return app.exec();
- }
[ 此帖被wader在2009-08-25 00:26重新编辑 ]