开发者

What is the best practice for communicating with another thread in Qt?

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 mai开发者_Python百科n 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜