i am a beginner in c++ and i am trying to retrieve data from a website using http request and to download the data in a file .

I am using the classes :

QMainWindow QtNetwork/QNetworkAccessManager QtNetwork/QNetworkRequest QtNetwork/QNetworkReply QUrl

The thing is that the file is created but there is no data in the file and i am getting an error that i don't understand . I searched through the forum found some similar kind of problems but did not understand as i am a beginner . Please help me its a school project and i am really stuck here.

Here is the httpWindow.h code


Qt Code:
  1. #ifndef HTTPWINDOW_H
  2. #define HTTPWINDOW_H
  3.  
  4. #include <QMainWindow>
  5. #include <QtNetwork/QNetworkAccessManager>
  6. #include <QtNetwork/QNetworkRequest>
  7. #include <QtNetwork/QNetworkReply>
  8. #include <QUrl>
  9. #include <QString>
  10.  
  11. class QFile;
  12.  
  13. namespace Ui {
  14. class httpWindow;
  15. }
  16.  
  17. class httpWindow : public QMainWindow
  18. {
  19. Q_OBJECT
  20.  
  21. public:
  22. explicit httpWindow(QWidget *parent = 0);
  23. ~httpWindow();
  24. void request(QUrl url);
  25.  
  26. private slots:
  27. void downloadFile();
  28. void httpFinished();
  29. void httpReadyRead();
  30. private:
  31. Ui::httpWindow *ui;
  32. QUrl url;
  33. QNetworkAccessManager *manager;
  34. QNetworkRequest requete;
  35. QNetworkReply *reply;
  36. QFile *file;
  37. int httpGetId;
  38. bool httpRequestAborted;
  39.  
  40. };
  41.  
  42. #endif // HTTPWINDOW_H
To copy to clipboard, switch view to plain text mode 


Here is the httpwindow.cpp

Qt Code:
  1. #include <QtWidgets>
  2. #include <qnetwork.h>
  3. #include <QString>
  4.  
  5. #include "httpwindow.h"
  6. #include "ui_httpwindow.h"
  7.  
  8. httpWindow::httpWindow(QWidget *parent) :
  9. QMainWindow(parent),
  10. ui(new Ui::httpWindow)
  11. {
  12. ui->setupUi(this);
  13. downloadFile();
  14. }
  15.  
  16. httpWindow::~httpWindow()
  17. {
  18. delete ui;
  19. }
  20.  
  21. void httpWindow::request(QUrl url)
  22. {
  23. manager = new QNetworkAccessManager(this);
  24. connect(manager, SIGNAL(finished()),
  25. this, SLOT(httpFinished()));
  26.  
  27. //requete.setUrl(QUrl("http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide"));
  28. requete.setUrl(url);
  29.  
  30. reply = manager->get(requete);
  31. connect(reply, SIGNAL(&reply::readyRead()), this, SLOT(httpReadyRead()));
  32. }
  33.  
  34. void httpWindow::downloadFile()
  35. {
  36. QUrl url("http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide") ;
  37. qDebug() << url.toString();
  38. QFileInfo fileInfo(url.path());
  39. //msg.setText("fileInfo = " + fileInfo);
  40. QString fileName = "C:\\testQt\\" + fileInfo.fileName();
  41. msg.setText("fileName = " + fileName);
  42.  
  43. if (fileName.isEmpty()){
  44.  
  45. fileName = "C:\testQt\fichier.html";
  46. msg.setText(" création d'un nouveau fichier fichier.html ");
  47.  
  48. }
  49. if (QFile::exists(fileName)) {
  50. QFile::remove(fileName);
  51. return;
  52.  
  53.  
  54. }
  55. file = new QFile(fileName);
  56. msg.setText(" QFile::exists(fileName) == true , file : ");
  57. if (!file->open(QIODevice::WriteOnly)) {
  58.  
  59. delete file;
  60. file = 0;
  61. return;
  62. }
  63.  
  64.  
  65. // schedule the request
  66. httpRequestAborted = false;
  67. request(url);
  68.  
  69. }
  70.  
  71. void httpWindow::httpFinished()
  72. {
  73. if (httpRequestAborted) {
  74. if (file) {
  75. file->close();
  76. file->remove();
  77. delete file;
  78. file = 0;
  79. }
  80. reply->deleteLater();
  81. return;
  82. }
  83.  
  84. file->flush();
  85. file->close();
  86.  
  87. QVariant redirectionTarget = reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
  88. if (reply->error()) {
  89. file->remove();
  90. // QMessageBox::information(this, tr("HTTP"),
  91. // tr("Download failed: %1.")
  92. // .arg(reply->errorString()));
  93. // downloadButton->setEnabled(true);
  94. } else if (!redirectionTarget.isNull()) {
  95. QUrl newUrl = url.resolved(redirectionTarget.toUrl());
  96. // if (QMessageBox::question(this, tr("HTTP"),
  97. // tr("Redirect to %1 ?").arg(newUrl.toString()),
  98. // QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
  99. url = newUrl;
  100. reply->deleteLater();
  101. file->open(QIODevice::WriteOnly);
  102. file->resize(0);
  103. request(url);
  104. return;
  105.  
  106. }
  107.  
  108. reply->deleteLater();
  109. reply = 0;
  110. delete file;
  111. file = 0;
  112.  
  113. }
  114.  
  115.  
  116. void httpWindow::httpReadyRead()
  117. {
  118. // this slot gets called every time the QNetworkReply has new data.
  119. // We read all of its new data and write it into the file.
  120. // That way we use less RAM than when reading it at the finished()
  121. // signal of the QNetworkReply
  122. if (file)
  123. file->write(reply->readAll());
  124.  
  125. }
  126. Here is the main.cpp code
  127.  
  128. #include "httpwindow.h"
  129. #include <QApplication>
  130.  
  131. int main(int argc, char *argv[])
  132. {
  133. QApplication a(argc, argv);
  134. httpWindow w;
  135. w.show();
  136.  
  137. return a.exec();
  138. }
To copy to clipboard, switch view to plain text mode 

The errors :

Qt Code:
  1. can't find linker symbol for virtual table for `QMessageBox' value
  2. found `RGB_MASK' instead
  3. can't find linker symbol for virtual table for `QMessageBox' value
  4. found `RGB_MASK' instead
  5. "http://fxrates.fr.forexprostools.com/index.php?force_lang=5&pairs_ids=1;3;2;4;7;5;8;6;&header-text-color=%23FFFFFF&curr-name-color=%230059b0&inner-text-color=%23000000&green-text-color=%232A8215&green-background=%23B7F4C2&red-text-color=%23DC0001&red-background=%23FFE2E2&inner-border-color=%23CBCBCB&border-color=%23cbcbcb&bg1=%23F6F6F6&bg2=%23ffffff&bid=show&ask=show&last=show&change=hide&last_update=hide"
  6. QObject::connect: No such signal QNetworkAccessManager::finished() in ..\ppe3_trading_test\httpwindow.cpp:24
  7. QObject::connect: (receiver name: 'httpWindow')
  8. QObject::connect: No such signal QNetworkReplyHttpImpl::&reply::readyRead() in ..\ppe3_trading_test\httpwindow.cpp:31
  9. QObject::connect: (receiver name: 'httpWindow')
To copy to clipboard, switch view to plain text mode 

Please do help me its really important for my schooling and its been 4 days i am trying to solve the problem .PLease help