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:
Qt Code:
  1. Main.cpp:
  2. ```
  3. #include <QCoreApplication>
  4. #include <QDebug>
  5. #include "downloader.h"
  6. int main(int argc, char *argv[])
  7. {
  8. QCoreApplication a(argc, argv);
  9.  
  10. QObject::connect(&Downloader::instance(),&Downloader::testSignal,
  11. [](){
  12. QString s="http://myweb/currxml.php";
  13.  
  14. });
  15. return a.exec();
  16. }
  17. ```
  18.  
  19. download.h:
  20. ```
  21. #ifndef Downloader_H
  22. #define Downloader_H
  23.  
  24. #include <QObject>
  25. #include <QNetworkAccessManager>
  26. #include <QNetworkRequest>
  27. #include <QNetworkReply>
  28. #include <QUrl>
  29. #include <QDateTime>
  30. #include <QFile>
  31. #include <QDebug>
  32. class Downloader : public QObject
  33. {
  34. Q_OBJECT
  35. Q_DISABLE_COPY(Downloader)
  36. public:
  37. static Downloader &instance();
  38. explicit Downloader(QObject *parent = nullptr);
  39. //virtual ~Downloader(){}
  40.  
  41. void doSomething();
  42. signals:
  43.  
  44. void testSignal();
  45.  
  46. public slots:
  47. //void testSlot();
  48. void replyFinished (QNetworkReply *reply);
  49.  
  50. private:
  51. QNetworkAccessManager *manager;
  52.  
  53. };
  54.  
  55. #endif // Downloader_H
  56. ```
  57. downloader.cpp
  58. ```
  59. #include "downloader.h"
  60. #include <QDebug>
  61.  
  62.  
  63. Downloader &Downloader::instance()
  64. {
  65. static Downloader _instance;
  66. return _instance;
  67. }
  68.  
  69. Downloader::Downloader(QObject *parent) : QObject(parent)
  70. {
  71. doSomething();
  72. }
  73.  
  74. void Downloader::doSomething()
  75. {
  76. //emit testSignal();
  77. qDebug() << "It's working!!!!";
  78. manager = new QNetworkAccessManager(this);
  79. manager->get(QNetworkRequest(QUrl(s)));
  80. emit instance().testSignal();
  81. }
  82.  
  83. void Downloader::replyFinished (QNetworkReply *reply)
  84. {
  85. if(reply->error())
  86. {
  87. qDebug() << "ERROR!";
  88. qDebug() << reply->errorString();
  89. }
  90. else
  91. {
  92. //qDebug() << reply->header(QNetworkRequest::ContentTypeHeader).toString();
  93. //qDebug() << reply->header(QNetworkRequest::LastModifiedHeader).toDateTime().toString();
  94. //qDebug() << reply->header(QNetworkRequest::ContentLengthHeader).toULongLong();
  95. //qDebug() << reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
  96. //qDebug() << reply->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toString();
  97. QString p = reply->request().url().fileName();
  98. QFile *file = new QFile("C:/Users/moh/"+p);
  99. if(file->open(QFile::Append))
  100. {
  101. file->write(reply->readAll());
  102. file->flush();
  103. file->close();
  104. }
  105. delete file;
  106. }
  107.  
  108. reply->deleteLater();
  109. }
  110. ```
To copy to clipboard, switch view to plain text mode