class TimedProgressBar(QProgressBar): """A QProgressBar showing a certain time elapse.""" hideOnTimeout = True def __init__(self, parent=None): super(TimedProgressBar, self).__init__(parent, minimum=0, maximum=100) self._timeline = QTimeLine(updateInterval=100, frameChanged=self.setValue) self._timeline.setFrameRange(0, 100) self._hideTimer = QTimer(timeout=self._done, singleShot=True, interval=3000) def start(self, total, elapsed=0.0): """Starts showing progress. total is the number of seconds (maybe float) the timeline will last, elapsed (defaulting to 0) is the value to start with. """ self._hideTimer.stop() self._timeline.stop() self._timeline.setDuration(total * 1000) self._timeline.setCurrentTime(elapsed * 1000) self.setValue(self._timeline.currentFrame()) self._timeline.resume() if self.hideOnTimeout: self.show() def stop(self, showFinished=True): """Ends the progress display. If showFinished is True (the default), 100% is shown for a few seconds and then the progress is reset. The progressbar is hidden if the hideOnTimeout attribute is True. """ self._hideTimer.stop() self._timeline.stop() if showFinished: self.setValue(100) self._hideTimer.start() else: self._done() def _done(self): if self.hideOnTimeout: self.hide() self.reset()