PDA

View Full Version : Regarding emit'ing signals from a thread (QT3)



milewis1
6th January 2006, 21:58
I have an application which pulls image data from memory (provided by a shared library) and displays the image to the user. My appliation has two threads, the GUI thread and the worker thread which gets the image data.

Right now when the worker thread gets all the data for an image I use emit to send the data (as a QByteArray) to the GUI thread. This does work, but I'm wondering if it's a good thing to do?

Every now and then I get weird errors that seem to indicate that the worker thread is actually handling the display too (I had hoped emitting the data from the worker thread would cause it to be processed in the GUI thread).

Basically, I'm wondering if this is the wrong approach to take. I've read that this works in QT4 (unfortunately I don't have the option of using QT4). Does anyone know if it works in QT3 as well?

Thanks in advance.

wysota
6th January 2006, 22:08
This is the wrong approach to take if you wish to display that image ;)

Read this: http://doc.trolltech.com/3.3/threads.html

Use custom events (http://doc.trolltech.com/3.3/qcustomevent.html) instead.

yop
6th January 2006, 22:15
If you have the Gui Programming with Qt3 book take a look at chapter 17, especially "Communicating with the GUI Thread".
In a few words you can create a custom event that will have as a public member the array you want to send to the gui thread. Then whenever you want the gui to accept data you send the new event to the gui's enent handler from the worker thread using
QApplication::postEvent() and the array can then be handled from your gui thread.

EDIT: Funny... Check out what happens if I don't wrap in [C O D E] tags: QApplication::postEvent()
EDIT2: With all this previewing and editing my post became obsolete. Anyway since you got the same answer twice it means that this is the way to go ;)