Qt World Summit: Submit your Presentation

[SOLVED] QGLWidget::makeCurrent () is per widget only or per thread?

  • The documentation for this function says:

    void QGLWidget::makeCurrent ()

    Makes this widget the current widget for OpenGL operations, i.e. makes the widget's rendering context the current OpenGL rendering context.

    However in my experience OpenGL contexts are per thread. The question comes up because I have a single threaded application that works fine on Windows but intermittently on Linux.

    I have a QGLWidget derived class that uses the standard overloads (paintGL, etc). I am loading textures outside of the widget but within the same thread (single threaded so of course). Texture display works fine on Windows but I get a white texture on Linux (indicative of missing texture), there are no OpenGL errors occurring on the texture load.

    So, is the OpenGL context defined with makeCurrent() function really only valid in this class? Do I need to re-write so that the texture load is done in this class, with a makeCurrent() at the start of the load? Is there another approach?


  • As far as I know it is per thread, but I guess it never hurts to call makeCurrent(). doneCurrent() may be a different story though.

  • So is QGLWidget creating its own OpenGL context on initialization? Maybe I could try QGLWidget::context () in my texture loading code and then set the OpenGL context like this:

    @QGLContext *oglc = MyOGLwidget->context ();

    Although that would seem to be redundant since once the OpenGL context is set for a thread its not supposed to change.

  • ^^^ well, i see that MyOGLwidget->context () is a const pointer return and makeCurrent() is not a const member, so that doesn't work.

  • Well, I did fix the problem. I noticed that before QGLWidget::initializeGL() that the opengl context outside the widget and within are the same. However, after this code runs, the context comes back 0 outside of the widget. The odd this is that even though this value is the same before the widget call, the texture loaded under that context doesn't work in the widget, for Linux.

    I couldn't see anyway to get the GLuint context value from the widget for use outside the widget, so I had to rearrange my code so that texture loads are done by the widget, with a makeCurrent() call in the method. This now works on both Windows and Linux.

    I am not at all sure why this happens as generally OpenGL contexts are per thread, but there is something happening that prevents correct external contexts after initializeGL().

  • Good to see you fixed it. I do wonder why the context doesn't seem to be thread specific, though.

Log in to reply