1

I am new to write QT code. I write a simple http client code (The Qt version is 5.9). I define a class that named my_http_client, it derived Qobject, and I using QNetworkAccessManager class to create http connection. I create 3 files, one is main.cpp, another two is my_http_client.h and my_http_client.cpp. The my_http_client class defined in file my_http_client.cpp. Ok, look the flowing is my code. When I compile my project, it has errors.

D:\Documents\code\c++\qt_console\http\http_client\my_http_client.cpp:16: error: no matching function for call to 'my_http_client::connect(QNetworkReply*&, const char*, my_http_client*, const char*)'
    QObject::connect(rep_QNetworkReply, SIGNAL(finished()), this, SLOT(httpFinished()));

I don't know what happened, I try to solve it, but i can't. I view same questions. The answer does not solve my problem.Please help me. How did I correct the error.Thanks.

my_http_client.h

#ifndef MY_HTTP_CLIENT_H
#define MY_HTTP_CLIENT_H


/*create by victory, time: 2020/05/16
This file define a simple httpclient class,support basic http request
*/

#include <QObject>
#include <QNetworkAccessManager>

QT_BEGIN_NAMESPACE
class QNetworkAccessManager;
class QNetworkReply;
QT_END_NAMESPACE

class my_http_client : public QObject
{
    Q_OBJECT

public:
    explicit my_http_client(QObject *parent = Q_NULLPTR);

    ~my_http_client();      //destructor

public:
    void get(const QUrl &url);


private:
    QNetworkAccessManager *manager;
    QNetworkReply* rep_QNetworkReply;


private slots:
    void httpFinished();
    void httpReadyRead();
};

#endif // MY_HTTP_CLIENT_H

my_http_client.cpp

#include "my_http_client.h"
#include <iostream>


my_http_client::my_http_client(QObject *parent)
    :  QObject(parent)
{
    this->manager = new QNetworkAccessManager(this);
}

//client send get request
void my_http_client::get(const QUrl &url)
{

   this->rep_QNetworkReply = this->manager->get(QNetworkRequest(url));
   connect(rep_QNetworkReply, SIGNAL(finished()), this, SLOT(httpFinished()));

   //connect(rep_QNetworkReply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
}

//QNetworkReply send a finished siganl
//function replyFinished recv it and handle it
void my_http_client::httpFinished()
{

    std::cout << "httpFinished:  " << rep_QNetworkReply << std::endl;

    QVariant redirectionTarget = rep_QNetworkReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    if (rep_QNetworkReply->error())
    {
        std::cerr << "get failed: " << rep_QNetworkReply->errorString() << std::endl;
    }

    rep_QNetworkReply->deleteLater();
    rep_QNetworkReply = Q_NULLPTR;


}



void my_http_client::httpReadyRead()
{
    std::cout << "httpReadyRead: " << std::endl;
    std::cout << rep_QNetworkReply->readAll() << std::endl;

}

my_http_client::~my_http_client()
{
    delete this->manager;
}

main.cpp

#include <QCoreApplication>
#include "my_http_client.h"


/*this is a sample http client, create it by victory*/
/*time is 2020:05:16*/

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    my_http_client pClient;
    pClient.get(QUrl("http://127.0.0.1:8888/index.html"));


    return a.exec();
}

  • Your slot `void httpFinished()` has to be changed to `void httpFinished(QNetworkReply *);`. Basically signatures have to be matched between signals and slots. FYI: https://doc.qt.io/qt-5/signalsandslots.html – Ramkumar R May 18 '20 at 13:08
  • I find manual about `QNetworkReply`, It has `finished` siganl, and no input arguments. [`QNetworkReply::finished`](https://doc.qt.io/qt-5.9/qnetworkreply.html#finished) – victoryFree May 19 '20 at 03:15

1 Answers1

1

Try adding #include <QNetworkReply> in my_http_client.h, it resolved the issue for me.

Also, I would suggest replacing std logging with qDebug() and qCritical() from #include <QDebug> (debugging in Qt)

alexey_zotov
  • 371
  • 1
  • 8