[Qt-interest] QPainter: It is not safe to use drawPixmap() outside the GUI thread

Andre Somers andre at familiesomers.nl
Thu Oct 29 08:27:39 CET 2009


Serge wrote:
> Thank you for all answers.
>
> I want to understand, why i can not use QPixmap from the other thread.
>
> Ander wrote: "Yes. Don't do GUI painting outside the GUI thread. Ever."
>
> Can you please explain or give some links, what is GUI painting? What is 
> the difference between GUI painting and non-GUI painting? Why when i use 
> QPixmap i use GUI painting?
>   
First of all: please search the mailinglist archives. This topic is 
discussed frequently.
A short answer is that a pixmap is* a datastructure that may reside 
outside the program memory, and it being manipulated through some 
graphics interface. It may, for instance, reside on the videocard where 
drawing operations to it are accelerated. Access to such resources must 
be serialized, otherwise you get undefined results. Threading makes it 
impossible to do that. All GUI elements are in the end painted on such 
pixmap surfaces, so you can not paint to these resources from any thread 
but the main thread.

There are of course much more detailed, nuanced and more correct 
explanations available, but this should give you a rough idea.

I was talking about GUI painting when I perhaps should have said pixmap 
painting. If you use a QImage to draw on instead of a QPixmap, you are 
sure to work with a resource that belongs to your own application that 
you have full control over. You *can* paint to this in a thread! A 
frequent method of using threaded rendering is to paint to such QImages 
in threads, send them to the GUI thread when they are done, and have the 
GUI thread paint those images to it's UI.

André





More information about the Qt-interest-old mailing list