Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-36611

QAbstractEventDispatcher::instance(thread)->hasPendingEvents() always returns false

    XMLWordPrintable

Details

    • Bug
    • Resolution: Done
    • P4: Low
    • 5.4.0 Beta
    • 5.2.0
    • Core: Event loop
    • 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
      

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            thiago Thiago Macieira
            apolotsk Alex Polotsk
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes