Qt World Summit: Submit your Presentation

Progress bar issue for Http request

  • Hello,

    I am currently having issues with the progress bar for http requests. Following is my code.

    QNetworkReply *reply;
    QNetworkAccessManager qnam;

        reply = qnam.get(QNetworkRequest(url));
        connect(reply, &QNetworkReply::finished, this, httpFinished); // Performs the function
          connect(reply,&QNetworkReply::downloadProgress,this, updateDownloadProgress);


    void updateDownloadProgress(qint64 read, qint64 total)
    qDebug() << read << total; // Problem is every time updateDownloadProgress is called, the total value is returned as -1 in comparison to read value, until the last call. For eg: 1. Read= 500, total=-1, 2. Read = 700, total = -1, 3. Read = 1000, total =1000) As a result, setMaximum of progress bar turns out to be -1 in the first 2 calls.

        Home* homeObj = qobject_cast<Home *>(this->parent()->parent()->parent()->parent()); // progress bar object in the parent class


    Could you please tell what is the reason for the above problem and how can it be solved? Also, the data being sent is dynamic in size and so I cannot hardcode the setMaximum beforehand.


  • The reason can be found in documentation:

    The bytesReceived parameter indicates the number of bytes received, while bytesTotal indicates the total number of bytes expected to be downloaded. If the number of bytes to be downloaded is not known, bytesTotal will be -1.

    The download is finished when bytesReceived is equal to bytesTotal. At that time, bytesTotal will not be -1.

    Basically above means that it is still reading. I do not think you are guaranteed to get a size from request

  • Lifetime Qt Champion


    On a side note, this line:

    Home* homeObj = qobject_cast<Home *>(this->parent()->parent()->parent()->parent());

    Is very very nasty. You have created a very tight coupling on 4 ancestors. This is really not a good design. You should forward the signal upper and update the progress bar from your Home object but not 4 levels down in a class that should not even care whether there's something connect to it's progress signal.

Log in to reply