Important: Please read the Qt Code of Conduct -

Opengl functions outside QWindow

  • I have a QWidget based program with a OpenGL window inherited from QWindow and QOpenGLFunctions_3_0:
    @class OpenGLWindow : public QWindow, protected QOpenGLFunctions_3_0

    Much like the "OpenGL Window Example": I have a class that is subclassed from OpenGLWindow to do my particular OpenGL stuff. Let's call this class glWindow. The glWindow is contained in a QWidget using QWidget::createWindowContainer(glWindow);

    glWindow is getting cluttered and I would like to break out functionality in member classes. For example:
    @class glWindow : public OpenGLWindow
    void render();
    customClass *m_cClass;
    void glWindow::render()

    How can I get the glfunctions to be available in the customClass?

  • A couple of options spring to mind:

    1. Don't inherit and instead get a pointer to the functions object from QOpenGLContext::versionFunctions<QOpenGLFunctions_3_0>(). Pass thsi pointer around.

    2. Just get another pointer to the functions object wherever you need it by first getting the current context and then using the function mentioned above.

    When you call the QOpenGLContext::versionFunctions() function the context retains ownership of the functions object so that it can be cached.

    So something like this will do the job:

    QOpenGLContext *ctx = QOpenGLContext::currentContext();
    m_funcs = ctx->versionFunctions<QOpenGLFunctions_3_0>();

    the last call is a noop if the functions have already been resolved.

    Hope this helps.

  • Ah! QOpenGLContext::currentContext(); Great!

Log in to reply