Question

I have a QThread derived class that communicates with the main thread by sending QEvents to it.

What is the best way for the main thread to communicate with the second thread?

The main thread has a pointer to the second one.

Was it helpful?

Solution

The best way to communicate between objects in Qt is to use signals and slots. It is a thread-safe way that is handled by the event loop and requires no locking on your part. You can also use events, though that use seems a little weird - an event is a notification of something happening, not a tool to chat.

You can also use threading primitives like QMutex, QSemaphore, QWaitCondition and QReadWriteLock (same as a QMutex, but as it's name suggests, allows you to lock for either reading or writing, not both at the same time).

You should read the Qt documentation, specifically I recommend you start with the Thread Support in Qt page.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top