Search code examples
c++multithreadingqtqapplication

Qt - run function on other thread


In my simple QApplication I have this scenario:

Class1 called from main thread.

Class2 called from other thread that contains a reference of Class1.

Is possible call Class1 function from Class2 using main thread?

I have tried with moveToThread without success. Thanks in advice, best regards.

Daniele


Solution

  • The easiest way is probably to make the call through a signal to slot connection betweeen the to objects. Specify Qt::QueuedConnection or Qt::BlockingQueuedConnection as the connection type when making the connection. This type of connection will queue the signal on the thread of the receiving object.

    For details see ConnectionType and connect