PDA

View Full Version : QT3 -- How does a gui communicate with a worker thread?



Ronayn
20th September 2011, 15:20
QT3 -- How does a gui communicate with a worker thread?

I have a simple gui that creates and then launches a thread (derived from QThread). The thread does some continuous processing and remains for the life of the gui. In order for the thread to communicate with the gui, I use postevent -- which, as I understand, is the correct way for a thread to communicate with the gui.

However, I am not sure how to safely communicate from the gui to the thread. Right now, I am directly accessing member functions/variables of my derived thread class (since the gui created/launched it). This method isn't safe, right?

What is the "official" way for the gui to communicate with a thread? (Note: I am stuck using QT3)

Thank You!

wysota
20th September 2011, 15:26
QT3 -- How does a gui communicate with a worker thread?
Using events.


I have a simple gui that creates and then launches a thread (derived from QThread). The thread does some continuous processing and remains for the life of the gui. In order for the thread to communicate with the gui, I use postevent -- which, as I understand, is the correct way for a thread to communicate with the gui.
That's correct.


However, I am not sure how to safely communicate from the gui to the thread.
Basically, you can't. You can only use mutexes and a shared variable. Hard to call that "communication" though.

Ronayn
20th September 2011, 16:30
Wysota, your first answer contradicts your third. To be crystal on this point, a QThread can use events to communicate (one-way) to the gui, but the gui cannot post events to the QThread (because it doesn't have an event loop). That is correct, yes?

So, you are saying QT3 doesnt provide a safe way for the gui to communicate to a QThread? If so, that's a bummer :(

Does QT4? (if you know)

wysota
20th September 2011, 20:13
Wysota, your first answer contradicts your third.
No, it doesn't :)

To be crystal on this point, a QThread can use events to communicate (one-way) to the gui, but the gui cannot post events to the QThread (because it doesn't have an event loop). That is correct, yes?
Yes, only one-way communication is safe in Qt3.


So, you are saying QT3 doesnt provide a safe way for the gui to communicate to a QThread? If so, that's a bummer :(
Not anything built-in. You can only use a mutex, have a queue of messages and periodically check the queue from within the thread.


Does QT4? (if you know)
Yes, in Qt4 you can use signals and slots and events (both ways).