How can I invoke functions on QObjects from another thread?

There are a two basic methods for calling a function from one thread on a QObject in another thread.

  • The most basic operation is to post an event to the object in the other thread. The event loop in the target objects thread will then deliver the event to the target object. See the documentation [].
  • The second approach is to make use of Qt’s queued connections. This is also implemented in terms of the first method, and works for any target object that has an event loop running in the thread that owns it. One can either specify a queued connection by passing the parameter Qt::QueuedConnection [] to the connect statement or use Qt::AutoConnection, the default, which decides at runtime how the slot should be called. See the documentation []

1 comment

January 28, 2011

Picture of ngocketit ngocketit

Lab Rat

Another way is using QMetaObject::invokeMethod()

Write a comment

Sorry, you must be logged in to post a comment.