Nominate our 2022 Qt Champions!

Creating a native OpenGL context with QOpenGLContext on a QWidget.

  • Hello,

    first of all, i'm very happy to see that Qt 5 and especially 5.1.0 starts to allow more and more native OpenGL with all those QOpenGL classes.

    My current problem: I try to provide a QWidget, that can support multiple OpenGL contexts for different APIs (plain, OSG, glut, Qt, etc.). I therefore use QSurface and the QSurfaceFormat for format description and wanted to use QOpenGLContext for native context creation. To provide a QSurface (which is abstract) instance, i have a Surface implmentation that returns the required settings. Only for the surfaceHandle i return nullptr, since the required type QPlatformSurface is not provided by the qt api.

    @QPlatformSurface * Surface::surfaceHandle() const
    return nullptr;

    But with that the following happens on context creation:
    @ // src\gui\kernel\qopenglcontext.cpp
    bool QOpenGLContext::makeCurrent(QSurface *surface)
    if (!surface->surfaceHandle())
    return false;

    My question: Is there a way to provide a valid surfaceHandle? or is there a way to use QOpenGLContext in combination with other Surfaces than offscreen and QWindow (i.e. QWidget)?

    Alternatives?: I think about using QGLWidget again (which i initially discarded in favor for using the newer QOpenGL classes). Or one could use the offscreen surface and blit the target fbo.

  • I also thought about embedding a QWindow inside a QWidget. But that seems to be far from its intended use cases.

  • I just found that:

    bq. ... internally QGLWidget creates a QGLContext, and that is in turn implemented in terms of a QOpenGLContext from QtGui. ...

    further more, there is this function:
    QOpenGLContext * QGLContext::contextHandle() const

    So i think in the case of the Qt/Native context one could use a QGLWidget instead of the QWidget, then create a QGLContext, that could return the QOpenGLContext which then can be used for further OpenGL rendering.

Log in to reply