cruzo
cruzo

Reputation: 57

Retrieve data from a site through HTTP request (with C++ using QT5.4)

I am writing a program in C++ using Qt version 5.4. I need to retrieve data from a website using http request and using the classes QNetworkAccessManager, QNetworkReply, QNetworkRequest, QUrl. I saw the different posts about this question but could not find any solution to my problem .

Here is the code of the header :fenetreApplication.h

#ifndef FENETREPRINCIPAL_H
#define FENETREPRINCIPAL_H

#include <QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkRequest>
#include <QUrl>
#include <QString>

class QFile;
class QNetworkReply;



namespace Ui {
class fenetrePrincipal;
}

class fenetrePrincipal : public QMainWindow
{
    Q_OBJECT

public:
    explicit fenetrePrincipal(QWidget *parent = 0);
    ~fenetrePrincipal();
    void request();

private slots:
    void downloadFile();
    void cancelDownload();
    void httpFinished();
    void httpReadyRead();



private:
    Ui::fenetrePrincipal *ui;

       QUrl url;
       QNetworkAccessManager *manager;
       QNetworkRequest requete;
       QNetworkReply *reply;
       QFile *file;
       int httpGetId;
       bool httpRequestAborted;
};

#endif // FENETREPRINCIPAL_H

Here is the code of the cpp :fenetreApplication.cpp

#include <QtWidgets>
#include <qnetwork.h>
#include <QString>

#include "fenetreprincipal.h"
#include "ui_fenetreprincipal.h"

fenetrePrincipal::fenetrePrincipal(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::fenetrePrincipal)
{
    ui->setupUi(this);
}

fenetrePrincipal::~fenetrePrincipal()
{
    delete ui;
}

void fenetrePrincipal::request()
{
    manager =  new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(httpFinished(QNetworkReply*)));

    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&change_in_percents=hide&last_update=show"));

    reply = manager->get(request());
connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
}

void fenetrePrincipal::downloadFile()
{

    QFileInfo fileInfo(url.path());
    QString fileName = fileInfo.fileName();
    if (fileName.isEmpty())
        fileName = "index.html";

    if (QFile::exists(fileName)) {

    file = new QFile(fileName);
    if (!file->open(QIODevice::WriteOnly)) {

        delete file;
        file = 0;
        return;
    }


    // schedule the request
    httpRequestAborted = false;
    request();
}



void fenetrePrincipal::httpFinished()
{
    if (httpRequestAborted) {
        if (file) {
            file->close();
            file->remove();
            delete file;
            file = 0;
        }
        reply->deleteLater();
        return;
    }

    file->flush();
    file->close();

    reply->deleteLater();
    reply = 0;
    delete file;
    file = 0;

}


void fenetrePrincipal::httpReadyRead()
{
    // this slot gets called every time the QNetworkReply has new data.
    // We read all of its new data and write it into the file.
    // That way we use less RAM than when reading it at the finished()
    // signal of the QNetworkReply
    if (file)
        file->write(reply->readAll());
}

Here is the code of the main .cpp

#include "fenetreprincipal.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    fenetrePrincipal w;
    w.show();

    return a.exec();
}

Here are the errors :

IN function 'void fenetrePrincipal::request()':
invalid use of void expression
     reply = manager->get(request());


                               ^
no matching function for call to 'fenetrePrincipal::connect(QNetworkReply*&, const char*,

fenetrePrincipal*, const char*)' connect(reply, SIGNAL(readyRead()), this, SLOT(httpReadyRead())); ^

In function 'void fenetrePrincipal::httpFinished()':
invalid use of incomplete type 'class QNetworkReply'
         reply->deleteLater();
              ^

erreur : forward declaration of 'class QNetworkReply'
 class QNetworkReply;
       ^


invalid use of incomplete type 'class QNetworkReply'
         file->write(reply->readAll());
                          ^

Can you help me identify and rectify the errors?

The goal is to create an app that shows the rates of different currencies. The rates are the data I need to retrieve and download in a file.

Upvotes: 1

Views: 1306

Answers (1)

Kunal
Kunal

Reputation: 3535

I think you have two issue with your code.

1) You forward declared QNetworkReply class, but never included header, so you are facing following error.

erreur : forward declaration of 'class QNetworkReply'
 class QNetworkReply;

include header below,

#include <QtNetwork/QNetworkReply>

2) QNetworkManager::get() accepts QNetworkRequest object, you called your own void request() function here.

reply = manager->get(request());

you should do something like,

manager->get(QNetworkRequest(QUrl("http://qt-project.org")));

If you resolve these issues, I think your error will be resolved.

Upvotes: 1

Related Questions