PDA

View Full Version : Advice needed regarding GUI and external thread cooperation



high_flyer
16th June 2009, 14:49
Hi,

I need some advice regarding the following problem:
I have a DLL that delivers images from a camera.
This is being done in an external thread, and my application is being notified by a callback the image acquisition thread is calling.

In that callback I have the ability to retrieve the new image buffer.
And at that point I am facing my problem:
If (while in the callback) I try to update my GUI (by using QLabel::setPixmap() with the new image, I get the debug message:
"QPixmap: It is not safe to use pixmaps outside the GUI thread"
which makes perfect sense, since indeed I am not in the GUI thread, but in the callback, which is in the image acquisition thread context.

But how then, can I let my application know when it should refresh it self with the new image , from an external thread?

Thanks in advance!

high_flyer
16th June 2009, 14:58
I have found one solution:
Looping in my GUI thread, and check for a flag set by the external thread.
It works.

But I wonder if there is a nicer solution?

Thanks.

^NyAw^
16th June 2009, 15:39
Hi,

Your thread grabs an image and there you can emit a signal to the main thread to let it display the image. Here you can emit a copy of image to ensure that there is no problem regarding memory cleanup when a new image arrives.

wysota
17th June 2009, 00:10
Hi Daniel, how is Mars? Still the "Red Planet"? :D

As for your question - I would post an event to an object living in the main thread with the event object carrying a QImage acquired from the external source. Then you'll be able to transform it to QPixmap if required.