Multithreading – issue a QT signal from a non QT thread or issue a QT main event loop at 4.5
I call an emit signal1 () from a non QT thread
It is just a pthread (pthread_create()), which calls a signaled QObject method
For example:
MyQbject: public QObject { ... void emitBunchOfSignals() { emit signal1(); emit signal2(); ... } ... }
The "run" method of my pthread has a pointer to the MyObject instance (the instance created in the context of the main QT GUI thread, not pthread) and calls the emitbunchofsignals() method
It was annoying before QT 4.5 Now, does QT 4.5 handle this? Does it call QAPP - > postevent() or something so that the signal is emitted in the QT GUI thread (and therefore the slot)?
thank you
Solution
You need to make sure to use queued connections with threads, because QT cannot automatically identify the object belonging to which thread ("thread affinity" is a term used in the document) When connecting, do the following:
connect(src,SIGNAL(signal-signature),dest,SLOT(slot-signature),Qt::QueuedConnection);
This will cause the signal to be placed in the event loop of the destination and called when its thread is running (i.e. its event loop)