[Interest] How to share a native OpenGL context with Qt?
marc.gilleron at gmail.com
Thu Jun 18 18:11:18 CEST 2015
Actually, here is what we have:
Our application has a main thread loop.
It loads Qt dynamically (because we don't necessarily need it at compile
time). Qt doesn't wraps our app.
At some point in our main thread, we create a QApplication and widgets.
Then, as we did before Qt integration, we load our OpenGL renderer. It
creates its own context, may load resources at any time, and currently
draws in its own non-Qt window.
But, the simple fact we now have Qt Windows and non-Qt windows with OpenGL
at the same time produces glitches in our renderer, and we're trying to
(We do this to keep them independant from each other. In the future we may
wrap our renderer window in a QWindow but at the moment we try to make it
Here is the kind of code I used to perform sharing, just after we create
our custom OpenGL context:
// Get the main Qt context
QOpenGLContext * qtContext = qt_gl_global_share_context();
if (qtContext == 0)
// Qt: Global OpenGL context is not available
// We get our renderer's OpenGL context
HGLRC hOurContext = /* get from somewhere */
HWND hOurWindow = /* get from somewhere */
QWGLNativeContext ourNativeContext(hOurContext, hOurWindow);
QOpenGLContext * ourContext = new QOpenGLContext;
However I didn't noticed any difference. Or is this the wrong way to share?
Or maybe there is one more problem:
we are still single-threaded, so we need to make sure the current OpenGL is
the good one when we perform our calls.
I said we cannot use the the approaches needing paintGL() and the like,
because we would be outside of our main loop, and would lead to
I also wonder if we can put the Qt main loop in ours, because at the moment
we have no clue when it updates...
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Interest