tags:

views:

50

answers:

3

Hi All,

I have my own application in QT.It has one main GUI thread which will handling the event from the inputs but i have created one thread that will applicable to change images every 10 seconds (just like slideshow or screen saver). but when i call function setPixmap from thread then it gives me warning that it's not safe to use Pixmap from thread.

what is the solution ?. why i don't use setPixmap from thread ?

Thanks, Neel

+2  A: 

The why, is because that function is not thread safe.

The solution, is to use a QTimer to run your function every 10 seconds. QTimer is integrated in the Qt Event Loop, so you don't need another thread to do it.

Matias Valdenegro
+1  A: 

I don't have an actual answer to this but I know that setPixmap() should only be called from the main GUI thread. I found this mailing list post from a few years back that also points to trolltechs docs. Reading through the thing quickly leads me to think that it has something to do with how different platforms render stuff and so on.

http://lists.trolltech.com/qt-interest/2008-11/thread00534-0.html

http://doc.trolltech.com/4.4/threads.html#painting-in-threads

Matti
This is the cause of the problem. QPixmaps must be handled within the main thread. Widgets should also only be manipulated by the main thread.
andref
A: 

Instead of having your worker thread call setPixmap(), have it emit a signal (something like newImagesReady()).

Then, connect that signal to your widget's update() slot. (Or make your own slot if you want to do more than refresh the widget).

This technique lets you cross thread boundaries.

Tom Sirgedas