Calling QCoreApplication::hasPendingEvents()
or QAbstractEventDispatcher::instance()->hasPendingEvents()
inside of a thread works just fine. However, outside of it, the latter one (with appropriate parameter) always returns false
(former cannot be used outside, because it refers to the thread from which it is called).
Here is a complete code:
#include <QCoreApplication>
#include <QAbstractEventDispatcher>
#include <QThread>
#include <QDebug>
bool hasPendingEvents(QThread *thread = 0) {
return QAbstractEventDispatcher::instance(thread)->hasPendingEvents();
}
class MyObject: public QObject {
Q_OBJECT
public slots:
void Run() {
qDebug() << __LINE__ << hasPendingEvents() << QCoreApplication::hasPendingEvents();
QThread::sleep(1);
}
};
int main(int argc, char *argv[]) {
QCoreApplication app(argc, argv);
QThread thread;
MyObject t;
t.moveToThread(&thread);
thread.start();
for (int i = 0; i<4; ++i) QMetaObject::invokeMethod(&t, "Run", Qt::QueuedConnection);
for (int i = 0; i<10; ++i) {
QThread::msleep(500);
qDebug() << __LINE__ << hasPendingEvents(&thread) << hasPendingEvents(t.thread());
}
return 0;
}
#include "main.moc"
Here is the output:
15 true true
31 false false
31 false false
15 true true
31 false false
31 false false
15 true true
31 false false
31 false false
15 false false
31 false false
31 false false
31 false false
31 false false
Why doesn't QAbstractEventDispatcher.hasPendingEvents()
work outside of a thread? Maybe there is an alternative?