I created a simple QGraphicsView-inherited class that uses a QGLWidget-inherited object as a viewport.

I want the contents of the QGLWidget to be rendered as the background of the QGraphicsView.

Sounds simple, right?

It works fine as long as I don't add any QGraphicItems to the scene -- the graphic that I have defined in my QGLWidget gets drawn as the background of the QGraphicsView.
However, as soon as I add an item to the scene, the background does not render properly.

Everything works smoothing if my QGLWidget renders in direct mode (e.g. glBegin(), glEnd()). However, if I use vertex arrays or VBOs, I get weird anomalies in my render.

My theory is that QGraphicsView is modifying the OpenGL context somewhere and not resetting its state, such that when I go to render in my QGLWidget, I am in an unexpected state.

Can someone please try the code I have posted here?
It uses the "GLWidget" class from <qt-dir>/examples/opengl/hellogl/qlwiget.* , and that in turns relies on <qt-dir>/examples/opengl/share/qtlogo.*

If you comment out the 'addEllipse' line, you should see the Qt graphic as the background, but if you keep it uncommented, you should see something wonky.

I'm using qt4.6.2 and Fedora11 64-bit on a higher-end NVIDIA card.

Qt Code:
  1. #include <QtGui>
  2. #include "glwidget.h" // from <qt-dir>/examples/opengl/hellogl/glwidget.*
  3.  
  4. // QGraphicsView that uses a GLWidget as its viewport
  5. class GLGraphicsView : public QGraphicsView
  6. {
  7. public:
  8. GLGraphicsView(QGraphicsScene * scene, QWidget * parent = 0 ) : QGraphicsView(scene, parent)
  9. {
  10. mpGLWidg = new GLWidget(NULL);
  11. setViewport(mpGLWidg);
  12. };
  13.  
  14. protected:
  15.  
  16. void drawBackground(QPainter *p, const QRectF &r)
  17. {
  18. mpGLWidg->updateGL();
  19. QGraphicsView::drawBackground(p, r);
  20. };
  21. private:
  22. GLWidget* mpGLWidg; // use GLWidget class from <qt-dir>/examples/opengl/hellogl/glwidget.*
  23. // it depends on <qt-dir>/examples/opengl/shared/qtlogo.*
  24. };
  25.  
  26.  
  27. int main(int argc, char *argv[])
  28. {
  29. QApplication app(argc, argv);
  30.  
  31. GLGraphicsView gView(&scene);
  32.  
  33. // comment this line out, and the logo will appear:
  34. scene.addEllipse(QRectF(10, 20, 30, 40), QPen(Qt::green), QBrush(Qt::SolidPattern));
  35.  
  36. gView.resize(800,600);
  37. gView.show();
  38.  
  39. int ret = app.exec();
  40. return ret;
  41. }
To copy to clipboard, switch view to plain text mode