PyQt QProgressBar

Summary: in this tutorial, you’ll learn how to use the PyQt QProgressBar class to create a progress bar widget.

Introduction to the PyQt QProgressBar

A progress bar widget notifies the users of the progress of an operation and reassures them that the program is still running.

To create a progress bar widget, you use the QProgressBar class:

QProgressBar()Code language: Python (python)

A progress bar has three important values:

  • The minimum value.
  • The maximum value.
  • The current step value.

By default, the minimum value defaults to zero, and the maximum value defaults to 100.

To update the progress, you increase the current step value. By doing this, the progress bar will display the percentage of steps that have been completed.

The QProgressBar class uses the following formula to calculate the progress of the steps:

(current_value - minimum ) / (maximum - minimum)Code language: Python (python)

For example, if the current_value is 50, then the percentage of steps is 50%. The progress bar will display the progress like this:

PyQt QProgressBar example

To set the current step value, you use the setValue() method. To get the current value, you use the value() method. To reset the progress bar so that it shows no progress, you use the reset() method.

If you don’t want to override the default minimum and maximum values, you can use the setMinimum() and setMaximum() methods to change them.

Also, you can use the setRange() method to set both minimum and maximum values at once:

setRange(minimum, maximum)Code language: Python (python)

To get the minimum and maximum values, you use the minimum() and maximum() methods.

In practice, you’ll use the progress bar widget with threads to update the progress of long-running operations

PyQt QProgressBar example

The following example uses the QProgressBar class to create a progress bar.

If you click the progress button, it’ll increase the current value to the maximum value. However, if you click the reset button, it’ll reset the progress bar.

PyQt QProgressBar - Program example
import sys
from PyQt6.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QProgressBar
from PyQt6.QtCore import Qt


class MainWindow(QWidget):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.setGeometry(100, 100, 300, 50)
        self.setWindowTitle('QProgressBar Demo')

        layout = QVBoxLayout()
        self.setLayout(layout)

        hbox = QHBoxLayout()
        self.progress_bar = QProgressBar(self)
        hbox.addWidget(self.progress_bar)

        layout.addLayout(hbox)

        hbox = QHBoxLayout()
        self.btn_progress = QPushButton('Progress', clicked=self.progress)
        self.bnt_reset = QPushButton('Reset', clicked=self.reset)

        # align buttons center
        hbox.addStretch()
        hbox.addWidget(self.btn_progress)
        hbox.addWidget(self.bnt_reset)
        hbox.addStretch()
        
        layout.addLayout(hbox)
        self.current_value = 0
        self.show()

    def reset(self):
        self.current_value = 0
        self.progress_bar.reset()

    def progress(self):
        if self.current_value  <= self.progress_bar.maximum():
            self.current_value += 5
            self.progress_bar.setValue(self.current_value)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = MainWindow()
    sys.exit(app.exec())
Code language: Python (python)

How it works.

First, create a progress bar using the QProgressBar class:

self.progress_bar = QProgressBar(self)Code language: Python (python)

Second, initialize the current value:

self.current_value = 0Code language: Python (python)

Third, increase the current value by 5 and update it as the current value using the setValue() for the progress bar in the progress() method:

def progress(self):
    if self.current_value  <= self.progress_bar.maximum():
        self.current_value += 5
        self.progress_bar.setValue(self.current_value)Code language: Python (python)

Finally, reset the current value and call the reset() method to reset the progress bar in the reset() method:

def reset(self):
    self.current_value = 0
    self.progress_bar.reset()Code language: Python (python)

Summary

  • Use the QProgressBar class to create progress bar widgets.
  • Use the setValue() to set the current value that reflects the percentage of the current progress.
  • Use the reset() method to reset the progress bar so that it shows no progress.
Did you find this tutorial helpful ?