From 43f2d43e8f42446985fe7e5e56c85ebd2812d5ab Mon Sep 17 00:00:00 2001 From: d3fault Date: Sun, 3 Dec 2017 11:41:22 -0700 Subject: [PATCH] Add doc explaining the gotcha when connecting a signal to qApp->exit Change-Id: I981e4bfdf679bf755665748e9d3b389a94561e55 Reviewed-by: Oswald Buddenhagen Reviewed-by: Martin Smith --- .../code/src_corelib_kernel_qcoreapplication.cpp | 2 +- src/corelib/kernel/qcoreapplication.cpp | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/corelib/doc/snippets/code/src_corelib_kernel_qcoreapplication.cpp b/src/corelib/doc/snippets/code/src_corelib_kernel_qcoreapplication.cpp index 7d4d5f2a8d..aae2456bf1 100644 --- a/src/corelib/doc/snippets/code/src_corelib_kernel_qcoreapplication.cpp +++ b/src/corelib/doc/snippets/code/src_corelib_kernel_qcoreapplication.cpp @@ -56,7 +56,7 @@ QApplication::sendEvent(mainWindow, &event); //! [1] QPushButton *quitButton = new QPushButton("Quit"); -connect(quitButton, SIGNAL(clicked()), &app, SLOT(quit())); +connect(quitButton, SIGNAL(clicked()), &app, SLOT(quit()), Qt::QueuedConnection); //! [1] diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 38bcba275f..7c39910212 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -1370,6 +1370,13 @@ void QCoreApplicationPrivate::execCleanup() By convention, a \a returnCode of 0 means success, and any non-zero value indicates an error. + It's good practice to always connect signals to this slot using a + \l{Qt::}{QueuedConnection}. If a signal connected (non-queued) to this slot + is emitted before control enters the main event loop (such as before + "int main" calls \l{QCoreApplication::}{exec()}), the slot has no effect + and the application never exits. Using a queued connection ensures that the + slot will not be invoked until after control enters the main event loop. + Note that unlike the C library function of the same name, this function \e does return to the caller -- it is event processing that stops. @@ -1898,6 +1905,13 @@ void QCoreApplicationPrivate::maybeQuit() to quit(), and you also often connect e.g. QAbstractButton::clicked() or signals in QAction, QMenu, or QMenuBar to it. + It's good practice to always connect signals to this slot using a + \l{Qt::}{QueuedConnection}. If a signal connected (non-queued) to this slot + is emitted before control enters the main event loop (such as before + "int main" calls \l{QCoreApplication::}{exec()}), the slot has no effect + and the application never exits. Using a queued connection ensures that the + slot will not be invoked until after control enters the main event loop. + Example: \snippet code/src_corelib_kernel_qcoreapplication.cpp 1