Important: Please read the Qt Code of Conduct -

QGraphicsView OpenGL

  • Hi,

    I'm trying to use QGraphicsView to render widgets on top of an OpenGL scene. I followed the instructions in this "article":, but I have some questions about it.

    1.: It is said that I have to restore all OpenGL state if I change it in QGraphicsScene::drawBackground(). Do I really have to restore every state variable which could probably have changed (things like VBO and texture bindings, too?) after each frame, or is it reliable (also in later versions of Qt) to only restore a small set of state variables? If not, what would be the best way to restore the whole state? I don't want to use the glPushAttrib/glPopAttrib commands, as they are deprecated, and I'm using OpenGL >= 2.0 and am trying to avoid deprecated functions. Do I really have to glGet and glEnable/glDisable them all by hand?

    2.: It seems like the widgets (QDialog, QWidget) I'm adding to the scene don't have a title bar, even in the example I compiled from the Qt Quarterly article. Do I have to do something special to activate them, or is this an issue with the OpenGL 2 paint engine?

    Thanks in advance!

  • Hi,

    For 1), Qt 4.6 introduced QPainter::beginNativePainting() and QPainter::endNativePainting(), which will handle restoring any state used by the GL paint engine -- wrapping your raw GL inside these functions should be all you need to do (assuming you can use 4.6 or later).


  • I tried using this, but it seems like there are still some problems. For example, with this very meaningful code snippet, my widgets won't show:

    @void GLGraphicsScene::drawBackground(QPainter* painter, const QRectF& rect)
    if (painter->paintEngine()->type() != QPaintEngine::OpenGL2) {


    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    GLuint buf;
    glGenBuffers(1, &buf);
    glBindBuffer(GL_ARRAY_BUFFER, buf);
    char tdata[4] = {1, 2, 3, 4};
    glBufferData(GL_ARRAY_BUFFER, sizeof(tdata), tdata, GL_STATIC_DRAW);

    //glBindBuffer(GL_ARRAY_BUFFER, 0);


    When I uncomment the glBindBuffer line, everything seems to work. Shouldn't this be automatically done as part of QPainter::endNativePainting(), too? It wouldn't be such a problem to do this at the end of each frame, but if I have to do it manually for buffer bindings, there might be some other surprises popping up, and I don't want to to always update drawBackground() when I'm changing some new state variable.

  • Anything new here? I still have the same two issues in Qt 4.8.0.

Log in to reply