[Qt-interest] Qt4.4.3 paintEvent thread-safe

Andreas Pakulat apaku at gmx.de
Tue Dec 23 11:42:49 CET 2008


On 23.12.08 10:57:39, Prasanta Sadhukhan wrote:
> Andreas Pakulat wrote:
> > On 22.12.08 14:43:42, Prasanta Sadhukhan wrote:
> >   
> >> which got solved when I introduced a mutex lock and unlock inside my 
> >> PaintEvent function, like this
> >>
> >> void QtWindow::paintEvent(QPaintEvent *event)
> >> {
> >>  
> >>     AWT_QT_LOCK;
> >>     QPainter p(this);
> >>     QPoint pt(0,0);
> >>     p.drawImage(pt, *QtImageDescPool[0].offscrSurface);
> >>     
> >
> > I guess this array is shared by both threads, the Qt-painting thread and
> > the one that paints onto the offscreen image?
> Yes
> >  If so, thats why you need the
> > lock, which probably causes the slowdown. There are only 2 solutions here:
> >
> > a) provide a completely copy of the offscreen image to the qt-painting
> > thread instead of using a global array (for example by using a custom Qt
> > event that carries the QImage)
> >   
> Can you please elaborate on this solution?
 
Whatever method you use to signal the Qt-painting-thread to update needs to
carry the image as parameter. The receiving method stores it in a local
variable and then when paintEvent is called it can use the image. This way
there's no concurrent access to the image data between the two threads. The
only downside is that you need 2x the memory of the image as its stored on
both ends for a short period of time.

Andreas

-- 
You will be singled out for promotion in your work.



More information about the Qt-interest-old mailing list