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 another thread?
Forum Updated to NodeBB v4.3 + New Features

How to update the GUI from another thread?

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 2.5k Views 3 Watching
  • 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.
  • H Offline
    H Offline
    HoliGui
    wrote on last edited by HoliGui
    #1

    Hi,
    I have a long-running task (for loop) which I want to run in a seperate QThread (to not block the gui).
    In each iteration of the loop I want to send an update to my GUI which will draw a line. How would I do this?
    Do I have to use signals to communicate between the MainThread (GUI) and the QThread where I run my computation?

    Bonus questions: Is it possible to pause the for loop which is inside the seperate QThread via some pause button and then continue again from where it stopped? Is there documentation for this?

    JonBJ 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi and welcome to devnet,

      See the Mandelbrot example for a possible implementation.

      As for stopping and restarting the loop, sure you can, a way to do it also shown in that example. There might be others depending on your needs though.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply
      1
      • H HoliGui

        Hi,
        I have a long-running task (for loop) which I want to run in a seperate QThread (to not block the gui).
        In each iteration of the loop I want to send an update to my GUI which will draw a line. How would I do this?
        Do I have to use signals to communicate between the MainThread (GUI) and the QThread where I run my computation?

        Bonus questions: Is it possible to pause the for loop which is inside the seperate QThread via some pause button and then continue again from where it stopped? Is there documentation for this?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @HoliGui said in How to update the GUI from another thread?:

        Do I have to use signals to communicate between the MainThread (GUI) and the QThread where I run my computation?

        Yep :)

        Bonus questions: Is it possible to pause the for loop which is inside the seperate QThread via some pause button and then continue again from where it stopped?

        QThreads do not natively support "pausing". You might achieve this with signals & slots too. You will have to write the link to a UI "Pause" button yourself.

        1 Reply Last reply
        1
        • SGaistS SGaist

          Hi and welcome to devnet,

          See the Mandelbrot example for a possible implementation.

          As for stopping and restarting the loop, sure you can, a way to do it also shown in that example. There might be others depending on your needs though.

          H Offline
          H Offline
          HoliGui
          wrote on last edited by HoliGui
          #4

          @SGaist Thank you this looks really promising. Do you have a suggestion how to decouple GUI logic from business logic?
          I tought about implementing an observer/Eventlistener pattern to communicate with the GUI.

          S 1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on last edited by
            #5

            In the case of the example the decoupling is already done.

            Your business logic should go in a class (or several depending on it) and then you can simply use signals and slots to transmit your data between both. Or use the model view approach.

            It really depends on what your business logic does and what kind of data it generates that shall be shown on the UI.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • H HoliGui

              @SGaist Thank you this looks really promising. Do you have a suggestion how to decouple GUI logic from business logic?
              I tought about implementing an observer/Eventlistener pattern to communicate with the GUI.

              S Offline
              S Offline
              SimonSchroeder
              wrote on last edited by
              #6

              @HoliGui said in How to update the GUI from another thread?:

              Thank you this looks really promising. Do you have a suggestion how to decouple GUI logic from business logic?
              I tought about implementing an observer/Eventlistener pattern to communicate with the GUI.

              Qt's signals and slots already is some sort of observer pattern: The observer will register its slot with the appropriate signals. No need to reinvent the wheel here. Your business logic would just have a signals saying "this has changed" and the GUI logic would take that information (in a slot) and decide how to display it.

              The problem of executing GUI code in a different thread occurred for me over and over again. You can queue function calls using QMetaObject::invokeMethod(qGuiApp, ...). Most easily just put in a lambda for '...'. I wrote a little helper library for this: https://github.com/SimonSchroeder/QtThreadHelper . You can just use guiThread(...) to execute any function inside the GUI thread. It is a single header library (requires C++17).

              1 Reply Last reply
              1

              • Login

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