Qt add function call to event loop from other thread

3.2k views Asked by At

I've stumbled across a problem I can't solve on an elegant way right now. The situation: I have a callback function which is called from outside my application. The callback function has to update some gui object.. Since I can't call (for example) repaint() from within another thread, I have to finde some way to add a function call to the main event loop so the task gets executed at some time.

One possible way would be to use this:

QMetaObject::invokeMethod(object, "functionName", Qt::QueuedConnection, Q_ARG(float, value)); 

However, this just gives me the response that no such Method "Object::functionName". (which is obviously a lie!)

I've also read about connecting a signal to a slot which will be called from the event loop by setting the connection type to Qt::QueuedConnection. However, using QOjbect.:connect() won't work since I don't knwo which object the signal needs to get. Nice would be something like

QObject::emit(object, SIGNAL(function(flaot)), arg);

1

There are 1 answers

2
jturcotte On BEST ANSWER

QMetaObject::invokeMethod is usually what you should use in this kind of situation. Make sure that:

  • object is a QObject subclass with the Q_OBJECT macro at the top
  • functionName is either declared in the slots section or has the Q_INVOKABLE macro