Get real upload progress in QT
-
wrote on 31 Dec 2014, 09:12 last edited by
So I want to upload a file using this code in qt:
@QFile *file = new QFile(this->itemsToUpload.at(index));
QUrl url("http://leonardogalli.ch/beta/upload_single.php");
QNetworkRequest request(url);QHttpMultiPart *multiPart = new QHttpMultiPart(QHttpMultiPart::FormDataType); QHttpPart loginPart; /* password */ loginPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"password\"")); loginPart.setBody("pass"); multiPart->append(loginPart); loginPart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"path\"")); loginPart.setBody(this->paths.at(index).toLocal8Bit()); qDebug() << this->paths.at(index).toLocal8Bit(); multiPart->append(loginPart); QHttpPart filePart; /* important that the files[] variable have the brackets, for PHP to interpret correctly */ filePart.setHeader(QNetworkRequest::ContentDispositionHeader, QVariant("form-data; name=\"uploaded\"; filename=\""+ file->fileName() + "\"")); qDebug() << "form-data; name=\"file1\"; filename=\""+ file->fileName() + "\""; file->open(QIODevice::ReadOnly); //qDebug() << file->readAll(); filePart.setBodyDevice(file); file->setParent(multiPart); // we cannot delete the file now, so delete it with the multiPart multiPart->append(filePart); QNetworkReply* reply = networkManager->post(request, multiPart); this->currentReply = reply; multiPart->setParent(reply); // delete the multiPart with the reply QObject::connect(reply,SIGNAL(uploadProgress(qint64,qint64)), this, SLOT(uploadProgress(qint64,qint64))); QObject::connect(reply, SIGNAL(finished()), this, SLOT(finished())); QObject::connect(reply, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(downloadProgress(qint64,qint64)));
}
void UploadManager::uploadProgress(qint64 bytesSent, qint64 bytesTotal)
{
if(bytesSent!=0 && bytesTotal != 0)
{
qDebug() << "Uploaded:" << bytesSent << " bytes of total: " << bytesTotal << "diff to total: " << bytesTotal-this->totalSize;
qint64 bytesDiff = bytesSent - this->lastUlSize;
this->UlSize += bytesDiff;
this->lastUlSize = bytesSent;
int timeDiff = QDateTime::currentMSecsSinceEpoch()/1000-this->time;
if(timeDiff!= 0 && bytesSent!=0 && bytesTotal != 0)
{
qDebug() << "Uploadspeed: " << ((this->UlSize/timeDiff));qint64 remainingUl = this->totalSize - this->UlSize; qint64 speed = ((this->UlSize/timeDiff)); //Download Speed in B/s int remainingTimeSec = remainingUl/speed; qDebug() << "Remaining time: " << this->readableTime(remainingTimeSec) << " percentage: " << (this->UlSize/this->totalSize)*100; float percentage = ((float)this->UlSize/(float)this->totalSize)*100; emit uploadProg(this->niceSpeed(speed), this->readableTime(remainingTimeSec), percentage); } }
}@
The problem is there is a really long delay between the last uploadProgress signal and the finished signal. According to "this":http://stackoverflow.com/questions/24363568/qt-qnetworkaccessmanager-long-delay-to-emit-finished-signal question this is due to the uploadProgress not being the progress when a chunk of data has finished uploading. So how would I get the real uploadProgress, when a chunk is finished uploading?
Update 1:
On mac this somehow works perfectly, so why wont it work on windows?
1/1