Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to make a progress bar for downloads?
Forum Updated to NodeBB v4.3 + New Features

How to make a progress bar for downloads?

Scheduled Pinned Locked Moved Solved General and Desktop
17 Posts 4 Posters 10.7k 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.
  • A Offline
    A Offline
    Armin
    wrote on last edited by A Former User
    #1

    Hi,
    I have writen a program that will download any thing i throw at it .
    but i'm little stuck now , i just don't know how to put progress bar for it, i just simply don't understand it.
    Can you give me some explanation or guide or anything?
    Thanks.

    [Edit: Added a meaningful title -- @Wieland]

    J.HilkJ 1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      if you are using QNetworkReply you can use networkReply->header(QNetworkRequest::ContentLengthHeader).toInt() to get the maximum size in bytes and then divide the size of what you already downloaded by this number to get the progress in %

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      4
      • A Armin

        Hi,
        I have writen a program that will download any thing i throw at it .
        but i'm little stuck now , i just don't know how to put progress bar for it, i just simply don't understand it.
        Can you give me some explanation or guide or anything?
        Thanks.

        [Edit: Added a meaningful title -- @Wieland]

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

        @Armin
        hi,

        You don't give us much information here :-)

        But I'll just asume, you use QT's Networkaccess Manager to do your downloading:
        So I'll post a section from my own project.

        First: You get the SIGNAL of your Networkreply:

         QNetworkReply *reply = NetworkManager->get(downloadRequest);
        
            connect(reply, &QNetworkReply::downloadProgress,this, &NetworkAccess::downloadProgress);
        
        

        Second: In your downloadProgess slot you set the values of your Progressbar:

        void NetworkAccess::downloadProgress(qint64 ist, qint64 max)
        {
            pBar->setRange(0,max);
            pBar->setValue(ist);
            if(max < 0) hideProgress();
        }
        

        simple and straight forward :-)


        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.

        A 2 Replies Last reply
        4
        • J.HilkJ J.Hilk

          @Armin
          hi,

          You don't give us much information here :-)

          But I'll just asume, you use QT's Networkaccess Manager to do your downloading:
          So I'll post a section from my own project.

          First: You get the SIGNAL of your Networkreply:

           QNetworkReply *reply = NetworkManager->get(downloadRequest);
          
              connect(reply, &QNetworkReply::downloadProgress,this, &NetworkAccess::downloadProgress);
          
          

          Second: In your downloadProgess slot you set the values of your Progressbar:

          void NetworkAccess::downloadProgress(qint64 ist, qint64 max)
          {
              pBar->setRange(0,max);
              pBar->setValue(ist);
              if(max < 0) hideProgress();
          }
          

          simple and straight forward :-)

          A Offline
          A Offline
          Armin
          wrote on last edited by
          #4

          @J.Hilk why in first connect it doesn't have argument?
          Can you explain more?
          Thanks

          VRoninV J.HilkJ 2 Replies Last reply
          0
          • A Armin

            @J.Hilk why in first connect it doesn't have argument?
            Can you explain more?
            Thanks

            VRoninV Offline
            VRoninV Offline
            VRonin
            wrote on last edited by
            #5

            @Armin https://wiki.qt.io/New_Signal_Slot_Syntax

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            1 Reply Last reply
            4
            • A Armin

              @J.Hilk why in first connect it doesn't have argument?
              Can you explain more?
              Thanks

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

              @Armin

              sure,

              connect(reply, &QNetworkReply::downloadProgress,this, &NetworkAccess::downloadProgress);
              

              is written in the new Syntax, introduced a while ago, can't remember the exact Qt 5.x version.

              its equivalent to

              connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
              

              I prefere the new over the old one. All the times I missed a bracket or added one to much..., I'm glad its now easier, at least for me :-)


              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
              3
              • J.HilkJ J.Hilk

                @Armin
                hi,

                You don't give us much information here :-)

                But I'll just asume, you use QT's Networkaccess Manager to do your downloading:
                So I'll post a section from my own project.

                First: You get the SIGNAL of your Networkreply:

                 QNetworkReply *reply = NetworkManager->get(downloadRequest);
                
                    connect(reply, &QNetworkReply::downloadProgress,this, &NetworkAccess::downloadProgress);
                
                

                Second: In your downloadProgess slot you set the values of your Progressbar:

                void NetworkAccess::downloadProgress(qint64 ist, qint64 max)
                {
                    pBar->setRange(0,max);
                    pBar->setValue(ist);
                    if(max < 0) hideProgress();
                }
                

                simple and straight forward :-)

                A Offline
                A Offline
                Armin
                wrote on last edited by
                #7

                @J.Hilk what are ist and max arguments ? where they come from?
                thnaks

                1 Reply Last reply
                0
                • A Offline
                  A Offline
                  Armin
                  wrote on last edited by
                  #8
                  ```
                  

                  connect(manager , SIGNAL(finished(QNetworkReply*)) , this , SLOT(replyFinished(QNetworkReply*)));
                  manager->get(QNetworkRequest(QUrl(downloadlink)));

                  
                  

                  void MainWindow::replyFinished(QNetworkReply *reply)
                  {
                  connect(reply, &QNetworkReply::downloadProgress,this, &MainWindow:: progressbar);
                  .
                  .
                  .
                  .

                  
                  Where i did wrong?
                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    Hi,

                    Aren't you connecting the downloadProgress signal once the reply is finished ? By finished it means that the query has completed so you are trying to get the download information after the download already happened.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    A 1 Reply Last reply
                    5
                    • SGaistS SGaist

                      Hi,

                      Aren't you connecting the downloadProgress signal once the reply is finished ? By finished it means that the query has completed so you are trying to get the download information after the download already happened.

                      A Offline
                      A Offline
                      Armin
                      wrote on last edited by
                      #10

                      @SGaist Thanks

                          QNetworkAccessManager *manager = new QNetworkAccessManager(this);
                          QNetworkReply *reply;
                          connect(manager , SIGNAL(downloadProgress(qint64,qint64)) , this , SLOT(progressbar(qint64,qint64)));
                          connect(manager , SIGNAL(finished(QNetworkReply*)) , this , SLOT(replyFinished(QNetworkReply*)));
                          manager->get(QNetworkRequest(QUrl(downloadlink)));
                      

                      What i do?

                      1 Reply Last reply
                      0
                      • SGaistS Offline
                        SGaistS Offline
                        SGaist
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Meaning ?

                        Interested in AI ? www.idiap.ch
                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                        A 1 Reply Last reply
                        1
                        • SGaistS SGaist

                          Meaning ?

                          A Offline
                          A Offline
                          Armin
                          wrote on last edited by
                          #12

                          @SGaist Where i did wrong in that code?

                          J.HilkJ 1 Reply Last reply
                          0
                          • SGaistS Offline
                            SGaistS Offline
                            SGaist
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            Since it's outside any context the only thing that can be said is that reply is not used and that you are likely going to have lots of memory eaten if you create a new QNetworkAccessManager per query and not deleting it afterward.

                            Interested in AI ? www.idiap.ch
                            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            1
                            • A Armin

                              @SGaist Where i did wrong in that code?

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

                              @Armin
                              hi,

                              in your sample, not the QNetworkAccessManager has the downloadProgress Signal, but the QNetworkReply

                              ->

                              QNetworkAccessManager *manager = new QNetworkAccessManager(this);
                                  QNetworkReply *reply = manager->get(QNetworkRequest(QUrl(downloadlink)));
                                  connect(reply, SIGNAL(downloadProgress(qint64,qint64)) , this , SLOT(progressbar(qint64,qint64)));
                                  connect(manager , SIGNAL(finished(QNetworkReply*)) , this , SLOT(replyFinished(QNetworkReply*)));
                                  
                              ``

                              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
                              4
                              • SGaistS Offline
                                SGaistS Offline
                                SGaist
                                Lifetime Qt Champion
                                wrote on last edited by
                                #15

                                To add to @J-Hilk which got the wrong connection I missed, you should provide more context on how you are using that code in your application.

                                Interested in AI ? www.idiap.ch
                                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                1 Reply Last reply
                                0
                                • A Offline
                                  A Offline
                                  Armin
                                  wrote on last edited by
                                  #16

                                  Thanks for all
                                  My problem solved.

                                  1 Reply Last reply
                                  0
                                  • SGaistS Offline
                                    SGaistS Offline
                                    SGaist
                                    Lifetime Qt Champion
                                    wrote on last edited by
                                    #17

                                    What was the problem ?

                                    Interested in AI ? www.idiap.ch
                                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                    1 Reply Last reply
                                    2

                                    • Login

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