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. Progress bar not showing up
Forum Updated to NodeBB v4.3 + New Features

Progress bar not showing up

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 984 Views 1 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.
  • P Offline
    P Offline
    poordev123
    wrote on last edited by
    #1

    I have a set of functions that when they run can take a few minutes to finish. I want to display a progress bar while they are running. I have a run function for now that just sleeps for x amount of time

    def run(self, n):
        for i in range(n):
            time.sleep(.05)
            self.ui.progressBar.setValue(i+1)
    

    and then in my main I have

    def run_pls_gui(self):
        self.ui.progressBar = QProgressBar()
        n = 50
        self.ui.progressBar.setMinimum(0)
        self.ui.progressBar.setMaximum(n)
        layout = QVBoxLayout()
        layout.addWidget(self.ui.progressBar)
    
        self.ui.pb_tester_connect_disconnect.clicked.connect(lambda: self.run(n))
    

    eventually I am going to change this so that instead of calling 'self.run' I will call the functions that need to run. My question is, how do I get the progress bar to show up while this function runs? Do I need to add to run? Or add to my main?

    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by VRonin
      #2

      You have 2 possibilities:

      • The quick and dirty is adding QtCore.QCoreApplication.processEvents inside the loop of run().
      • If you want to do it properly then move the computation to a different thread (e.g. with QThread or QtConcurrent) and send update signals to the main thread to update the progress bar

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      P 1 Reply Last reply
      1
      • VRoninV VRonin

        You have 2 possibilities:

        • The quick and dirty is adding QtCore.QCoreApplication.processEvents inside the loop of run().
        • If you want to do it properly then move the computation to a different thread (e.g. with QThread or QtConcurrent) and send update signals to the main thread to update the progress bar
        P Offline
        P Offline
        poordev123
        wrote on last edited by
        #3

        @VRonin So I would start a QThread in my main function and that would make the progress bar show up? From there I would then use

        progressBar.setValue(x)
        

        to do that?

        VRoninV S 2 Replies Last reply
        0
        • P poordev123

          @VRonin So I would start a QThread in my main function and that would make the progress bar show up? From there I would then use

          progressBar.setValue(x)
          

          to do that?

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          @poordev123 said in Progress bar not showing up:

          So I would start a QThread in my main function and that would make the progress bar show up?

          No, it's more involved than that, see https://mayaposch.wordpress.com/2011/11/01/how-to-really-truly-use-qthreads-the-full-explanation/

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • P poordev123

            @VRonin So I would start a QThread in my main function and that would make the progress bar show up? From there I would then use

            progressBar.setValue(x)
            

            to do that?

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

            @poordev123 said in Progress bar not showing up:

            So I would start a QThread in my main function and that would make the progress bar show up? From there I would then use
            progressBar.setValue(x)

            to do that?

            Maybe it is a little easier to use QtConcurrent::run() instead of a fully fledged QThread. This is only an aside. There are several possibilities to use threads here.

            There is one thing all threaded solutions have in common: You cannot just call progressBar.setValue(x). This function can only be called from the GUI thread. Otherwise your app might (occassionally) crash. Instead you have to put this call into the event loop of the GUI thread. Just have a signal inside your new thread connected to the slot setValue() of your progressBar. Connections between threads will just work properly. You can also skip having your own slot and post an event inside the event loop of the GUI thread directly. In C++ this would look like this:

            QMetaObject::invokeMethod(qApp, [=](int x){ progressBar.setValue(x); });
            

            One thing to remember: Don't update the progressBar too often as this will significantly slow down the actual computation. You can check if the progress value has actually changed (for one single percent) or if at least 100ms have passed (or both). This little trick can be the difference between seconds and minutes.

            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