QGLWidget in another thread? What is the documentation referring to? QGLWidget in another thread? What is the documentation referring to? multithreading multithreading

QGLWidget in another thread? What is the documentation referring to?


Little late on the answer, but widgets need to be moved to a new thread by the thread that created them. In your case something like:

QGLWidget *glWidget=new QGLWidget();QThread *newThread=new QThread();glWidget->doneCurrent();glWidget->context()->moveToThread(newThread);

Here I only move the openGL context to the new thread, this requires that some overrides in QGLWidget are caught and ignore. You will need to create a new derived class from QGLWidget and override the following.

virtual void glInit();virtual void glDraw();virtual void initializeGL();virtual void resizeGL(int width, int height);virtual void paintGL();

This stops the standard UI thread from trying to make the OpenGL context current in the UI thread. Once you override the above you will need an event system to notify the thread that some of the events have happened, mainly resizeGl and paintGL otherwise the widget will not react properly with others around it.

The final part is in the creation of the QGLWidget. One of the parameters in the construct is const QGLWidget * shareWidget:

QGLWidget ( QWidget * parent = 0, const QGLWidget * shareWidget = 0, Qt::WindowFlags f = 0 )QGLWidget ( QGLContext * context, QWidget * parent = 0, const QGLWidget * shareWidget = 0, Qt::WindowFlags f = 0 )QGLWidget ( const QGLFormat & format, QWidget * parent = 0, const QGLWidget * shareWidget = 0, Qt::WindowFlags f = 0 )

You would then create the ui QGLWidget and the threaded GLWidget(derivided from QGLWidget with all of the overrides mentioned above) making sure when creating the thread GLWidget you provide the QGLWidget as a sharedWidget. This will make the 2 opengl contexted shared and allow you to load a texture in one that both can see. The code should look something like this:

QGLWidget *glWidget=new QGLWidget();GLWidget *threadedWidget=new GLWidget(0/*parent*/, glWidget);QThread *newThread=new QThread();threadedWidget->moveToThread(newThread);

As for code I recently wrote a little example program using threaded QGLWidgets mainly for openGL/openCL interop, but it shows using muliple QGLWidgets in draw threads sharing a single context. The code is on github and a description is here http://www.krazer.com/?p=109


I have to say I don't remember exactly how I did this, anyway I don't think the QGLWidget must be moved to another thread, and in fact that is not what the documentation says. It says to make it current: QGLWidget::makeCurrent(). That will make the OpenGL context of that QGLWidget current in the new thread.

However, I'd try with a QGLContext class. You can instantiate a QGLContext and then call QGLContext::create() to share with the one of the QGLWidget. With the QGLContext you can bind the textures.