for example first thing i am not doing my _instance like a pointer. And i dont initialize it the first time instance is called.
So i think this example that i did with instances is wrong.
Code:
#include <QCoreApplication>
#include <QDebug>
#include "downloader.h"
int main(int argc, char *argv[])
{
QObject::connect(Downloader
::instance(),Downloader
::testSignal,
{
});
return a.exec();
}
downloader.cpp:
#include "downloader.h"
#include <QDebug>
Downloader &Downloader::instance()
{
Downloader _instance;
return _instance;
}
{
doSomething();
}
void Downloader::doSomething()
{
//emit testSignal();
qDebug() << "It's working!!!!";
QString s
="http://myweb/currxml.php";
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();
}
downloader.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:
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
Code:
#include <QCoreApplication>
#include <QDebug>
#include "downloader.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QObject::connect(Downloader::instance(),Downloader::testSignal,
{
});
return a.exec();
}
downloader.cpp:
#include "downloader.h"
#include <QDebug>
Downloader &Downloader::instance()
{
Downloader _instance;
return _instance;
}
Downloader::Downloader(QObject *parent) : QObject(parent)
{
doSomething();
}
void Downloader::doSomething()
{
//emit testSignal();
qDebug() << "It's working!!!!";
QString s="http://myweb/currxml.php";
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();
}
downloader.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:
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
To copy to clipboard, switch view to plain text mode
Bookmarks