[Qt-interest] beginNativePainting

Samuel Rødal sroedal at trolltech.com
Thu Dec 10 18:56:25 CET 2009


John Clayton wrote:
> Thanks - I guess the next question is then, 'when' should it be called? 
> 
> By the time my paint() method for a given QGraphicsItem is being called 
> - the model and projection matrix has been set up (right?) - and the 
> object simply needs to set clipping and render - so if within the 
> QGraphicsItem::paint() method I then call beginNativePainting(), doesn't 
> that break the QGraphicsView's state setup?

The GL 2 paint engine doesn't use the model and projection matrices 
internally, instead it uses shader programs where the matrix is set as a 
shader uniform. Calling beginNativePainting() however ensures that the 
model and projection matrices are set to the values you would expect in 
order to do raw OpenGL (1.x) painting while honoring the painter's matrix.

> So,a shorter way to ask the same question - is this code going to fly; 
> or is a a 'bad thing' (tm).
> 
> It's the code in my QGraphicsItem derived base class - and is 
> responsible for setting up the clipping, then drawing the content, then 
> drawing the [potentially] curved border around *any* object in the scene
> 
> void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, 
> QWidget *widget) {
> painter->beginNativePainting();
> setupClipping(painter, option, widget);
> paintContent(painter, option, widget);
> paintBorder(painter, option, widget);
> painter->endNativePainting();
> }

This looks like it should work, as long as the various functions don't 
use painter state changing or drawing commands. Using painter commands 
inside a begin/endNativePainting block is not supported, as the whole 
point of beginNativePainting is to reset the paint engine's GL state to 
the defaults (except for the modelview and projection matrices which can 
still be useful) so that the user who wants to issue raw OpenGL commands 
can do so.

--
Samuel



More information about the Qt-interest-old mailing list