Hi!
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.
{ Q_OBJECT
signals:
void resultReady(Data* d)
public:
Worker() ..
public slots:
void calculate()
{
emit resultReady(d)
}
};
Worker* w = new Worker();
w.moveToThread(thread);
connect(w,SIGNAL(resultReady(Data*)),this,SLOT(showResult(Data*)));
thread->start();
// To start the worker you need to invoke the method through the event
// loop of the threads its living in
// Just wrote this down out of my head ... typos included.
class Worker : public QObject
{ Q_OBJECT
signals:
void resultReady(Data* d)
public:
Worker() ..
public slots:
void calculate()
{
emit resultReady(d)
}
};
Worker* w = new Worker();
QThread* thread = new QThread();
w.moveToThread(thread);
connect(w,SIGNAL(resultReady(Data*)),this,SLOT(showResult(Data*)));
thread->start();
// To start the worker you need to invoke the method through the event
// loop of the threads its living in
QMetaObject::invokeMethod(w,"calculate");
To copy to clipboard, switch view to plain text mode
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.
Johannes
Bookmarks