Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Downloading multiple files
niftybytes last edited by
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);
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)
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();
jquery.open(QIODevice::WriteOnly | QIODevice::Text);
out << replyString;
It creates the file for the second slot but does not download the contents.
Thanks in advance for the help.
Taamalus last edited by
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.
goetz last edited by
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.