[Qt-interest] QGL2PaintEngineEx vc QGLContext

Samuel Rødal sroedal at trolltech.com
Thu Dec 10 14:49:26 CET 2009


John Clayton wrote:
> Hi All
> 
>  From testing - I notice that drawing a texture to my GL surface (on a  
> Mac, using 4.6.0, in a QGraphicsItem paint() method) can be done in  
> two different ways.
> 
> Method 1 - use QPainter->drawImage(QRectF, QImage)
> 
> or
> 
> Method 2 - use QGLWidget->drawTexture(QRectF, int, int)
> 
> Method 1 works perfectly in my case, and looking at its code it is  
> using the new Qt4.6 opengl code.
> Method 2 fails to draw anything (it used to work in 4.5.3), and the  
> code to draw looks *totally* different compared with Method 1.
> 
> So, how can I use Method 1 to draw - given that i have a texture ID.   
> My requirement is to draw textures into a QGraphicsItem, and QGLWidget- 
>  >drawTexture() appears to fit the bill - but it doesn't draw anything  
> (unless I call beginNativePainting first - but that wipes out the  
> clipping path).
> 
> Is there a way I can use the QPainter based method to draw textures?
> 
> Thanks
> --
> John Clayton
> Skype: johncclayton

For the modelview and projection matrices to be set up as you would 
expect you need to call beginNativePainting() in 4.6. How the GL paint 
engine handles clipping is an internal detail, it might use the depth 
buffer, stencil buffer, or a different approach, so we don't want to 
expose that through beginNativePainting(). Instead you should manually 
make sure to paint within the clip path, or ideally simplify your 
graphics item so it only uses a rectangle clip (path clips are more 
expensive in any case).

There _is_ a drawTexture call in QGL2PaintEngineEx (which you can get by 
static_cast<QGL2PaintEngineEx *>(painter->paintEngine()) if the paint 
engine's type function returns QPaintEngine::OpenGL2, but it's internal 
private API, so you shouldn't use it unless you're prepared that it 
might break in any future release. For now QGLWidget::drawTexture() is 
the only supported way to go, but it doesn't regard the current 
painter's state as you've noticed.

We might get some better options for using OpenGL in graphics items in 
the future, potentially through the (currently in research) Qt/3D project:
http://labs.trolltech.com/blogs/2009/11/18/qt3d-brings-qt-style-coding-to-3d/

--
Samuel



More information about the Qt-interest-old mailing list