Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. Multithreading a qglwidget
Forum Updated to NodeBB v4.3 + New Features

Multithreading a qglwidget

Scheduled Pinned Locked Moved Installation and Deployment
3 Posts 2 Posters 3.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    ezerek
    wrote on last edited by
    #1

    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=" &lt;&lt; QTime::currentTime().msec() &lt;&lt; " thread=" &lt;&lt; id &lt;&lt; ":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.

    1 Reply Last reply
    0
    • D Offline
      D Offline
      divide
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • E Offline
        E Offline
        ezerek
        wrote on last edited by
        #3

        There is an example that demonstrates how to do this in the latest version of QT. The example is called hellowindow.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved