diff options
Diffstat (limited to 'src/corelib/kernel/qeventloop.cpp')
-rw-r--r-- | src/corelib/kernel/qeventloop.cpp | 105 |
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 |