PDA

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



Alir3z4
14th February 2012, 22:25
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'


QString Status::remainingTime() const
{

if(_downloadRate){
qDebug() << _downloadRate << "###" << _totalLength << "###" << _completedLength;
int speed = _downloadRate;
int remLength = _totalLength - _completedLength;
int remSecond = remLength/speed;
int hour = remSecond/3600;
remSecond = remSecond%3600;
int minute = remSecond/60;
remSecond = remSecond%60;
return QTime(hour, minute, remSecond).toString();
}

return QString("n/a");

}

QString Status::downloadRate() const
{
if(_downloadRate){
int rate = _downloadRate/1024;
if(rate < 1)
return QString(tr("%1Kb/s").arg(rate));
else if(rate >= 1)
return QString(tr("%1Mb/s").arg(rate));
}
return QString(tr("n/a"));
}

void Status::updateFileStatus(qint64 bytesReceived, qint64 bytesTotal)
{
qDebug() << _startTime->elapsed();
if(!_totalLength)
_totalLength = bytesTotal;
_completedLength = bytesReceived;
_progress = _completedLength*100/_totalLength;
_downloadRate = _completedLength / _startTime->elapsed();
}

wysota
14th February 2012, 23:19
What does bytesTotal contain?

Alir3z4
14th February 2012, 23:23
From the Qt doc :D

bytesTotal indicates the total number of bytes expected to be downloaded
I told so, i connected to QNetworkReply::downloadProgress ( qint64 bytesReceived, qint64 bytesTotal ) [signal] (http://developer.qt.nokia.com/doc/qt-4.8/qnetworkreply.html#downloadProgress)

wysota
14th February 2012, 23:32
No, you don't understand. I'm asking about the exact value you get in this variable. Is it by any chance -1?

Alir3z4
14th February 2012, 23:37
No, it's not.
It's exact file size!
To insure myself about that, also i put

qDebug() << _downloadRate << "###" << _totalLength << "###" << _completedLength;
to keep eye on it :|
Is it becuase of the type [qint64] which i get them from the downoadProgress ?

wysota
15th February 2012, 10:32
And how do you calculate the download rate?

Alir3z4
15th February 2012, 13:30
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.

wysota
15th February 2012, 14:51
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?