How to update the GUI from worker thread?
-
I know GUI cant be updated from worker thread but only by main/GUI thread using signal and slots. But I want to update my GUI from worker thread.How can this be possible. How can I give my worker thread authority to change GUI like main thread do.
[Moved to General and Desktop ~kshegunov]
-
You can define the parameterised signal from worker thread. Emit that signal from worker thread. Define the slots in main thread context. Connect signal & slots. It should work. It is not a good practice update UI directly from worker thread.
-
@NoumanYosuf said in How to update the GUI from worker thread?:
But I want to update my GUI from worker thread.How can this be possible.
It is not. Connect your worker's signals to slots in the GUI thread.
-
But Isnt then the slot will be executed from main thread then and worker thread will not actly update UI but only trigger signal?
-
But Isnt then the slot will be executed from main thread then and worker thread will not actly update UI but only trigger signal?
@NoumanYosuf said in How to update the GUI from worker thread?:
But Isnt then the slot will be executed from main thread then and worker thread will not actly update UI but only trigger signal?
Yes, that is correct.
-
But it want to update UI from worker. just like main do.
-
Don't directly update UI from worker thread. @kshegunov suggested define the signals in worker threads. Have slots in main thread. Connect signals & Slots. This way it help you.
-
@NoumanYosuf said in How to update the GUI from worker thread?:
But Isnt then the slot will be executed from main thread then and worker thread will not actly update UI but only trigger signal?
Yes, that is correct.
@kshegunov Thankyou for the reply.
I want worker thread to update GUI. I want main thread to be free from burden to update GUI because my GUI has to be update very frequently. If that is possible then my main thread will be faster to do some other heavy task too without slowing down the application. -
@kshegunov Thankyou for the reply.
I want worker thread to update GUI. I want main thread to be free from burden to update GUI because my GUI has to be update very frequently. If that is possible then my main thread will be faster to do some other heavy task too without slowing down the application.@NoumanYosuf You should know this: UI may not be updated from other threads than UI thread! This is simply not supported and will lead to misbehaviour and crashes. Don't even try.
So, your UI thread is supposed to handle the UI, not some other threads. Your worker threads, which do all the other work, simply notify the UI thread to update the UI like @kshegunov said. It is completely wrong to let the worker thread to update UI - worker thread is responsible for all the work which is done behind UI not the UI itself.
"main thread will be faster to do some other heavy task too without slowing down the application" - looks like you mean UI thread when you say main thread. Again: do not do any heavy work in main thread! Do it in worker threads, then there will be no slow down of your UI. -
I was trying if this is possible. I am aware that UI cant be updated from the worker and only through signal and slots. Thank you for the reply.
-
I was trying if this is possible. I am aware that UI cant be updated from the worker and only through signal and slots. Thank you for the reply.
@NoumanYosuf said in How to update the GUI from worker thread?:
I was trying if this is possible.
It simply is not. You can't do anything about it, it's how the Qt toolkit is designed.