[Qt-interest] QPainter, Clipping and Opacity

Freddie Witherden freddie at witherden.org
Thu Aug 6 20:23:30 CEST 2009


Hi all,

I am having great trouble solving a problem that is conceptually  
simple. It can be simplified as follows:
  - Draw a line from (-10, 0) -> (10, 0); fill it black.
  - Draw an ellipse at (0, 0) with an rx,ry of (3, 3); fill it a solid  
colour, say red.

Now QGraphicsItem::setOpacity works by setting the painter opacity;  
hence the black line is visible under the circle when the opacity < 1.  
This is undesirable (users aren't meant to know the line is  
underneath!).

I first tried drawing the ellipse first, then setting the composition  
mode to DestinationOver and then drawing the line. The line was not  
visible.

Next, I tried creating a clipping path consisting of the ellipse;  
subtracted it from my items bounding rectangle and then used that as  
the painters clipping. It worked (albeit with a performance hit) on OS  
X with the normal painter. However, with the OpenGL painter it did not  
work (Intel X3100).

In OpenGL this problem would be solved by using the following construct:
glBlendFunc(GL_CONSTANT_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glBlendColor(1.0f, 1.0f, 1.0f, objectAlpha);

Sadly Qt does not have a similar construct and the (unfortunate) way  
in which ::setOpacity works makes it of little use.

Currently my best idea is to paint everything to a QImage, then draw  
that to the GraphicsItem. Does anyone know of any better ways of doing  
this?

Regards, Freddie.



More information about the Qt-interest-old mailing list