QT 中關閉視窗後 進程仍然存在

來源:互聯網
上載者:User

#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(); }

 

本意是這個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 可以不用關閉,程式運行結束的時候會自動關閉

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.