QTimer::singleShot and QMetaMethod::invoke

后端 未结 3 1627
醉梦人生
醉梦人生 2021-01-01 19:26

In some Qt examples, I see they use QTimer::singleShot(0, this , SLOT(funcA())), why not to call the slot funcA directly? also the same question f

相关标签:
3条回答
  • 2021-01-01 19:32

    These methods can also be used to invoke protected and private members of a class (if they are defined as slots) from a scope that would otherwise require public access.

    0 讨论(0)
  • 2021-01-01 19:50

    Every system has a event loop where events are processed. Say like

    application::processEvents()
    {
    // process event list..
    }
    

    Now the place where you write QTimer::singleShot(0, this, SLOT(doSomething())); might be inside some processing event. When this loop is done, processEvents will be called again and in that the doSomething() will be executed.

    So this is like calling doSomething in the next event loop, rather than calling it immediately. Hope you get the idea.

    0 讨论(0)
  • 2021-01-01 19:53

    The following lines are all functionally equivalent:

    QTimer::singleShot(0, object, &Class::funcA); // Qt 5
    QTimer::singleShot(0, object, SLOT(funcA())); // Qt 4
    QMetaObject::invokeMethod(object, "funcA", Qt::QueuedConnection); 
    

    As is now apparent, the intent is to execute the call within the event loop. The queued call results in the posting of an QMetaCallEvent to the object. This event is handled by QObject::event and results in the call of the desired method. Thus, the following are exactly equivalent, even if the latter is a private implementation detail - letting me skip the details of instantiating the event:

    QMetaObject::invokeMethod(object, "funcA", Qt::QueuedConnection);
    QCoreApplication::postEvent(object, new QMetaCallEvent{...});
    

    This comes handy in various situations. For example:

    • To execute some code after all hitherto posted events have been handled.

    • To execute only after the event loop has started.

    • To call an invokable method that's not accessible due to C++ access modifiers. The invokable methods are: signals, slot, and methods declared Q_INVOKABLE.

    • The direct call is unsafe (read: an error!) when a QObject resides in another thread, unless you're explicitly calling a method documented as thread-safe.

    The queued call is a necessity if you wish to ensure that an event loop quits immediately: a direct quit() call is a no-op if the loop is not running yet.

    int main(int argc, char ** argv) {
      QCoreApplication app{argc, argv};
      app.quit(); // this is a no-op since the event loop isn't running yet
      return app.exec(); // will not quit as desired
    }
    
    int main(int argc, char ** argv) {
      QCoreApplication app{argc, argv};
      QMetaObject::invokeMethod(&app, "quit", Qt::QueuedConnection);
      return app.exec(); // will return immediately
    }
    

    Ideally, you'd use postToThread from this answer, it offers the lowest-cost way of calling methods in other threads:

    int main(int argc, char ** argv) {
      QCoreApplication app{argc, argv};
      postToThread([]{ qApp->quit(); });
    }
    

    An alternative way of doing it is using a QObject as a signal source:

    int main(int argc, char ** argv) {
      QCoreApplication app{argc, argv};
      {
        QObject src;
        src.connect(&src, &QObject::destroyed, &app, &QCoreApplication::quit,
                    Qt::QueuedConnection);
      }
      return app.exec(); // will return immediately
    }
    

    Yet another way would be to use a custom event and act in its destructor:

    int main(int argc, char ** argv) {
      QCoreApplication app{argc, argv};
      struct QuitEvent : QEvent {
        QuitEvent() : QEvent(QEvent::None) {}
        ~QuitEvent() { qApp->quit(); }
      };
      QCoreApplication::postEvent(&app, new QuitEvent);
      return app.exec(); // will return immediately
    }
    
    0 讨论(0)
提交回复
热议问题