From 2376a5c4e0ea930d43d9377f4e93738a20659097 Mon Sep 17 00:00:00 2001 From: Thiago Macieira Date: Tue, 3 Apr 2012 10:36:32 -0300 Subject: [PATCH] Make the event notification on Windows be mandatory in all dispatchers This way, QWinEventNotifier will work on all Windows systems, not just with the default event dispatcher. Other dispatchers (other than QWin32EventDispatcher) are permitted, so the class should not abort just because of that. If a dispatcher really doesn't want to implement this, they need to implement the virtuals to do nothing, possibly print a warning. Change-Id: I2c132bcde95b9d5941c8906a0fcd2ad964087772 Reviewed-by: Bradley T. Hughes --- src/corelib/kernel/qabstracteventdispatcher.h | 9 +++++++++ src/corelib/kernel/qwineventnotifier.cpp | 12 +++++++----- .../kernel/qcoreapplication/tst_qcoreapplication.cpp | 5 +++++ tests/auto/corelib/thread/qthread/tst_qthread.cpp | 5 +++++ .../tst_benchlibeventcounter.cpp | 5 +++++ .../benchliboptions/tst_benchliboptions.cpp | 5 +++++ 6 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/corelib/kernel/qabstracteventdispatcher.h b/src/corelib/kernel/qabstracteventdispatcher.h index 96498d207da..e70530b2831 100644 --- a/src/corelib/kernel/qabstracteventdispatcher.h +++ b/src/corelib/kernel/qabstracteventdispatcher.h @@ -53,6 +53,10 @@ QT_BEGIN_NAMESPACE class QAbstractEventDispatcherPrivate; class QSocketNotifier; +#ifdef Q_OS_WIN +class QWinEventNotifier; +#endif + class Q_CORE_EXPORT QAbstractEventDispatcher : public QObject { Q_OBJECT @@ -95,6 +99,11 @@ public: virtual int remainingTime(int timerId) = 0; +#ifdef Q_OS_WIN + virtual bool registerEventNotifier(QWinEventNotifier *notifier) = 0; + virtual void unregisterEventNotifier(QWinEventNotifier *notifier) = 0; +#endif + virtual void wakeUp() = 0; virtual void interrupt() = 0; virtual void flush() = 0; diff --git a/src/corelib/kernel/qwineventnotifier.cpp b/src/corelib/kernel/qwineventnotifier.cpp index b48bd0c8cc5..d81d1ec4600 100644 --- a/src/corelib/kernel/qwineventnotifier.cpp +++ b/src/corelib/kernel/qwineventnotifier.cpp @@ -134,10 +134,12 @@ QWinEventNotifier::QWinEventNotifier(HANDLE hEvent, QObject *parent) : QObject(*new QWinEventNotifierPrivate(hEvent, false), parent) { Q_D(QWinEventNotifier); - QEventDispatcherWin32 *eventDispatcher = qobject_cast(d->threadData->eventDispatcher); - Q_ASSERT_X(eventDispatcher, "QWinEventNotifier::QWinEventNotifier()", - "Cannot create a win event notifier without a QEventDispatcherWin32"); - eventDispatcher->registerEventNotifier(this); + QAbstractEventDispatcher *eventDispatcher = d->threadData->eventDispatcher; + if (!eventDispatcher) { + qWarning("QWinEventNotifier: Can only be used with threads started with QThread"); + } else { + eventDispatcher->registerEventNotifier(this); + } d->enabled = true; } @@ -205,7 +207,7 @@ void QWinEventNotifier::setEnabled(bool enable) return; d->enabled = enable; - QEventDispatcherWin32 *eventDispatcher = qobject_cast(d->threadData->eventDispatcher); + QAbstractEventDispatcher *eventDispatcher = d->threadData->eventDispatcher; if (!eventDispatcher) // perhaps application is shutting down return; diff --git a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp index e8f6c29a948..2ac075bebf8 100644 --- a/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp +++ b/tests/auto/corelib/kernel/qcoreapplication/tst_qcoreapplication.cpp @@ -622,6 +622,11 @@ public: void interrupt() {} void flush() {} +#ifdef Q_OS_WIN + bool registerEventNotifier(QWinEventNotifier *) { return false; } + void unregisterEventNotifier(QWinEventNotifier *) { } +#endif + bool visited; }; diff --git a/tests/auto/corelib/thread/qthread/tst_qthread.cpp b/tests/auto/corelib/thread/qthread/tst_qthread.cpp index 41cfc523544..da49f43b7de 100644 --- a/tests/auto/corelib/thread/qthread/tst_qthread.cpp +++ b/tests/auto/corelib/thread/qthread/tst_qthread.cpp @@ -1239,6 +1239,11 @@ public: void interrupt() {} void flush() {} +#ifdef Q_OS_WIN + bool registerEventNotifier(QWinEventNotifier *) { return false; } + void unregisterEventNotifier(QWinEventNotifier *) { } +#endif + bool visited; }; diff --git a/tests/auto/testlib/selftests/benchlibeventcounter/tst_benchlibeventcounter.cpp b/tests/auto/testlib/selftests/benchlibeventcounter/tst_benchlibeventcounter.cpp index 82de6535864..b077291cc62 100644 --- a/tests/auto/testlib/selftests/benchlibeventcounter/tst_benchlibeventcounter.cpp +++ b/tests/auto/testlib/selftests/benchlibeventcounter/tst_benchlibeventcounter.cpp @@ -64,6 +64,11 @@ public: bool unregisterTimers(QObject*) { return false; } int remainingTime(int) { return 0; } void wakeUp() {} + +#ifdef Q_OS_WIN + bool registerEventNotifier(QWinEventNotifier *) { return false; } + void unregisterEventNotifier(QWinEventNotifier *) { } +#endif }; class tst_BenchlibEventCounter: public QObject diff --git a/tests/auto/testlib/selftests/benchliboptions/tst_benchliboptions.cpp b/tests/auto/testlib/selftests/benchliboptions/tst_benchliboptions.cpp index d3be93b7151..7673aa396e5 100644 --- a/tests/auto/testlib/selftests/benchliboptions/tst_benchliboptions.cpp +++ b/tests/auto/testlib/selftests/benchliboptions/tst_benchliboptions.cpp @@ -64,6 +64,11 @@ public: bool unregisterTimers(QObject*) { return false; } int remainingTime(int) { return 0; } void wakeUp() {} + +#ifdef Q_OS_WIN + bool registerEventNotifier(QWinEventNotifier *) { return false; } + void unregisterEventNotifier(QWinEventNotifier *) { } +#endif }; class tst_BenchlibOptions: public QObject