Congratulations to our 2022 Qt Champions!

Downloading multiple files

  • Hello, I am confused on how to use QNetworkAccessManger to download multiple files from the web. The code I have now is:
    @ QNetworkAccessManager* mNetworkManager = new QNetworkAccessManager(this);
    QUrl urlM;
    QUrl urlJ;
    urlM = QUrl("");
    QObject::connect(mNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(createMobile(QNetworkReply*)));
    QNetworkReply* replyM = mNetworkManager->get(QNetworkRequest(urlM));
    urlJ = QUrl("");
    QObject::connect(mNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(createJquery(QNetworkReply*)));
    QNetworkReply* replyJ = mNetworkManager->get(QNetworkRequest(urlJ));@

    I had it working when the function only had to deal with one.

    My slot is the same for both createMobile and createJquery with some spots changed for saving.

    @void ProjectMgr::createMobile(QNetworkReply* reply)
    QString replyString;
    if(reply->error() == QNetworkReply::NoError)
    //Assuming this is a human readable file replyString now contains the file
    replyString = QString::fromUtf8(reply->readAll().data());
    Project *current = getCurrentProject();
    QString projectPath = current->getProjectPath();
    QFile jquery(projectPath+"/payload/web/jquery/"); | QIODevice::Text);
    QTextStream out(&jquery);
    out << replyString;

    It creates the file for the second slot but does not download the contents.

    Thanks in advance for the help.


    I have no idea if this will help you, yet it gives the basic approach for downloading files using the NetworkManager. Right now I don't see where the code asks for the file transfer, and am also very soft on jquery.

  • You connect both slots to a single signal, so as soon as one of your downloads has finished, both slots are called!

    The correct way would be to create just one slot, connected to signal finished and save the QNetworkReply pointer returned by QNAM.get() in a class attribute. In your slot compare which request finished (the reply pointer is set in the signal and delivered to the slot) and put the bytes into the respective file.

Log in to reply