summaryrefslogtreecommitdiffstats
path: root/src/corelib/kernel/qeventloop.cpp
diff options
context:
space:
mode:
authorStephen Kelly <stephen.kelly@kdab.com>2011-10-26 13:29:51 +0200
committerQt by Nokia <qt-info@nokia.com>2012-01-31 23:22:15 +0100
commita512e210ac5b032c5fc2edf1ddf72e5a414485fd (patch)
tree0e9615f317fe5c24bd002155b205a01d5716a1d3 /src/corelib/kernel/qeventloop.cpp
parent36a590c90ddab6fa438b7b54b05d89e8f9698a52 (diff)
Add the event loop quitlock feature to QtCore.
A feature of a ref-counted quit (managed by a quit-lock class) is added to both QEventLoop and QCoreApplication. This allows, for example, an event loop to quit() when there is no more work for it to do. quitOnLastWindowClosed is implemented in terms of the refcount in QCoreApplication so that jobs can be completed before the application quits. Change-Id: I14c8f4e7ee12bbf81a6e5849290d4c8ff37fa110 Reviewed-by: David Faure <faure@kde.org> Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'src/corelib/kernel/qeventloop.cpp')
-rw-r--r--src/corelib/kernel/qeventloop.cpp105
1 files changed, 94 insertions, 11 deletions
diff --git a/src/corelib/kernel/qeventloop.cpp b/src/corelib/kernel/qeventloop.cpp
index 0c1b8c77ff..8b0ec85679 100644
--- a/src/corelib/kernel/qeventloop.cpp
+++ b/src/corelib/kernel/qeventloop.cpp
@@ -43,24 +43,15 @@
#include "qabstracteventdispatcher.h"
#include "qcoreapplication.h"
+#include "qcoreapplication_p.h"
#include "qelapsedtimer.h"
#include "qobject_p.h"
+#include "qeventloop_p.h"
#include <private/qthread_p.h>
QT_BEGIN_NAMESPACE
-class QEventLoopPrivate : public QObjectPrivate
-{
- Q_DECLARE_PUBLIC(QEventLoop)
-public:
- inline QEventLoopPrivate()
- : exit(true), inExec(false), returnCode(-1)
- { }
- bool exit, inExec;
- int returnCode;
-};
-
/*!
\class QEventLoop
\brief The QEventLoop class provides a means of entering and leaving an event loop.
@@ -305,6 +296,17 @@ void QEventLoop::wakeUp()
d->threadData->eventDispatcher->wakeUp();
}
+
+bool QEventLoop::event(QEvent *event)
+{
+ if (event->type() == QEvent::Quit) {
+ quit();
+ return true;
+ } else {
+ return QObject::event(event);
+ }
+}
+
/*!
Tells the event loop to exit normally.
@@ -315,4 +317,85 @@ void QEventLoop::wakeUp()
void QEventLoop::quit()
{ exit(0); }
+
+class QEventLoopLockerPrivate
+{
+public:
+ explicit QEventLoopLockerPrivate(QEventLoopPrivate *loop)
+ : loop(loop), app(0)
+ {
+ loop->ref();
+ }
+
+ explicit QEventLoopLockerPrivate(QCoreApplicationPrivate *app)
+ : loop(0), app(app)
+ {
+ app->ref();
+ }
+
+ ~QEventLoopLockerPrivate()
+ {
+ if (loop)
+ loop->deref();
+ else
+ app->deref();
+ }
+
+private:
+ QEventLoopPrivate *loop;
+ QCoreApplicationPrivate *app;
+};
+
+/*!
+ \class QEventLoopLocker
+ \brief The QEventLoopLocker class provides a means to quit an event loop when it is no longer needed.
+
+ The QEventLoopLocker operates on particular objects - either a QCoreApplication
+ instance or a QEventLoop instance.
+
+ This makes it possible to, for example, run a batch of jobs with an event loop
+ and exit that event loop after the last job is finished. That is accomplished
+ by keeping a QEventLoopLocker with each job instance.
+
+ The variant which operates on QCoreApplication makes it possible to finish
+ asynchronously running jobs after the last gui window has been closed. This
+ can be useful for example for running a job which uploads data to a network.
+
+ \sa QEventLoop, QCoreApplication
+*/
+
+/*!
+ Creates an event locker operating on the \p app.
+
+ The application will quit when there are no more QEventLoopLockers operating on it.
+
+ \sa QCoreApplication::quit(), QCoreApplication::isQuitLockEnabled()
+ */
+QEventLoopLocker::QEventLoopLocker()
+ : d_ptr(new QEventLoopLockerPrivate(static_cast<QCoreApplicationPrivate*>(QObjectPrivate::get(QCoreApplication::instance()))))
+{
+
+}
+
+/*!
+ Creates an event locker operating on the \p app.
+
+ This particular QEventLoop will quit when there are no more QEventLoopLockers operating on it.
+
+ \sa QEventLoop::quit()
+ */
+QEventLoopLocker::QEventLoopLocker(QEventLoop *loop)
+ : d_ptr(new QEventLoopLockerPrivate(static_cast<QEventLoopPrivate*>(QObjectPrivate::get(loop))))
+{
+
+}
+
+/*!
+ Destroys this event loop locker object
+ */
+QEventLoopLocker::~QEventLoopLocker()
+{
+ delete d_ptr;
+}
+
QT_END_NAMESPACE