Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to update the GUI from worker thread?
QtWS25 Last Chance

How to update the GUI from worker thread?

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 4.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dheerendra
    Qt Champions 2022
    wrote on 10 Nov 2018, 11:28 last edited by
    #2

    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.

    Dheerendra
    @Community Service
    Certified Qt Specialist
    http://www.pthinks.com

    1 Reply Last reply
    5
    • K Offline
      K Offline
      kshegunov
      Moderators
      wrote on 12 Nov 2018, 11:02 last edited by
      #3

      @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.

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      4
      • N Offline
        N Offline
        NoumanYosuf
        wrote on 13 Nov 2018, 15:55 last edited by
        #4

        But Isnt then the slot will be executed from main thread then and worker thread will not actly update UI but only trigger signal?

        K 1 Reply Last reply 13 Nov 2018, 16:09
        0
        • N NoumanYosuf
          13 Nov 2018, 15:55

          But Isnt then the slot will be executed from main thread then and worker thread will not actly update UI but only trigger signal?

          K Offline
          K Offline
          kshegunov
          Moderators
          wrote on 13 Nov 2018, 16:09 last edited by
          #5

          @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.

          Read and abide by the Qt Code of Conduct

          N 1 Reply Last reply 15 Nov 2018, 06:54
          1
          • N Offline
            N Offline
            NoumanYosuf
            wrote on 15 Nov 2018, 06:42 last edited by
            #6

            But it want to update UI from worker. just like main do.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              dheerendra
              Qt Champions 2022
              wrote on 15 Nov 2018, 06:45 last edited by
              #7

              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.

              Dheerendra
              @Community Service
              Certified Qt Specialist
              http://www.pthinks.com

              1 Reply Last reply
              3
              • K kshegunov
                13 Nov 2018, 16:09

                @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.

                N Offline
                N Offline
                NoumanYosuf
                wrote on 15 Nov 2018, 06:54 last edited by
                #8

                @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.

                J 1 Reply Last reply 15 Nov 2018, 07:32
                0
                • N NoumanYosuf
                  15 Nov 2018, 06:54

                  @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.

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 15 Nov 2018, 07:32 last edited by
                  #9

                  @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.

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  1 Reply Last reply
                  4
                  • N Offline
                    N Offline
                    NoumanYosuf
                    wrote on 15 Nov 2018, 08:42 last edited by NoumanYosuf
                    #10

                    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.

                    K 1 Reply Last reply 15 Nov 2018, 10:35
                    0
                    • N NoumanYosuf
                      15 Nov 2018, 08:42

                      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.

                      K Offline
                      K Offline
                      kshegunov
                      Moderators
                      wrote on 15 Nov 2018, 10:35 last edited by
                      #11

                      @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.

                      Read and abide by the Qt Code of Conduct

                      1 Reply Last reply
                      1

                      11/11

                      15 Nov 2018, 10:35

                      • Login

                      • Login or register to search.
                      11 out of 11
                      • First post
                        11/11
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved