PDA

View Full Version : update widget from separate thread



method
9th July 2009, 15:34
hello, i am new to QT and i am having some difficulties getting a widget to update as desired.

i have a separate thread that acquires images from a camera, and i would like to display a live feed from the camera in my gui.

i am calling widget->update after a new image has been acquired, but the widget does not repaint.

calling repaint or some other more direct method to repaint the widget fails a qt thread-safety assertion.

can someone please point me in the right direction?

thanks!

wysota
9th July 2009, 20:16
You can't access methods from widgets from worker threads, it will eventually lead to a crash. You need to pass the data to the main thread (using events or signals) and update the widget there.

method
9th July 2009, 20:33
hi wysota, thanks for replying.

i understand that is the mechanism i need to use, but i am a little unclear of the details of how to go about emitting the signal in this instance.

a good strategy for my application seems to be to create a function in the main GUI class that will then emit the proper signal, but i'm having trouble locating the section of the documentation that demonstrates the proper use of the emit keyword.

method
9th July 2009, 21:01
i shoudl clarify, i can't find anything that demonstrates the use of signals and slots across different threads.


if i call the function:



void myWidget::emitUpdate()
{
emit update();
}


from my worker thread, nothing happens.

if i emit repaint() insteand, i fail a thread-safety assertion.

my current strategy is to overload paintEvent and recursively call update(). this gives me very fluid video, but it is very wasteful of CPU; i'd like to update only when i have new frames available.

wysota
9th July 2009, 22:38
Connect a signal to update() (which is a slot not a signal so don't emit it). It will always be called in the context of the main thread (regardless where you emit a signal connected to it). The rest is just transferring data there so that update has anything to do.

method
10th July 2009, 14:33
got it, thanks! :)