PDA

View Full Version : change QGraphicsView* in QThread



deca5423
28th June 2009, 22:52
I want to be able to pass a QGraphicsView* to my QThread class and be able to update the view in the thread. The QGraphicsView* obviously belongs to a gui and is not created by thread. I attempted to edit the QGraphicsView* with the thread and I get these errors in the console:

QObject::startTimer: timers cannot be started from another thread

It crashes 90% of the time. I read you cannot change a QWidget* in a thread it wasn't created. Fair enough. But what am I supposed to do?

wysota
28th June 2009, 23:01
No way, we can't access widgets from worker threads. You have to do all gui operations in the main thread transfering data across threads if needed using events or signals.

deca5423
29th June 2009, 13:46
Ok I added a signal in my QThread class and a slot in the class that uses the QGraphicsView. First it actually adds lines to a QGraphicsScene, and I update this using a slot, and then another signal to update the QGraphicsView with a slot. My QThread emits the signals. However, it seems like more than half the signals get lost because its only drawing half the lines. It got rid of the original timer errors but it will often crash.

The reason I'm doing this is becuase I have a QListWidget that will draw up to and over 30,000 lines to a QGraphicsScene to a QGraphicsView. This can be lengthy and if the item is changed in the list ( signal currentItemChanged ), we're wasting time waiting it for it to draw it even though its been changed! So basically I want to be able to kill the thread thats attempting to draw if the item has changed to stop wasting time and begin drawing the new one.

Or is there a better way to do this?

wagmare
29th June 2009, 13:55
we're wasting time waiting it for it to draw it even though its been changed! So basically I want to be able to kill the thread thats attempting to draw if the item has changed to stop wasting time and begin drawing the new one.


then see u use wait() condition in destructor of QThread ... means letting the Thread finish its pending work .. sometimes system crash when the thread killed before completed ..

i will recommend mandlebrot example
http://doc.trolltech.com/4.3/threads-mandelbrot.html

will helps a lot

deca5423
30th June 2009, 02:12
Awesome, thank you wagmare. Using the mutex makes sense. I didn't know terminating a thread while its running would cause it to crash. ( I didn't actually read the api call until now, I just saw it from intellisense ). Also figured out that it wasn't drawing half the lines because I had to use Qt::BlockingQueuedConnection otherwise it just gets bombarded with signals and it gets lost. I used a function to set a boolean in the thread to tell it return from run(). Works exactly how I want. Thanks again!