Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. ProgressBar resume from where it stopped
Forum Updated to NodeBB v4.3 + New Features

ProgressBar resume from where it stopped

Scheduled Pinned Locked Moved Solved Qt for Python
11 Posts 4 Posters 1.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • monamourM Offline
    monamourM Offline
    monamour
    wrote on last edited by SGaist
    #1

    I have created a desktop application by PYQT5 and python 3.7 to download a video by clicking the download button and save it locally in the PC.

    The code will fetch the video link from (lineEdit.text()) which is labeled "URL" and save it in the local directory in (lineEdit_2.text()) which is labeled "SAVE AS". If the download stops for any reason, it will be resumed again by press the start download button. In addition, the ProgressBar start from 1% until 100% along with downloading the video. Everything working smoothly.

    The question is, once the video stops in the middle for any reason then it resumes the downloading again but the ProgressBar should start from where it stopped but it is not. For example, if it stops in 50% then should be resumed from 50% and continue. However, it starts from 0% (from the beginning).

    The code

    
        def curl_progress(self,total, existing, totalfrac,fracmb):
    
            global frac,tsize,size,save_location
    
            try:
    
                frac= float(existing)/float(total)
                self.progressBar.setValue(totalfrac)
                QApplication.processEvents()
               
            except (ZeroDivisionError, RuntimeError, TypeError, NameError):
                frac = 0
    
            self.textBrowser.append("Downloaded %d/%d %d%%" % (existing, total, totalfrac))
    
    
            if frac ==1.0:
                self.textBrowser.append("")
                size = os.path.getsize(save_location)
                tsize= (size /1024 /1024)
                QMessageBox.information(self,"Download Completed", "The Download is Finished and the size is %03.2f MB" %(tsize,))
                self.textBrowser.append('Size of file is %03.2f MB' %(tsize,))
                self.progressBar.setValue(0)
                self.lineEdit.setText('')
                self.lineEdit_2.setText('')
                QMessageBox.close(self)
    
    
            else:
                self.textBrowser.append("Downloaded %d/%d %d%%" % (existing, total, totalfrac))
    
    
        def curl_limit_rate(self,rate_limit):
            global tsize,size,save_location
            url= self.lineEdit.text()
            save_location = self.lineEdit_2.text()
            if len(url) == 0 and len(save_location) == 0:
                QMessageBox.information(self, "Error", "Please put the links")
                return
            if len(url) > 0 and len(save_location) == 0:
                QMessageBox.information(self, "Error", "Please put the location")
                return
    
            if len(url) == 0 and len(save_location) > 0:
                QMessageBox.information(self, "Error", "Please put the link")
                return
    
            if len(url) > 0 and len(save_location) > 0:
    
                c = pycurl.Curl()
                c.setopt(pycurl.CAINFO, certifi.where())
                c.setopt(c.URL,url)
                c.setopt(c.MAX_RECV_SPEED_LARGE, rate_limit)
                if os.path.exists(save_location):
                    file_id = open(save_location, "ab")
                    c.setopt(c.RESUME_FROM, os.path.getsize(save_location))
                else:
                    file_id = open(save_location, "wb")
    
                c.setopt(c.WRITEDATA, file_id)
                c.setopt(c.NOPROGRESS, 0)
                c.setopt(c.PROGRESSFUNCTION, self.curl_progress)
                c.perform()
                c.close()
            else:
                QMessageBox.information(self, "Error", "Unknown error!")
    
    
    

    The picture

    progressBarU.jpg

    Many thanks in advance,

    [edit: fixed text paragraph SGaist]

    jsulmJ 1 Reply Last reply
    0
    • monamourM monamour

      I have created a desktop application by PYQT5 and python 3.7 to download a video by clicking the download button and save it locally in the PC.

      The code will fetch the video link from (lineEdit.text()) which is labeled "URL" and save it in the local directory in (lineEdit_2.text()) which is labeled "SAVE AS". If the download stops for any reason, it will be resumed again by press the start download button. In addition, the ProgressBar start from 1% until 100% along with downloading the video. Everything working smoothly.

      The question is, once the video stops in the middle for any reason then it resumes the downloading again but the ProgressBar should start from where it stopped but it is not. For example, if it stops in 50% then should be resumed from 50% and continue. However, it starts from 0% (from the beginning).

      The code

      
          def curl_progress(self,total, existing, totalfrac,fracmb):
      
              global frac,tsize,size,save_location
      
              try:
      
                  frac= float(existing)/float(total)
                  self.progressBar.setValue(totalfrac)
                  QApplication.processEvents()
                 
              except (ZeroDivisionError, RuntimeError, TypeError, NameError):
                  frac = 0
      
              self.textBrowser.append("Downloaded %d/%d %d%%" % (existing, total, totalfrac))
      
      
              if frac ==1.0:
                  self.textBrowser.append("")
                  size = os.path.getsize(save_location)
                  tsize= (size /1024 /1024)
                  QMessageBox.information(self,"Download Completed", "The Download is Finished and the size is %03.2f MB" %(tsize,))
                  self.textBrowser.append('Size of file is %03.2f MB' %(tsize,))
                  self.progressBar.setValue(0)
                  self.lineEdit.setText('')
                  self.lineEdit_2.setText('')
                  QMessageBox.close(self)
      
      
              else:
                  self.textBrowser.append("Downloaded %d/%d %d%%" % (existing, total, totalfrac))
      
      
          def curl_limit_rate(self,rate_limit):
              global tsize,size,save_location
              url= self.lineEdit.text()
              save_location = self.lineEdit_2.text()
              if len(url) == 0 and len(save_location) == 0:
                  QMessageBox.information(self, "Error", "Please put the links")
                  return
              if len(url) > 0 and len(save_location) == 0:
                  QMessageBox.information(self, "Error", "Please put the location")
                  return
      
              if len(url) == 0 and len(save_location) > 0:
                  QMessageBox.information(self, "Error", "Please put the link")
                  return
      
              if len(url) > 0 and len(save_location) > 0:
      
                  c = pycurl.Curl()
                  c.setopt(pycurl.CAINFO, certifi.where())
                  c.setopt(c.URL,url)
                  c.setopt(c.MAX_RECV_SPEED_LARGE, rate_limit)
                  if os.path.exists(save_location):
                      file_id = open(save_location, "ab")
                      c.setopt(c.RESUME_FROM, os.path.getsize(save_location))
                  else:
                      file_id = open(save_location, "wb")
      
                  c.setopt(c.WRITEDATA, file_id)
                  c.setopt(c.NOPROGRESS, 0)
                  c.setopt(c.PROGRESSFUNCTION, self.curl_progress)
                  c.perform()
                  c.close()
              else:
                  QMessageBox.information(self, "Error", "Unknown error!")
      
      
      

      The picture

      progressBarU.jpg

      Many thanks in advance,

      [edit: fixed text paragraph SGaist]

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @monamour Hod do you resume?
      If existing/total are correct then it should just work.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      monamourM 1 Reply Last reply
      0
      • jsulmJ jsulm

        @monamour Hod do you resume?
        If existing/total are correct then it should just work.

        monamourM Offline
        monamourM Offline
        monamour
        wrote on last edited by monamour
        #3

        @jsulm
        By using this function curl_limit_rate(self,rate_limit) it will resume without any problem but the progressbar will start from 0.

        If the video file size is 100MB and download just 50MB in the same time the progressbar stops in 50% as well . However, once I start the download again it will continue from 50MB by the function above and in the same time the progressbar starts from 0%. So, the progressbar is not sync with resume function, this what I want.

        jsulmJ 1 Reply Last reply
        0
        • monamourM monamour

          @jsulm
          By using this function curl_limit_rate(self,rate_limit) it will resume without any problem but the progressbar will start from 0.

          If the video file size is 100MB and download just 50MB in the same time the progressbar stops in 50% as well . However, once I start the download again it will continue from 50MB by the function above and in the same time the progressbar starts from 0%. So, the progressbar is not sync with resume function, this what I want.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @monamour How is curl_progress called? Is it a callback/slot?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          monamourM 1 Reply Last reply
          0
          • jsulmJ jsulm

            @monamour How is curl_progress called? Is it a callback/slot?

            monamourM Offline
            monamourM Offline
            monamour
            wrote on last edited by
            #5

            @jsulm By pycurl, callbacks are defined using the setopt() method for Curl objects with options "PROGRESSFUNCTION" .

            jsulmJ 1 Reply Last reply
            0
            • monamourM monamour

              @jsulm By pycurl, callbacks are defined using the setopt() method for Curl objects with options "PROGRESSFUNCTION" .

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @monamour Then I guess you will need to write some code for that (unless there is a way to tell libcurl to not to reset progress). You will need to save last total/existing and use them also to calculate frac.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              monamourM 2 Replies Last reply
              0
              • jsulmJ jsulm

                @monamour Then I guess you will need to write some code for that (unless there is a way to tell libcurl to not to reset progress). You will need to save last total/existing and use them also to calculate frac.

                monamourM Offline
                monamourM Offline
                monamour
                wrote on last edited by
                #7

                @jsulm Ok I will try. Many thanks,

                1 Reply Last reply
                0
                • jsulmJ jsulm

                  @monamour Then I guess you will need to write some code for that (unless there is a way to tell libcurl to not to reset progress). You will need to save last total/existing and use them also to calculate frac.

                  monamourM Offline
                  monamourM Offline
                  monamour
                  wrote on last edited by
                  #8

                  @jsulm I have tried but no success. May you give me the clue please?

                  Thanks,

                  1 Reply Last reply
                  0
                  • monamourM Offline
                    monamourM Offline
                    monamour
                    wrote on last edited by
                    #9

                    @Denni-0 The code that I've posted is the last update because I don't know what is the logic I have to use and update it in the previous code.

                    Actually. the part that related to the resume the progress bar is not in the code.

                    1 Reply Last reply
                    0
                    • monamourM Offline
                      monamourM Offline
                      monamour
                      wrote on last edited by
                      #10

                      @Denni-0 It has been solved in [https://stackoverflow.com/questions/59630587/progressbar-resume-from-where-it-stopped/59633636#59633636]

                      Many thanks,

                      J.HilkJ 1 Reply Last reply
                      2
                      • monamourM monamour

                        @Denni-0 It has been solved in [https://stackoverflow.com/questions/59630587/progressbar-resume-from-where-it-stopped/59633636#59633636]

                        Many thanks,

                        J.HilkJ Offline
                        J.HilkJ Offline
                        J.Hilk
                        Moderators
                        wrote on last edited by
                        #11

                        @monamour
                        well, great! Thanks for sharing the answer as well, don't forget to use the topic tools to set the topic to solved!


                        Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                        Q: What's that?
                        A: It's blue light.
                        Q: What does it do?
                        A: It turns blue.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved