Important: Please read the Qt Code of Conduct -

How to correctly create a new QGLContext for a QGLWidget?

  • Hi there, I'm in the process of upgrading my application to use Qt 5 and come across a stumbling block. Previously I had used this code to give my GL widget a new GL context (the this pointer is the GL widget):

    @// Create a fresh OpenGL context and make it current.
    QGLContext* newContext = new QGLContext(this->format(), this);
    this->setContext(newContext, NULL, true);

    However it doesn't appear to work anymore, instead it makes the GL widget have a grey background and then lots of OpenGL "Invalid Operation" errors whenever glGetError() is run. I've checked the QGLContext::create() and QGLContext::isValid() functions are returning true (they are) so I'm not sure where its going wrong. This method used to work for me in 4.7 and 4.8. If it makes any difference I'm using MSVC2012 and have compiled Qt with the "-opengl desktop" option. OpenGL rendering itself seems to work as I can render it fine before resetting the context, it's only afterwards I can't.

    Is there an alternative way I should explore for achieving this? Thanks in advance. :)

  • Out of curiosity why do you want to replace the context?

  • Hi ZapB! Basically the application I'm making acts as a container which allows the user to select from several plugins containing OpenGL work (demos and tests etc), when the application loads a new plugin it would be preferred to give the plugin a fresh OpenGL context to work with so it isn't affected by any previous state that other plugins may have put it into.

    I realise the correct way to do this is probably to implement a state tracking mechanism that reverses itself when the plugin is unloaded but at the current time that would require a lot of time spent with refactoring which I don't really have. The other "quick" solution I've thought of (if I can't replace the context manually) would be to create the QGLWidget object again, but the last time I tried that approach it looked quite ugly as the user interface reshifted itself noticably when it re-added the widget.

Log in to reply