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. Get real upload progress in QT
Forum Update on Monday, May 27th 2025

Get real upload progress in QT

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 744 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.
  • D Offline
    D Offline
    DJ_Lectr0
    wrote on 31 Dec 2014, 09:12 last edited by
    #1

    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 Reply Last reply
    0

    1/1

    31 Dec 2014, 09:12

    • Login

    • Login or register to search.
    1 out of 1
    • First post
      1/1
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved