문제

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.

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top