View Full Version : QGraphicsView over existing QGLWidget

30th August 2012, 17:47
I have an object which extends QGLWidgets and renders a 3d-scene.

I need to add some buttons to the 3D view and some animation effects, like button opacity change.
I found out that a simple way to do that would be render the 3D scene using a QGraphicsView as explained in this example

The problem is that they don't use an already existing QGLWidget.
They do their GL-rendering from QGraphicsView::drawBackground(...).

Is there any way to render the QGraphicsScene scene using the QGLWidget rendering functions?
I tried to call paintGL from the drawBackground function, but it is not working.
This is the code

//This is how the viewport is initialized
QGraphicsView *oglGraphicsView = new QGraphicsView();
oglGraphicsView->setViewport(new MyQGLWidget(QGLFormat(QGL::SampleBuffers)));
oglGraphicsView->setViewportUpdateMode(QGraphicsView::FullViewportU pdate);
oglGraphicsView->setScene(new MyGraphicsScene());

// implementation of the drawBackground
void MyGraphicsScene::drawBackground(QPainter *painter, const QRectF &rect)

glClearColor( 0.0, 0.0, 0.0, 1.0 );
QList<QGraphicsView *> viewsList = views();
if( viewsList.isEmpty() )
MyQGLWidget *myGlViewport = static_cast<MyQGLWidget *>(viewsList[0]->viewport());
// paint using MyQGLWidget rendering function
myGlViewport ->paintGL();


I know that the correct way to do it is to move the gl rendering in the drawbackground function, but to do that I will lose all high level rendering functions provided by QGlWidget.

I would be very glad if you could give me some more hints.

I'm using Qt 4.7