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();
}
}
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
));
}
}
void Status::updateFileStatus(qint64 bytesReceived, qint64 bytesTotal)
{
qDebug() << _startTime->elapsed();
if(!_totalLength)
_totalLength = bytesTotal;
_completedLength = bytesReceived;
_progress = _completedLength*100/_totalLength;
_downloadRate = _completedLength / _startTime->elapsed();
}
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();
}
To copy to clipboard, switch view to plain text mode
Bookmarks