QProgressBar

../_images/4_8_qprogressbar_sample.png

../_images/4_8_qprogressbar_sample2.png

The figure above shows the basic progress bar for macOS (top) and Windows 7 (bottom). The progress bar is a widget that is used for time-consuming tasks. ( QProgressBar official document )

The QProgressBar widget provides a horizontal and vertical progress bar. You can set the minimum and maximum values for the progress bar with setMinimum() and setMaximum() methods, or you can set the range at once with setRange() methods. Defaults are 0 and 99.

The setValue() method lets you set the progress of the progress bar to a specific value, and the reset() method returns to its initial state.


../_images/4_8_qprogressbar_sample3.png

If you set both the minimum and maximum values for the progress bar to 0, the progress bar will always be displayed as in progress as shown in the figure above. This feature can be useful if you don’t know the capacity of the file you’re downloading.


Example

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QProgressBar
from PyQt5.QtCore import QBasicTimer


class MyApp(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        self.pbar = QProgressBar(self)
        self.pbar.setGeometry(30, 40, 200, 25)

        self.btn = QPushButton('Start', self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.doAction)

        self.timer = QBasicTimer()
        self.step = 0

        self.setWindowTitle('QProgressBar')
        self.setGeometry(300, 300, 300, 200)
        self.show()

    def timerEvent(self, e):

        if self.step >= 100:

            self.timer.stop()
            self.btn.setText('Finished')
            return

        self.step = self.step + 1
        self.pbar.setValue(self.step)

    def doAction(self):

        if self.timer.isActive():
            self.timer.stop()
            self.btn.setText('Start')
        else:
            self.timer.start(100, self)
            self.btn.setText('Stop')


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = MyApp()
    sys.exit(app.exec_())

Create a horizontal progress bar and pushbutton. The pushbutton lets you start and stop the progress bar.


Description

self.pbar = QProgressBar(self)

Create a single progress bar with the QProgressBar generator.


self.timer = QBasicTimer()

To activate the progress bar, use a timer object.


self.timer.start(100, self)

To run a timer event, call the start() method This method has two parameters: the first is the end time and the second is the object on which the event will be performed.


def timerEvent(self, e):

    if self.step >= 100:

        self.timer.stop()
        self.btn.setText('Finished')
        return

    self.step = self.step + 1
    self.pbar.setValue(self.step)

Each QObject and its descendants has a timerEvent() event handler. To respond to a timer event, reconfigure the event handler.


def doAction(self):

    if self.timer.isActive():
        self.timer.stop()
        self.btn.setText('Start')
    else:
        self.timer.start(100, self)
        self.btn.setText('Stop')

The doAction() method helps start and stop the timer.


Results

../_images/4_8_qprogressbar.png ../_images/4_8_qprogressbar_mac.png

Figure 5-8. Make progress bar.


Prev/Next