[Qt-interest] Xor'ing in paint event

Ian Thomson Ian.Thomson at iongeo.com
Wed Aug 26 15:49:35 CEST 2009


Hi

This causes a stack overflow because calling the paintEvent is virtual, 
meaning that calling it on your 'this' pointer will call 
derived->paintEvent, even if you cast your this pointer to a Base type. 
So you're calling the function recursively forever, overflowing the stack.

A better solution would be to add a function to the base class called 
'draw' which takes a QPainter*. In the base class paintEvent, just make 
a QPainter in the standard way, and call the draw function. In the 
derived class, open a QPainter on an appropriately sized QImage, and 
then pass that along to the draw function.

Cheers,
Ian.

Ender EREL wrote:
> Hi Folks,
> 
> I have a Base class and a Derived class. Both have to do some painting 
> in their paint events.
> 
> In the derived class, i want my drawings to be xor'ed with the drawings 
> of the base class.
> 
> Since the documentation clearly says "Warning: You can only set the 
> composition mode for QPainter objects that operates on a QImage.", I 
> tried a workaround.
> 
> I tried grabbing the drawings of my base widget using 
> QPixmap::grabWidget((Base*)this), then convert the result to a QImage, 
> then construct a QPainter with the image and 
> setCompositionMode(CompositionMode_Xor), and then do my drawings onto 
> that image and finally draw this image to the widget. Since just 
> explaining that method took this long, i don't think it is an elegant 
> solution. Also it doesn't work because QPixmap::grabWidget((Base*)this) 
> causes a stack overflow.
> 
> Anyone have any ideas on how to achieve this? Or any leads on where to 
> start?
> 
> Thanks in advance.
> 




More information about the Qt-interest-old mailing list