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. Progress bar issue for Http request

Progress bar issue for Http request

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 3 Posters 624 Views
  • 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.
  • V Offline
    V Offline
    vsukhwal
    wrote on last edited by
    #1

    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
    
        homeObj->progressBar->setMaximum(total);
    
        homeObj->progressBar->setValue(read);
    

    }

    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.

    Thanks,
    Vidushi

    1 Reply Last reply
    0
    • O Offline
      O Offline
      Oleksandr Malyushytskyy
      wrote on last edited by Oleksandr Malyushytskyy
      #2

      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

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

        Hi,

        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.

        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