[Qt-interest] QGLWidget and threads

Simon Drouin drouin.simon at gmail.com
Mon Sep 20 15:49:26 CEST 2010


I want to trigger a repaint of a QGLWidget from a working thread (the paint
itself should occur in the main thread).

I was hoping the updateGL function would be smart enough to post an event
when it is being called from a thread that is different from the main
thread. It doesn't seem to be the case. Seems like updateGL can't be called
from other threads. Is this right?

Assuming I can't use updateGL, I implemented a replacement in my QGLWidget
derived class (MyGLWidget):

void MyGLWidget::RequestRedraw()
{
    // If we are in the main thread, repaint now
    if( QApplication::instance()->thread() == QThread::currentThread() )
    {
        updateGL();
    }
    // Oterwise, post a repaint event to tell the widget to redraw
    else
    {
        QPaintEvent * e = new QPaintEvent( rect() );
        QCoreApplication::postEvent( this, e );
    }
}

Seems to be working on OSX. Can anyone see a problem with this code? Is it
portable?

s.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20100920/95992d86/attachment.html 


More information about the Qt-interest-old mailing list