How to make a progress bar for downloads?



  • 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]



  • 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 %



  • @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 :-)



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





  • @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 :-)



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



  • ```
    

    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?

  • Lifetime Qt Champion

    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.



  • @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?


  • Lifetime Qt Champion

    Meaning ?



  • @SGaist Where i did wrong in that code?


  • Lifetime Qt Champion

    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.



  • @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*)));
        
    ``

  • Lifetime Qt Champion

    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.



  • Thanks for all
    My problem solved.


  • Lifetime Qt Champion

    What was the problem ?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.