【问题标题】:no matching function for call to 'connect'(QT)调用“连接”(QT)没有匹配的功能
【发布时间】:2020-09-04 06:10:22
【问题描述】:

我是编写 QT 代码的新手。我写了一个简单的http客户端代码(Qt版本是5.9)。我定义了一个名为my_http_client 的类,它派生了Qobject,并且我使用QNetworkAccessManager 类来创建http 连接。我创建了 3 个文件,一个是 main.cpp,另外两个是 my_http_client.hmy_http_client.cpp。在文件 my_http_client.cpp 中定义的 my_http_client 类。好的,看看流动的是我的代码。当我编译我的项目时,它有错误。

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()));

我不知道发生了什么,我试图解决它,但我不能。我看 same questions。 答案不能解决我的问题。请帮助我。我是如何纠正错误的。谢谢。

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;
}

ma​​in.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();
}

【问题讨论】:

  • 您的插槽void httpFinished() 必须更改为void httpFinished(QNetworkReply *);。基本上签名必须在信号和插槽之间匹配。仅供参考:doc.qt.io/qt-5/signalsandslots.html
  • 我找到了关于QNetworkReply 的手册,它有finished siganl,并且没有输入参数。 QNetworkReply::finished

标签: c++ qt compiler-errors qobject


【解决方案1】:

尝试在my_http_client.h 中添加#include &lt;QNetworkReply&gt;,它为我解决了这个问题。

另外,我建议用qDebug()qCritical()#include &lt;QDebug&gt; (debugging in Qt) 替换std 日志记录

【讨论】:

  • 要接受答案,您应该按答案的赞成和反对按钮下的勾号图标。更多游览页面详情:stackoverflow.com/tour
  • 是的,明白了!,我已经做到了。感谢您的回答
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-13
  • 1970-01-01
相关资源
最近更新 更多