PDA

View Full Version : One GL Context Multiple Widgets - Qt 6



qtoptus
24th November 2021, 16:59
I'm trying to create multiple OpenGL-capable QWidget's but sharing the same GL context. I tried to use QOpenGLWidget but it creates a GL context for every widget.
Is it possible to achieve this using QWidget and Create QOpenGLContext manually and associate it with every created QWidget? Any working examples?

Thanks.

qtoptus
25th November 2021, 15:44
I hope my question is clear otherwise I will assume it's not something doable using Qt. Fair enough! :D

d_stranz
25th November 2021, 16:58
Is it possible to achieve this using QWidget and Create QOpenGLContext manually and associate it with every created QWidget?

QOpenGLContext::setShareContext() won't do what you want?

qtoptus
25th November 2021, 19:17
QOpenGLContext::setShareContext() won't do what you want?

Not really. Once QOpenGLWidget is instantiated, a GL context is created for the widget and cannot be used for subsequent instances of QOpenGLWidget.

d_stranz
26th November 2021, 05:15
If you create the widget, call its method to retrieve the context, then call setShareContext() with that pointer using your common context, that doesn't work?

qtoptus
26th November 2021, 15:25
If you create the widget, call its method to retrieve the context, then call setShareContext() with that pointer using your common context, that doesn't work?

Then the default created context has to be deleted and the shared context is assigned to every new widget, assuming setShareContext changes the widget context. But this sounds like a hack!

d_stranz
26th November 2021, 16:36
But this sounds like a hack!

Don't know; I don't write explicit OpenGL code that much. I think your only option is to look at the implementation of QOpenGLWidget and then derive your own version from QWidget that lets you set the context externally instead of creating it by default. With luck you may be able to reuse a lot of the source code.