0

i want to implement a QProgressbar for a function in my GUI. I thought the process is simple but it seems not to be.

I thought i can implement the bar like this:

1- Button clicked 2- Progressbar and function start simultaneously 3- Function and Progressbar end simultaneously

What i have found when i searched is the update of this bar with a for-loop.I don't want to do so because my code is a simple function that must be run only one time.

Is it possible to do so ? Or am i misunderstanding something ? I found that the start and stop signals have to be managed with Threading. I wanted to ask here before i go and do further search.

Thanks.

thethe
  • 1
  • 2
  • If you have a function that has no control over progress or knowledge about its duration, you cannot show any value for the progress bar. That's quite clear: if you don't know how far is a place where you're going to, can you predict how much time it takes to arrive there even during the travel? – musicamante Apr 18 '22 at 09:47
  • Thank you for the hint. I mean, can i show the bar without Percentages ? Only an indicator ? – thethe Apr 18 '22 at 10:50
  • @thethe If you set the min and max of the progress-bar to zero, it will show a busy indicator. It may still be necessary to run the function in a worker thread so as not to block the gui. Use the started/finished signals of the thread to start/stop the progress-bar. – ekhumoro Apr 18 '22 at 10:56
  • @ekhumoro Thank you. Do you have an example for this kind of implementation ? – thethe Apr 18 '22 at 11:27
  • @thethe If you search SO, you will find dozens of basic examples for how to use worker threads in pyqt. – ekhumoro Apr 18 '22 at 12:02

1 Answers1

0

I think the best practice is, using the main thread for QProgressbar or QProgressDialog and side thread for your function.

When main thread is used for your function, the UI won't be updated, even you use for-loop to setValue.

What I am doing is, create a QThread like with a signal giving out the progress:

class AThread(QThread):
    progress = pyqtSignal(int)

    def run(self):
       for i in range(100):
           self.progress.emit(i)

Then set the value of progressbar using the number in the signal

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
lee zhang
  • 26
  • 2
  • The OP clearly says that they don't want to use a for loop. – musicamante Apr 18 '22 at 09:52
  • This is just an example. The for loop here is used for telling progress. He can use anyway to give out progress, even dummy progress. My key point is, if using main thread to calculate, there is no way to update progressbar, even with forloop. – lee zhang Apr 19 '22 at 07:41
  • That's not completely true nor accurate, but that's not the point: the threading option was already known to the OP, and this answer doesn't really add anything to dozens of other answers even here on StackOverflow, especially considering that you did exactly what the OP doesn't want. Even if it was an example, you could've at least used a `time.sleep()`, for the sake of consistency. – musicamante Apr 19 '22 at 10:35