Details
-
Bug
-
Resolution: Done
-
P4: Low
-
5.2.0
-
None
-
Windows 7 x64
Description
Calling QAbstractEventDispatcher::instance()->hasPendingEvents() inside of a thread works just fine. However, outside of it (with parameter = new _thread), function always returns false.
Here is a complete code:
#include <QApplication> #include <QAbstractEventDispatcher> #include <QThread> #include <QDebug> bool hasPendingEvents(QThread *thread = 0) { return QAbstractEventDispatcher::instance(thread)->hasPendingEvents(); } bool hasPendingEvents2(QThread *thread) { return thread->eventDispatcher()->hasPendingEvents(); } class MyObject: public QObject { Q_OBJECT public slots: void Run() { qDebug() << __LINE__ << hasPendingEvents() << hasPendingEvents2(thread()) << QCoreApplication::hasPendingEvents(); QThread::sleep(2); } }; int main(int argc, char *argv[]) { QApplication app(argc, argv); QThread thread; MyObject object; object.moveToThread(&thread); thread.start(); for (int i = 0; i<2; ++i) QMetaObject::invokeMethod(&object, "Run", Qt::QueuedConnection); /* For some reason, without a little sleep hasPendingEvents2() will crash app because of "read access violation at 0x0". */ QThread::sleep(1); qDebug() << __LINE__ << hasPendingEvents(&thread) << hasPendingEvents2(&thread); return app.exec(); } #include "main.moc"
Output:
20 true true true 39 false false 20 false false false