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("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.
-
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.
-
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.