Question about qobject and http
-
Hi i am trying to download files of my webpage with http. But i want send the string since qobject connection or is not possible? The other thing i'm doing with functions but maybe will better or can be do with signal/slots like always.
Code: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(); }
-
Hi,
First thing: do not create static QObject.
Make your _instance a pointer and initialize it the first time
instance
is called.Also, isn't this question the same as the one you posted here ?
-
Hi,
First thing: do not create static QObject.
Make your _instance a pointer and initialize it the first time
instance
is called.Also, isn't this question the same as the one you posted here ?
-
@SGaist one question this can be a good way?
class://GlobalClass.h class GlobalClass { public: static GlobalClass* get() { if ( m_instance == nullptr ) { m_instance = new GlobalClass; } return m_instance; } void set_value( int value ) { m_value = value; } int get_value() { return m_value; } ~GlobalClass() { delete m_instance; } private: GlobalClass() : m_value( 0 ) { } static GlobalClass* m_instance; int m_value; };
And then i can use, using this:
GlobalClass::get()->set_value(2); auto val = GlobalClass::get();
-
Yes it's one correct way to implement a singleton.