Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Marshal calls to Qt main thread
dreijer last edited by
I'd like to wrap libcommuni, which uses Qt, in a different DLL project, which doesn't use Qt. As far as I've been able to tell, I need to run the Qt message pump (via QCoreApplication) to make sure networking, signals, etc. work properly. However, I'm running in to some problems figuring out how to do just that.
Basically, I want to spin up a QThread in the DLL, which calls QCoreApplication::exec() and pumps all the Qt events. I then want to marshal external calls to the DLL, which are on a different thread, to the Qt main thread, so I can safely use libcommuni.
It looks like the recommended approach is to use signals and slots for this, but I've been unable to get that to work. I create a signal on the QObject class that is called via the DLL and I connect it to a slot on the QThread that runs the Qt message pump. However, if I specify Qt::QueuedConnection when connecting the signal and slot, the message is never delivered when I emit the signal. If I omit Qt::QueuedConnection altogether, the slot is called immediately on the calling thread rather than the Qt main thread.
I've also tried explicitly calling QCoreApplication::postEvent() on the DLL thread to send an event to the Qt main thread, but event(QEvent) is never called in the target QThread.
Any ideas on what I'm doing wrong here? I'm thinking the issue might be that the signal is emitted from a non-QThread perhaps?
bodzio131 last edited by
I'm not sure but you can connect only QObjects created in QThread with event loop (thread of creation is important). Have you created mentioned QObject in QThread or DLL thread?
There is probably similar dependency between postEvent and object which is calling this method: try to create QObject in your QThread and then use it to post event into QCoreApplication.
Hope it helps.