Qt add function call to event loop from another thread

I stumbled upon a problem that I cannot solve in an elegant way right now. Situation: I have a callback function called from outside my application. The callback function needs to update some gui object. Since I cannot call (for example) repaint()

from another thread, I need to find a way to add a function call to the main event loop so that the task will run 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 answer that no such Method "Object::functionName"

. (which is obviously a lie!)

I also read about connecting a signal to a slot that will be called from the event loop by setting the connection type to Qt::QueuedConnection

. However, the usage QOjbect.:connect()

won't work as I don't know which object should receive the signal. Nice will be something like

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

+3


source to share


1 answer


QMetaObject::invokeMethod

usually used for this kind of situation. Make sure that:



  • object

    is a QObject subclass with a Q_OBJECT macro at the top
  • functionName

    declared in the slots section or has a Q_INVOKABLE macro
+2


source







All Articles