I work in a team to develop a QT Application with c . Among other things, the app needs to download files from the internet. Here is the code I wrote to download files:
int main(int argc, char *argv[]){
QCoreApplication app(argc, argv);
QNetworkAccessManager man;
std::string urlc = "https://upload.wikimedia.org/wikipedia/commons/7/73/Flat_tick_icon.svg"; //Ramdom svg file
QUrl url(QString::fromStdString(urlc));
QNetworkRequest req(url);
QNetworkReply* reply = man.get(req);
QObject::connect(reply, &QNetworkReply::finished, [department, region, &reply](){
QByteArray read = reply->readAll();
QString savefile = QString::fromStdString("file.png");
QFile out(savefile);
out.open(QIODevice::WriteOnly);
out.write(read);
out.close();
reply->close();
reply->deleteLater();
app.quit();
});
return app.exec();
}
The above code works well and downloads the file at the specified url. However, if I try to extract the downloading of files to a separate function (see code below), the program doesn't work. In fact, it never even starts downloading the file.
std::string download_file(){
QNetworkAccessManager man;
std::string urlc = "https://upload.wikimedia.org/wikipedia/commons/7/73/Flat_tick_icon.svg"; //Ramdom svg file
QUrl url(QString::fromStdString(urlc));
QNetworkRequest req(url);
QNetworkReply* reply = man.get(req);
QObject::connect(reply, &QNetworkReply::finished, [department, region, &reply](){
QByteArray read = reply->readAll();
QString savefile = QString::fromStdString("file.png");
QFile out(savefile);
out.open(QIODevice::WriteOnly);
out.write(read);
out.close();
reply->close();
reply->deleteLater();
});
return "file.png";
}
int main(int argc, char *argv[]){
QCoreApplication app(argc, argv);
std::string path = download_file();
std::cout << path << std::endl;
return app.exec();
}
What can be the reason for such behaviour?
CodePudding user response:
The difference is that QNetworkAccessManager has a greater scope in the first case, as opposed to the second case, which is only a local variable, so it will be destroyed at the attempt. One way to solve is to create a class that handles all the logic:
#include <QCoreApplication>
#include <QFile>
#include <QNetworkAccessManager>
#include <QNetworkReply>
class Downloader: public QObject{
Q_OBJECT
public:
Downloader(QObject *parent=nullptr):QObject(parent){
connect(&m_manager, &QNetworkAccessManager::finished, this, &Downloader::handle_finished);
}
void download(const QUrl & url, const QString & filename){
QNetworkRequest request;
request.setUrl(url);
request.setAttribute(QNetworkRequest::User, filename);
m_manager.get(request);
}
Q_SIGNAL void finished(bool);
private:
void handle_finished(QNetworkReply *reply){
bool ok = false;
if(reply->error() == QNetworkReply::NoError){
QByteArray read = reply->readAll();
QString filename = reply->request().attribute(QNetworkRequest::User).toString();
QFile out(filename);
if(out.open(QIODevice::WriteOnly)){
out.write(read);
out.close();
ok = true;
}
}
else{
qDebug() << reply->error() << reply->errorString();
}
reply->deleteLater();
Q_EMIT finished(ok);
}
QNetworkAccessManager m_manager;
};
#include "main.moc"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Downloader downloader;
QObject::connect(&downloader, &Downloader::finished, [](bool sucess){
qDebug() << "download" << sucess;
});
downloader.download(QUrl("https://upload.wikimedia.org/wikipedia/commons/7/73/Flat_tick_icon.svg"), "file.svg");
return a.exec();
}