Important: Please read the Qt Code of Conduct -

Downloading zip using QNetworkAccessManager

  • Hello, I have a code that downloades a zip with the QNetworkAccessManager.
    The code works fine on my machine, but on another machine the bytesTotal is only 44kb, but the file is 15mb.
    Also on my machine the content-length header returns 15mb, but on his machine the messagebox showing the content-lenght is empty so the content-length is not received.

    I tried messing around with setting different rawHeader options, like accept-encoding gzip, identity etc.
    For me it works even without the rawHeader but on the other machine I can't get it to work.

    void Installer::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
            content_length = bytesTotal;
            emit setDownloadSize(bytesTotal);
                char bytes[256]="";
                sprintf(bytes, "%s %d(%d)\n%f MB/s", package, (DWORD)bytesReceived, (DWORD)bytesTotal, (double)((double)bytesReceived/1000)/(double)((double)timer.elapsed()));
                emit downloadProgress(bytesReceived);
                emit setDownloadInfo(bytes);
    bool showMessage=true;
    void Installer::onNewDataDownloaded()
        auto reply = qobject_cast<QNetworkReply*>(sender());
            if (!reply)
            QByteArray arr = reply->readAll();
            char* data =;
            DWORD count = arr.count();
                MessageBoxA(0, reply->rawHeader("content-length"), reply->rawHeader("content-length"), 0);
            body.insert(body.end(), data, data+count);
    void Installer::DownloadFileFromHTTP(char* url)
            MessageBoxA(0, url, "Download URL", 0);
            request = QNetworkRequest((QUrl(url)));
            request.setSslConfiguration(QSslConfiguration::defaultConfiguration()); // HTTPS
            request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:47.0) Gecko/20100101 Firefox/47.0");
            request.setRawHeader("Accept-Encoding", "identity");
            request.setRawHeader("Accept", "*/*");
            //request.setRawHeader("Transfer-Encoding", "identity");
            request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::NoLessSafeRedirectPolicy);
            QNetworkReply * reply = download_thread.get(request);
            connect(reply, SIGNAL(readyRead()), this, SLOT(onNewDataDownloaded()));
            connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(onDownloadProgress(qint64, qint64)));
            connect(reply, SIGNAL(finished()), this, SLOT(onFinishedDownloading()), Qt::UniqueConnection);
            if (!reply)
                MessageBoxA(0, "No reply", "", 0);

  • Lifetime Qt Champion

    @Vadru said in Downloading zip using QNetworkAccessManager:


    Don't mix WinAPI with Qt when not needed. Also the code is incomplete (e.g. what should be 'download_thread') and passing a char* pointer to a stack around in a signal is ... strange and error prone.

Log in to reply