Important: Please read the Qt Code of Conduct -

Using `QOpenGLFramebufferObject` with `QGraphicsView`

  • The QOpenGLWidget has a GL framebuffer in which it expects everything to be rendered. This framebuffer is not the system's default framebuffer.

    QOpenGLContext::defaultFramebufferObject() returns the default framebuffer. This function is used in QOpenGLFramebufferObject::release() to rebind the default framebuffer after one is done with using one's own.

    Now, for this to work with QOpenGLWidget it is necessary for defaultFramebufferObject() to return not the system's default, but the framebuffer used by the widget.

    Otherwise calling QOpenGLFramebufferObject::bind() and subsequently release() will leave you rendering into the system's default framebuffer 0 while the widget expects you to render to, say, framebuffer 3.

    Looking at the sources shows that QOpenGLWidget, before calling a virtual paintGL() method, sets a flag on QOpenGLContext to make its defaultFramebufferObject() method return the correct framebuffer. QOpenGLFramebufferObject::bind() and release() inside the paintGL() method do therefore work as expected.

    So far so good. This is also all documented like this.

    We are, however, using QOpenGLWidget as viewport for QGraphicsView. When using QOpenGLFramebufferObject inside QGraphicsEffect::draw() the aformentioned intialization step that modifies QOpenGLContext::defaultFramebufferObject()'s behaviour seems to be missing. A call to QOpenGLFramebufferObject::release() leaves you with the wrong framebuffer bound. We have to manually save the framebuffer id before QOpenGLFramebufferObject::bind() and restore it later.

    Now my question is: Are we misusing QOpenGLFramebufferObject or is there an oversight in QGraphicsView, failing to set the correct default framebuffer before calling any user-overriden draw() methods?

  • Lifetime Qt Champion


    I wouldn't say an oversight but likely you have a use case that has not happened before. Also QOpenGLFramebufferObject is a pretty new class compared to QGraphicsView.

    I'd recommend brining this to the interest mailing list. You'll find there Qt's developers/maintainers.