I can't seem to get this to work.
.h
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include <string>
#include <Windows.h>
#include <qstring.h>
{
Q_OBJECT
public:
explicit downloader
(QObject *parent
= 0);
void Do_download();
std::string getExePath();
void Do_parse(bool download, QNetworkReply *reply);
public slots:
void replyFinished (QNetworkReply *reply);
private:
QNetworkAccessManager *manager;
bool downloadSuccess;
};
#endif // DOWNLOADER_H
#ifndef DOWNLOADER_H
#define DOWNLOADER_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QFile>
#include <QDebug>
#include <QMessageBox>
#include <string>
#include <Windows.h>
#include <qstring.h>
class downloader : public QObject
{
Q_OBJECT
public:
explicit downloader(QObject *parent = 0);
void Do_download();
std::string getExePath();
void Do_parse(bool download, QNetworkReply *reply);
public slots:
void replyFinished (QNetworkReply *reply);
private:
QNetworkAccessManager *manager;
bool downloadSuccess;
};
#endif // DOWNLOADER_H
To copy to clipboard, switch view to plain text mode
i added a bool variable, to check if download passed, and a Do_parse function to call afterwards.
.cpp
#include "downloader.h"
#include "strings.h"
#include <string>
#include <Windows.h>
downloader
::downloader(QObject *parent
) :{
downloadSuccess = false;
}
void downloader::Do_download()
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this,SLOT(replyFinished(QNetworkReply*)));
manager
->get
(QNetworkRequest
(QUrl("http://www.hnb.hr/tecajn/hvazeca.htm")));
Do_parse(downloadSuccess, reply); // this doesn't work
}
void downloader::replyFinished(QNetworkReply *reply)
{
if(reply->error())
{
msgBox.setText(strings::msgBoxDownReplyError);
msgBox.exec();
downloadSuccess = false;
}
else
{
downloadSuccess = true;
}
}
void downloader::Do_parse(bool download, QNetworkReply *reply)
{
if(download==true)
{
{
file->write(data);
file->flush();
file->close();
}
delete file;
reply->deleteLater();
}
}
#include "downloader.h"
#include "strings.h"
#include <string>
#include <Windows.h>
downloader::downloader(QObject *parent) :
QObject(parent)
{
downloadSuccess = false;
}
void downloader::Do_download()
{
manager = new QNetworkAccessManager(this);
connect(manager, SIGNAL(finished(QNetworkReply*)),
this,SLOT(replyFinished(QNetworkReply*)));
manager->get(QNetworkRequest(QUrl("http://www.hnb.hr/tecajn/hvazeca.htm")));
Do_parse(downloadSuccess, reply); // this doesn't work
}
void downloader::replyFinished(QNetworkReply *reply)
{
if(reply->error())
{
QMessageBox msgBox;
msgBox.setText(strings::msgBoxDownReplyError);
msgBox.exec();
downloadSuccess = false;
}
else
{
downloadSuccess = true;
}
}
void downloader::Do_parse(bool download, QNetworkReply *reply)
{
if(download==true)
{
QFile *file = new QFile(strings::filePathQt);
QByteArray data = reply->readAll();
if(file->open(QIODevice::ReadWrite | QIODevice::Truncate | QIODevice::Text))
{
file->write(data);
file->flush();
file->close();
}
delete file;
reply->deleteLater();
}
}
To copy to clipboard, switch view to plain text mode
I thought of something like this. but i guess i'm missing something about the reply thing.
Am i on the right track here?
Is it possible to send the reply into another function like i tried in my code?
Bookmarks