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 i开发者_高级运维t 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
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.
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
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.
精彩评论