How can I invoke functions on QObjects from another thread?

来源:互联网 发布:moto2008概预算软件 编辑:程序博客网 时间:2024/04/28 11:55

 How to call 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 [doc.qt.nokia.com].
  • 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 [doc.qt.nokia.com]to the connect statement or use Qt::AutoConnection, the default, which decides at runtime how the slot should be called. See the documentation [doc.qt.nokia.com]
  • Another way is using QMetaObject::invokeMethod()