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. Downloading multiple files
QtWS25 Last Chance

Downloading multiple files

Scheduled Pinned Locked Moved General and Desktop
3 Posts 3 Posters 3.8k 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.
  • N Offline
    N Offline
    niftybytes
    wrote on last edited by
    #1

    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("http://code.jquery.com/mobile/1.0a3/jquery.mobile-1.0a3.min.js");
    QObject::connect(mNetworkManager, SIGNAL(finished(QNetworkReply*)), this, SLOT(createMobile(QNetworkReply*)));
    QNetworkReply* replyM = mNetworkManager->get(QNetworkRequest(urlM));
    urlJ = QUrl("http://code.jquery.com/jquery-1.5.min.js");
    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();
    if(!replyString.isNull())
    {
    QFile jquery(projectPath+"/payload/web/jquery/jquery.mobile-1.0a4.1.js");
    jquery.open(QIODevice::WriteOnly | QIODevice::Text);
    QTextStream out(&jquery);
    out << replyString;
    }
    reply->deleteLater();
    }@

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

    Thanks in advance for the help.

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Taamalus
      wrote on last edited by
      #2

      http://www.java2s.com/Code/Cpp/Qt/DownloadfromURL.htm

      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.

      ... time waits for no one. - Henry

      1 Reply Last reply
      0
      • G Offline
        G Offline
        goetz
        wrote on last edited by
        #3

        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.

        http://www.catb.org/~esr/faqs/smart-questions.html

        1 Reply Last reply
        0

        • Login

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