Hi,

Is it possible to use opengl to draw e.g. an OpenGL texture or a simple triangle inside an QGraphicsItem bounding rect?

I tried following, but the triangle is drawn outside of the bounding rect of the QGraphicsItem.

Qt Code:
  1. class MyItem : public QGraphicsItem
  2. {
  3. public:
  4. MyItem( QGraphicsItem * parent = 0 ) : QGraphicsItem(parent),
  5. w(200), h(200)
  6. {}
  7.  
  8. void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
  9. {
  10. painter->drawRect(boundingRect());
  11. painter->beginNativePainting();
  12.  
  13. glViewport(20, 20, w, h); /* Reset The Current Viewport And Perspective Transformation */
  14. glMatrixMode(GL_PROJECTION);
  15. glLoadIdentity();
  16. gluPerspective(45.0f, (GLfloat)w / (GLfloat)h, 0.1f, 100.0f);
  17. glMatrixMode(GL_MODELVIEW);
  18.  
  19. glLoadIdentity();
  20. glTranslatef(0.0f , 0.0f, -6.0f);
  21. glBegin(GL_TRIANGLES);
  22. glVertex3f(0.0f, 1.0f, 0.0f);
  23. glVertex3f(-1.0f, -1.0f, 0.0f);
  24. glVertex3f(1.0f, -1.0f, 0.0f);
  25. glEnd();
  26.  
  27. painter->endNativePainting();
  28. }
  29.  
  30. QRectF boundingRect () const{
  31. return QRectF(20,20,w,h);
  32. }
  33.  
  34. private:
  35.  
  36. int w, h;
  37. };
To copy to clipboard, switch view to plain text mode