View Full Version : QGLWidget & QGLContext & QThread

1st April 2011, 15:01

I have written a class MyGLWidget which is inherited from QGLWidget. It has all the mouse handling functions for rotating/zooming/panning.

I want to paint on this MyGLWidget from two different places in the program (by not using function pointers for paintGL function). I do not have idea that how can I use QGLContext and makeCurrent stuff?

Also, how can I create QGLContext with QThread if I want to use QThread for drawing from two different places? How can I share a QGLContext?

I studied the QThread and QGLWidget example on article http://doc.trolltech.com/qq/qq06-glimpsing.html and seems fine to me. But how can I share QGLContext from two different threads?

Can anybody please throw some light on this?
Thank you.


2nd April 2011, 16:17

I would recommend against sharing a glcontext between different threads! You can't really do anything in parallel. The context has to be blocked and released by each thread.

See: http://www.equalizergraphics.com/documentation/parallelOpenGLFAQ.html

Try to change your design in such a way, that your background-threads pre-calculate everything possible (e.g. textures etc) and make the actual calls to the glContext from just one thread.

You can use signals and slots to communicate between the worker objects of your threads.


3rd April 2011, 11:38
Hi Johannes,

Thank you very much for your ideas on my question.

I agree with you. As you said, one thread should pre-compile all the required drawings and another thread should draw those heavy GL drawings. The reason I was asking to share a glcontext was that my application may require huge visualization data to draw which may not be feasible on a single core master node.

Also, can you please elaborate on how can I create a glcontext for each thread and use them? In my application, actually I wanted to draw different things (grid or contours) on a single glwidget. But now if I use two threads and create two glcontext for each, I need to create two glwidget windows (?)

Also, can you provide a small example (or suggest a link) which uses qglwidget and qthread using signal and slot mechanism?

Thanks for taking out time for me.


3rd April 2011, 13:26

How to use multiple glWidgets and contexts and update each from their own thread is described perfectly well in the link you first posted!

As to the signal slot mechanism, it doesn't matter if its a qglwidget or something else... The general idea is the following:

// Just wrote this down out of my head ... typos included.
class Worker : public QObject
void resultReady(Data* d)
Worker() ..
public slots:
void calculate()
emit resultReady(d)

Worker* w = new Worker();
QThread* thread = new QThread();
connect(w,SIGNAL(resultReady(Data*)),this,SLOT(sho wResult(Data*)));
// To start the worker you need to invoke the method through the event
// loop of the threads its living in
The this object in the above code, could be your subclassed QGLWidget. I don't know enough about your situation. How do you toggle a redraw? You would need to invoke the calculate method from there... And make sure that the Data you pass along is not overwritten by the Worker inadvertently. Either create new data variable every time or if that is to expensive make it doublebuffered and protect each halfbuffer with a mutex, which should virtually never block because the drawing should be much faster then the calculation. What you gain by this approach is that you can have a multitude of worker threads preparing stuff for the glwidget to draw.