Results 1 to 8 of 8

Thread: Correct this code if im doing it wrong to calculate remainingTime of download a file

  1. #1
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Exclamation Correct this code if im doing it wrong to calculate remainingTime of download a file

    I tried to get remainingTime of downloading a file, as you can see from the source code below, i set some variables on updateFileStatus SLOT which i connected to updateProgress signal from QNetworkReply.
    The problem is, the result fo the remainingTime is nor correct, it's return wrong time like 20:32:17 ... [QTime string format] // when i return the integer values of hour, minute, remSecond it's something like empty/null/uninitialized variable'

    Qt Code:
    1. QString Status::remainingTime() const
    2. {
    3.  
    4. if(_downloadRate){
    5. qDebug() << _downloadRate << "###" << _totalLength << "###" << _completedLength;
    6. int speed = _downloadRate;
    7. int remLength = _totalLength - _completedLength;
    8. int remSecond = remLength/speed;
    9. int hour = remSecond/3600;
    10. remSecond = remSecond%3600;
    11. int minute = remSecond/60;
    12. remSecond = remSecond%60;
    13. return QTime(hour, minute, remSecond).toString();
    14. }
    15.  
    16. return QString("n/a");
    17.  
    18. }
    19.  
    20. QString Status::downloadRate() const
    21. {
    22. if(_downloadRate){
    23. int rate = _downloadRate/1024;
    24. if(rate < 1)
    25. return QString(tr("%1Kb/s").arg(rate));
    26. else if(rate >= 1)
    27. return QString(tr("%1Mb/s").arg(rate));
    28. }
    29. return QString(tr("n/a"));
    30. }
    31.  
    32. void Status::updateFileStatus(qint64 bytesReceived, qint64 bytesTotal)
    33. {
    34. qDebug() << _startTime->elapsed();
    35. if(!_totalLength)
    36. _totalLength = bytesTotal;
    37. _completedLength = bytesReceived;
    38. _progress = _completedLength*100/_totalLength;
    39. _downloadRate = _completedLength / _startTime->elapsed();
    40. }
    To copy to clipboard, switch view to plain text mode 
    ...یه مرد هیچوقت زمین نمیخوره

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Correct this code if im doing it wrong to calculate remainingTime of download a f

    What does bytesTotal contain?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Correct this code if im doing it wrong to calculate remainingTime of download a f

    From the Qt doc
    bytesTotal indicates the total number of bytes expected to be downloaded
    I told so, i connected to QNetworkReply::downloadProgress ( qint64 bytesReceived, qint64 bytesTotal ) [signal]
    ...یه مرد هیچوقت زمین نمیخوره

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Correct this code if im doing it wrong to calculate remainingTime of download a f

    No, you don't understand. I'm asking about the exact value you get in this variable. Is it by any chance -1?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Correct this code if im doing it wrong to calculate remainingTime of download a f

    No, it's not.
    It's exact file size!
    To insure myself about that, also i put
    Qt Code:
    1. qDebug() << _downloadRate << "###" << _totalLength << "###" << _completedLength;
    To copy to clipboard, switch view to plain text mode 
    to keep eye on it :|
    Is it becuase of the type [qint64] which i get them from the downoadProgress ?
    Last edited by Alir3z4; 15th February 2012 at 00:22.
    ...یه مرد هیچوقت زمین نمیخوره

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Correct this code if im doing it wrong to calculate remainingTime of download a f

    And how do you calculate the download rate?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2011
    Posts
    69
    Thanks
    13
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Correct this code if im doing it wrong to calculate remainingTime of download a f

    Quote Originally Posted by wysota View Post
    And how do you calculate the download rate?
    Stop asking weird question, i provide the source, what is it?
    Next i'm sure you gonna asking about my lip stick color :|
    //
    Anyway, i fixed it, i just mimic the minitube way to calculate downloadRate, remainingTime.
    ...یه مرد هیچوقت زمین نمیخوره

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Correct this code if im doing it wrong to calculate remainingTime of download a f

    Quote Originally Posted by Alir3z4 View Post
    Stop asking weird question, i provide the source, what is it?
    Anyway, i fixed it, i just mimic the minitube way to calculate downloadRate, remainingTime.
    So the question about how you calculate the rate wasn't so weird after all, was it?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. What's wrong with my code?
    By Dave2011 in forum Newbie
    Replies: 4
    Last Post: 12th August 2011, 23:35
  2. What is wrong with this code?
    By Jeffb in forum Newbie
    Replies: 6
    Last Post: 18th February 2011, 08:35
  3. Download Source Code
    By psih128 in forum Newbie
    Replies: 3
    Last Post: 6th July 2009, 11:01
  4. What is wrong with my code?
    By Dante in forum Qt Programming
    Replies: 27
    Last Post: 6th April 2009, 08:56
  5. Replies: 0
    Last Post: 6th March 2009, 09:19

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.