Using `QOpenGLFramebufferObject` with `QGraphicsView`
-
The
QOpenGLWidgethas 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 inQOpenGLFramebufferObject::release()to rebind the default framebuffer after one is done with using one's own.Now, for this to work with
QOpenGLWidgetit is necessary fordefaultFramebufferObject()to return not the system's default, but the framebuffer used by the widget.Otherwise calling
QOpenGLFramebufferObject::bind()and subsequentlyrelease()will leave you rendering into the system's default framebuffer0while the widget expects you to render to, say, framebuffer3.Looking at the sources shows that
QOpenGLWidget, before calling a virtualpaintGL()method, sets a flag onQOpenGLContextto make itsdefaultFramebufferObject()method return the correct framebuffer.QOpenGLFramebufferObject::bind()andrelease()inside thepaintGL()method do therefore work as expected.So far so good. This is also all documented like this.
We are, however, using
QOpenGLWidgetas viewport forQGraphicsView. When usingQOpenGLFramebufferObjectinsideQGraphicsEffect::draw()the aformentioned intialization step that modifiesQOpenGLContext::defaultFramebufferObject()'s behaviour seems to be missing. A call toQOpenGLFramebufferObject::release()leaves you with the wrong framebuffer bound. We have to manually save the framebuffer id beforeQOpenGLFramebufferObject::bind()and restore it later.Now my question is: Are we misusing
QOpenGLFramebufferObjector is there an oversight inQGraphicsView, failing to set the correct default framebuffer before calling any user-overridendraw()methods? -
Hi,
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.