Multithreading a qglwidget
-
I am trying to render into an opengl context from a worker thread. Right now I am trying get this example: "writingmultithreadedglapplications":http://doc.qt.digia.com/qq/qq06-glimpsing.html#writingmultithreadedglapplications to work with Qt 5. This example was updated to work in Qt 4.7 (and posted "here":https://github.com/aewallin/sandbox/tree/master/qt_opengl_threads).
I have tested the updated version with Qt 4.8.4 and it works there also. However, with Qt 5 it crashes on the first makeCurrent() call in the worker thread.
Does anyone know what changed?
The full code for may problem can be found at the github link, but the most relevant portions are (probably):
@class GLWidget : public QGLWidget {
Q_OBJECT
public:
GLWidget(QWidget *parent): QGLWidget(parent), glt(this)
{
setAutoBufferSwap(true);
resize(320, 240);
doneCurrent();
}
void startRendering(){
glt.start();
}
void stopRendering(){
glt.stop();
glt.wait();
}
protected:void resizeEvent(QResizeEvent *evt){ glt.resizeViewport(evt->size()); } void paintEvent(QPaintEvent *){ // Handled by the GLThread. doneCurrent(); } void closeEvent(QCloseEvent *evt){ stopRendering(); QGLWidget::closeEvent(evt); } GLThread glt;
};@
and
@void GLThread::run(){
qDebug() << id << ":run..";glw->makeCurrent(); // << "Crashes here" glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // This Will Clear The Background Color To Black glClearDepth(1.0); // Enables Clearing Of The Depth Buffer glDepthFunc(GL_LESS); // The Type Of Depth Test To Do glEnable(GL_DEPTH_TEST); // Enables Depth Testing glShadeModel(GL_SMOOTH); // Enables Smooth Color Shading glMatrixMode(GL_PROJECTION); glLoadIdentity(); // Reset The Projection Matrix gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f); // Calculate The Aspect Ratio Of The Window glMatrixMode(GL_MODELVIEW); while (doRendering) { rotAngle = rotAngle + (id+1)*3; // threads rotate pyramid at different rate! qDebug() << "time=" << QTime::currentTime().msec() << " thread=" << id << ":rendering..."; if (doResize) { glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f,(GLfloat)w/(GLfloat)h,0.1f,100.0f); glMatrixMode(GL_MODELVIEW); doResize = false; } // Rendering code goes here glDrawTriangle(); glw->updateGL(); msleep(40); }
}@
Any help would be greatly appreciated.
-
I'm having the same problem. Anyone ?
The error message when trying to call makeCurrent for a different thread is:
Cannot make QOpenGLContext current in a different thread -
There is an example that demonstrates how to do this in the latest version of QT. The example is called hellowindow.