How to move to thread opengl widget that was promoted in QtDesigner?
-
Hi,
I have two different widgets (for example aGLWidget and bGLWidget). They inherit QOpenGLWidget. I also created QWidget (myGLWindow) that contains QTabWidget. There is two tabs, each contain one widget promoted to one of my QOpenGLWidget. I need to move that section to different thread, because I have also view from camera in other window. So it should works asynchronously.
I know that I can't move myGLWindow because it is GUI object created by the main window. So please tell how can I run that two QOpenGLWidget in other thread if I create them by promoting? -
@never_ever
Hello,
Promoting has nothing to do with the issue. Widgets (QOpenGLWidget
included) are not reentrant, so you can't, or rather mustn't, move them into a thread different from the main.Kind regards.
-
Ok, I read more about QOpenGLWdget and I found that I can move context to the thread, but I have to also reimplement paintEvent to do nothing.
So I think I should do this:QThread* glThread = new QThread(); aGLWidget->context()->moveToThread(glThread); bGLWidget->context()->moveToThread(glThread);
And then, after thread is done rendering I should update my GUI, but how do I know that rendering is done? If I should cubclass my own thread or there is some signal for this?
What with resizeEvent? If I also should reimplement it to do nothing?
-
@never_ever
Hello,
Only one thread is able to draw onto a context, or rather the surface of a given context, note the documentation's warning about thread affinity. That said,somewhere from the main threadyou should callswapBuffers
to make your changes visible, this entails managing the double-buffering yourself. You should know when rendering is done, since you do that in your worker threads, so you can connect a signal from the worker(s) to the GUI to notify it when you've finished drawing the surface. You don't need to subclass theQThread
class, instead extendQObject
and move an instance of it to the worker thread and use it to do the rendering/other work for you (the so called worker object approach). I'm pretty sure that if you need to resize the window (like handling the resize event) at some point, you'll need to add a mutex to serialize access to your context, so when resizing is done there will be no rendering occurring at the same time from another thread, that you can do relatively simply with a singleQMutex
object. I hope this helps.Kind regards.