The question is i can use one outside class with qobject and download some files of my webpage without use one only instance. I am trying to update the code because i dont want to use singleton i will need more instances. And i am trying to update this code to use qobject to connect my external class and use different instances not only one. And if it possible to pass some arguments like one string to say where i download the file.
Code:
Main.cpp:
```
#include <QCoreApplication>
#include <QDebug>
#include "downloader.h"
int main(int argc, char *argv[])
{
QObject::connect(&Downloader
::instance(),
&Downloader
::testSignal,
[](){
QString s
="http://myweb/currxml.php";
});
return a.exec();
}
```
download.h:
```
#ifndef Downloader_H
#define Downloader_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QDateTime>
#include <QFile>
#include <QDebug>
{
Q_OBJECT
Q_DISABLE_COPY(Downloader)
public:
static Downloader &instance();
explicit Downloader
(QObject *parent
= nullptr
);
//virtual ~Downloader(){}
void doSomething();
signals:
void testSignal();
public slots:
//void testSlot();
void replyFinished (QNetworkReply *reply);
private:
QNetworkAccessManager *manager;
};
#endif // Downloader_H
```
downloader.cpp
```
#include "downloader.h"
#include <QDebug>
Downloader &Downloader::instance()
{
static Downloader _instance;
return _instance;
}
{
doSomething();
}
void Downloader::doSomething()
{
//emit testSignal();
qDebug() << "It's working!!!!";
manager = new QNetworkAccessManager(this);
manager
->get
(QNetworkRequest
(QUrl(s
)));
emit instance().testSignal();
}
void Downloader::replyFinished (QNetworkReply *reply)
{
if(reply->error())
{
qDebug() << "ERROR!";
qDebug() << reply->errorString();
}
else
{
//qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
//qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
//qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
//qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
//qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
QString p
= reply
->request
().
url().
fileName();
if(file
->open
(QFile::Append)) {
file->write(reply->readAll());
file->flush();
file->close();
}
delete file;
}
reply->deleteLater();
}
```
Main.cpp:
```
#include <QCoreApplication>
#include <QDebug>
#include "downloader.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QObject::connect(&Downloader::instance(),&Downloader::testSignal,
[](){
QString s="http://myweb/currxml.php";
});
return a.exec();
}
```
download.h:
```
#ifndef Downloader_H
#define Downloader_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkRequest>
#include <QNetworkReply>
#include <QUrl>
#include <QDateTime>
#include <QFile>
#include <QDebug>
class Downloader : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(Downloader)
public:
static Downloader &instance();
explicit Downloader(QObject *parent = nullptr);
QString s;
//virtual ~Downloader(){}
void doSomething();
signals:
void testSignal();
public slots:
//void testSlot();
void replyFinished (QNetworkReply *reply);
private:
QNetworkAccessManager *manager;
};
#endif // Downloader_H
```
downloader.cpp
```
#include "downloader.h"
#include <QDebug>
Downloader &Downloader::instance()
{
static Downloader _instance;
return _instance;
}
Downloader::Downloader(QObject *parent) : QObject(parent)
{
doSomething();
}
void Downloader::doSomething()
{
//emit testSignal();
qDebug() << "It's working!!!!";
manager = new QNetworkAccessManager(this);
manager->get(QNetworkRequest(QUrl(s)));
emit instance().testSignal();
}
void Downloader::replyFinished (QNetworkReply *reply)
{
if(reply->error())
{
qDebug() << "ERROR!";
qDebug() << reply->errorString();
}
else
{
//qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
//qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
//qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
//qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
//qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
QString p = reply->request().url().fileName();
QFile *file = new QFile("C:/Users/moh/"+p);
if(file->open(QFile::Append))
{
file->write(reply->readAll());
file->flush();
file->close();
}
delete file;
}
reply->deleteLater();
}
```
To copy to clipboard, switch view to plain text mode
Bookmarks