0

I have a ProgressBar for downloading data.

Sometimes the task can timeout and if so I want to be able to restart the process.

  1. Task is started
  2. Timer is started
  3. if the timer finishes, end the task
  4. press the button to restart the task

All of the above works fine except for the progressbar does not start again.

I can reset it to 0 but it will not move (before that it was stuck at 100%!)

If the task finishes before the timer runs out, the progress bar works fine again.

How can I restart the progressbar if the timer runs out before the task?

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

class DownloadThread(QThread):
    taskFinished = pyqtSignal()

    def __init__(self, parent=None):
        super(DownloadThread, self).__init__(parent)

    def run(self):
        for i in range(20000):
            print(i)
        self.taskFinished.emit()


class Example(QWidget):

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

        self.initUI()

    def initUI(self):      
        # Set Progress Bard
        self.progressBar = QProgressBar(self)
        self.progressBar.setGeometry(30, 40, 200, 25)

        # Button
        self.btn = QPushButton('Start', self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.start_download_thread)

        # Set Timer
        self.timer = QBasicTimer()
        self.step = 0

        # Display        
        self.setGeometry(300, 300, 280, 170)
        self.show()

    def start_download_thread(self):
        # Set Progress bar to 0%
        self.progressBar.setValue(0)


        #Start Thread
        self.Download = DownloadThread()
        self.Download.taskFinished.connect(self.onDownloaded)
        self.onDownloading()
        self.Download.start()    

    def onDownloading(self):
        #start the timer
        self.progressBar.show()
        self.timerSwitch()


    def timerSwitch(self):
        # Turn timer off or on
        if self.timer.isActive():
            self.timer.stop()
        else:
            self.timer.start(2, self)

    def onDownloaded(self):
        # Stop the timer
        self.timerSwitch()
    # progress bar 100%
        self.progressBar.setValue(1)
        self.progressBar.setRange(0, 1)

    def timerEvent(self, e): 
        if self.step >= 100:
            self.timer.stop()
            # self.Download.quit()

            return
        self.step = self.step + 1
        self.progressBar.setValue(self.step)


def main():

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


if __name__ == '__main__':
    main()
johnashu
  • 2,167
  • 4
  • 19
  • 44

1 Answers1

0

I solved this by moving the Qtimer() step into the start_download_thread

def start_download_thread(self):      

    self.step = 0 # reset the steps to 0

This means it resets the steps and the progress bar starts again.

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

class DownloadThread(QThread):
    taskFinished = pyqtSignal()

    def __init__(self, parent=None):
        super(DownloadThread, self).__init__(parent)

    def run(self):
        for i in range(20000):
            print(i)
        self.taskFinished.emit()


class Example(QWidget):

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

        self.initUI()

    def initUI(self):      
        # Set Progress Bard
        self.progressBar = QProgressBar(self)
        self.progressBar.setGeometry(30, 40, 200, 25)

        # Button
        self.btn = QPushButton('Start', self)
        self.btn.move(40, 80)
        self.btn.clicked.connect(self.start_download_thread)

        # Set Timer
        self.timer = QBasicTimer()

        # Display        
        self.setGeometry(300, 300, 280, 170)
        self.show()

    def start_download_thread(self):       

        self.step = 0

        # Set Progress bar to 0%
        self.progressBar.setValue(0)       

        #Start Thread
        self.Download = DownloadThread()
        self.Download.taskFinished.connect(self.onDownloaded)
        self.onDownloading()
        self.Download.start()

    def onDownloading(self):
        #start the timer
        self.progressBar.show()
        self.timerSwitch()

    def timerSwitch(self):
        # Turn timer off or on
        if self.timer.isActive():
            self.timer.stop()
        else:
            self.timer.start(2, self)

    def onDownloaded(self):
        # Stop the timer
        self.timerSwitch()
    # progress bar 100%
        self.progressBar.setValue(100)
        self.progressBar.setRange(0, 100)

    def timerEvent(self, e): 
        if self.step >= 100:
            self.timer.stop()
            return
        self.step = self.step + 1
        self.progressBar.setValue(self.step)


def main():

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


if __name__ == '__main__':
    main()
johnashu
  • 2,167
  • 4
  • 19
  • 44