[Qt-interest] Segment fault when updating OpenGL QGLWidget from thread ?

Samuel Rødal sroedal at trolltech.com
Wed Jul 1 15:51:48 CEST 2009


Ed Sutton wrote:
> Hi Samuel,
> 
> Thank you for your reply.
> 
>>> How can I synchronize thread access to my GUI window?
>>> 1 - I have a thread receiving video data over the network.
>>>
>>> 2 - When a frame is complete it makes a callback to my QGLWidget derived
>>> class's frame handler  - a static method that gets passed the instance
>>> of the class. 
> 
> 
>> You shouldn't necessarily have to use a QMutex, sending the image data
>> for the frame over as a QVector / QImage using signals and slots might
>> be sufficient.
> 
> It sounds like signals and slots is not only a call-back mechanism, it also synchronizes thread access to the GUI.  Is this correct?

Correct, signals and slots work across threads as well, though you need 
to manually start a QEventLoop in other threads than the GUI thread. You 
can do this by calling QThread::exec() 
(http://doc.trolltech.com/4.5/qthread.html#exec) at the end of your 
QThread::run() implementation.

> How would I implement this?
>  
> 1 - Derive an object from QObject 

Yep, then instantiate this object in the beginning of QThread::run().

> 2 - Connect derived QObject signal to the GUI's slot

Do this in QThread::run() before calling exec(). Qt will use the 
Qt::QueuedConnection connection type to ensure that no direct call is 
made between threads. Thus the signal will be delivered asynchronously.

> 3 - Make QObject receive the callback from my worker thread and emit the signal and synchronization will be handled automatically?

Yep, if you earlier had a loop in QThread::run() receiving the video 
data you need to change this to a timer or connect a slot to 
QIODevice::readyRead() to handle the incoming data.

--
Samuel



More information about the Qt-interest-old mailing list