diff options
author | Sven Anderson <Sven.Anderson@snom.com> | 2011-09-08 17:40:55 +0200 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2011-11-15 10:16:12 +0100 |
commit | 2b7d98ef8fbd6cf49326fa0bbf154e9bacbb7b49 (patch) | |
tree | 250f2752acb679a237504ea70888b0ad5efa1251 /src/corelib/kernel | |
parent | 51b7d3c8b621c2de6f98f465f478ec574bb14195 (diff) |
Allow to create a custom event dispatcher for specific QThreads.
QAbstractEventDispatcher() does no longer install itself into the current
thread. Instead the new methods QThread::setEventDispatcher() and
QCoreApplication::setEventDispatcher() allow to install a custom event
dispatcher into any QThread as long as there is no default event dispatcher
created yet. That is, before the thread has been started with
QThread::start() or, in case of the main thread, before QCoreApplication has
been instantiated.
Change-Id: I7367e13d8d8aebed5a5651260bb69b8818eb1b90
Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'src/corelib/kernel')
-rw-r--r-- | src/corelib/kernel/qabstracteventdispatcher.cpp | 29 | ||||
-rw-r--r-- | src/corelib/kernel/qabstracteventdispatcher_p.h | 1 | ||||
-rw-r--r-- | src/corelib/kernel/qcoreapplication.cpp | 29 | ||||
-rw-r--r-- | src/corelib/kernel/qcoreapplication.h | 3 |
4 files changed, 37 insertions, 25 deletions
diff --git a/src/corelib/kernel/qabstracteventdispatcher.cpp b/src/corelib/kernel/qabstracteventdispatcher.cpp index 10de2caf62..b936ac4ed2 100644 --- a/src/corelib/kernel/qabstracteventdispatcher.cpp +++ b/src/corelib/kernel/qabstracteventdispatcher.cpp @@ -49,16 +49,6 @@ QT_BEGIN_NAMESPACE -void QAbstractEventDispatcherPrivate::init() -{ - Q_Q(QAbstractEventDispatcher); - if (threadData->eventDispatcher != 0) { - qWarning("QAbstractEventDispatcher: An event dispatcher has already been created for this thread"); - } else { - threadData->eventDispatcher = q; - } -} - // we allow for 2^24 = 8^8 = 16777216 simultaneously running timers struct QtTimerIdFreeListConstants : public QFreeListDefaultConstants { @@ -127,8 +117,9 @@ void QAbstractEventDispatcherPrivate::releaseTimerId(int timerId) instance() and call functions on the QAbstractEventDispatcher object that is returned. If you want to use your own instance of QAbstractEventDispatcher or of a QAbstractEventDispatcher - subclass, you must create your instance \e before you create the - QApplication object. + subclass, you must install it with QCoreApplication::setEventDispatcher() + or QThread::setEventDispatcher() \e before a default event dispatcher has + been installed. The main event loop is started by calling QCoreApplication::exec(), and stopped by calling @@ -145,29 +136,21 @@ void QAbstractEventDispatcherPrivate::releaseTimerId(int timerId) reimplementation of QAbstractEventDispatcher that merges Qt and Motif events together. - \sa QEventLoop, QCoreApplication + \sa QEventLoop, QCoreApplication, QThread */ /*! Constructs a new event dispatcher with the given \a parent. */ QAbstractEventDispatcher::QAbstractEventDispatcher(QObject *parent) - : QObject(*new QAbstractEventDispatcherPrivate, parent) -{ - Q_D(QAbstractEventDispatcher); - d->init(); -} + : QObject(*new QAbstractEventDispatcherPrivate, parent) {} /*! \internal */ QAbstractEventDispatcher::QAbstractEventDispatcher(QAbstractEventDispatcherPrivate &dd, QObject *parent) - : QObject(dd, parent) -{ - Q_D(QAbstractEventDispatcher); - d->init(); -} + : QObject(dd, parent) {} /*! Destroys the event dispatcher. diff --git a/src/corelib/kernel/qabstracteventdispatcher_p.h b/src/corelib/kernel/qabstracteventdispatcher_p.h index 620a4499e2..31c579d865 100644 --- a/src/corelib/kernel/qabstracteventdispatcher_p.h +++ b/src/corelib/kernel/qabstracteventdispatcher_p.h @@ -67,7 +67,6 @@ public: inline QAbstractEventDispatcherPrivate() : event_filter(0) { } - void init(); QAbstractEventDispatcher::EventFilter event_filter; static int allocateTimerId(); diff --git a/src/corelib/kernel/qcoreapplication.cpp b/src/corelib/kernel/qcoreapplication.cpp index 9408d414f0..c30f6135a9 100644 --- a/src/corelib/kernel/qcoreapplication.cpp +++ b/src/corelib/kernel/qcoreapplication.cpp @@ -660,8 +660,10 @@ void QCoreApplication::init() d->createEventDispatcher(); Q_ASSERT(QCoreApplicationPrivate::eventDispatcher != 0); - if (!QCoreApplicationPrivate::eventDispatcher->parent()) + if (!QCoreApplicationPrivate::eventDispatcher->parent()) { QCoreApplicationPrivate::eventDispatcher->moveToThread(d->threadData->thread); + QCoreApplicationPrivate::eventDispatcher->setParent(this); + } d->threadData->eventDispatcher = QCoreApplicationPrivate::eventDispatcher; @@ -2517,6 +2519,31 @@ bool QCoreApplication::hasPendingEvents() return false; } +/*! + Returns a pointer to the event dispatcher object for the main thread. If no + event dispatcher exists for the thread, this function returns 0. +*/ +QAbstractEventDispatcher *QCoreApplication::eventDispatcher() +{ + if (QCoreApplicationPrivate::theMainThread) + return QCoreApplicationPrivate::theMainThread->eventDispatcher(); + return 0; +} + +/*! + Sets the event dispatcher for the main thread to \a eventDispatcher. This + is only possible as long as there is no event dispatcher installed yet. That + is, before QCoreApplication has been instantiated. This method takes + ownership of the object. +*/ +void QCoreApplication::setEventDispatcher(QAbstractEventDispatcher *eventDispatcher) +{ + QThread *mainThread = QCoreApplicationPrivate::theMainThread; + if (!mainThread) + mainThread = QThread::currentThread(); // will also setup theMainThread + mainThread->setEventDispatcher(eventDispatcher); +} + /* \fn void QCoreApplication::watchUnixSignal(int signal, bool watch) \internal diff --git a/src/corelib/kernel/qcoreapplication.h b/src/corelib/kernel/qcoreapplication.h index ce3f349844..4d3ee9bc7a 100644 --- a/src/corelib/kernel/qcoreapplication.h +++ b/src/corelib/kernel/qcoreapplication.h @@ -61,6 +61,7 @@ class QTextCodec; class QTranslator; class QPostEventList; class QStringList; +class QAbstractEventDispatcher; #define qApp QCoreApplication::instance() @@ -114,6 +115,8 @@ public: static void removePostedEvents(QObject *receiver); static void removePostedEvents(QObject *receiver, int eventType); static bool hasPendingEvents(); + static QAbstractEventDispatcher *eventDispatcher(); + static void setEventDispatcher(QAbstractEventDispatcher *eventDispatcher); virtual bool notify(QObject *, QEvent *); |