
Originally Posted by
kiranraj
1. Is it possible to do this ?
Yes.
since Both the threads will be accessing the same GraphicsScene.
Use signals and slots - do all needed calculations in the worker thread and then signal the results to the main thread that will insert the new item into the scene.
Main thread will be reading other thread will be adding line items to it.
No, items have to be added by the main thread. If you don't need any calculations and just want to add a bunch of items without freezing the application then you won't need another thread - just use a timer with a 0 timeout.
2. Is there any way to synchronize both the threads ?
Signals and slots or events (which is practically the same).
3. Is there any other solution?
I think I already answered that... But anyway a small snippet of code to illustrate:
Q_OBJECT
public:
m_last = -1;
initializeScene(); // starts the asynchronous task that fills the scene with items
}
private slots:
void initializeScene(){
int done = 0;
while(++m_last<10000 && done++<10){
addLine(...);
}
if(m_last>=10000)
return;
QTimer::singleShot(0,
this,
SLOT(initializeScene
()));
}
};
class GraphicsScene : public QGraphicsScene {
Q_OBJECT
public:
GraphicsScene(...) : QGraphicsScene(...){
m_last = -1;
initializeScene(); // starts the asynchronous task that fills the scene with items
}
private slots:
void initializeScene(){
int done = 0;
while(++m_last<10000 && done++<10){
addLine(...);
}
if(m_last>=10000)
return;
QTimer::singleShot(0, this, SLOT(initializeScene()));
}
};
To copy to clipboard, switch view to plain text mode
Bookmarks