QT请求" QObject :: connect:没有这样的插槽BackEnd :: RequestReceived"

时间:2017-10-06 13:13:27

标签: c++ qt

我试图通过QT请求获取一些数据。

my backend.h

#ifndef BACKEND_H
#define BACKEND_H

#include <QNetworkAccessManager>
#include <QObject>
#include <QString>
#include <QNetworkReply>
class BackEnd : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QString userName READ userName WRITE setUserName NOTIFY userNameChanged)

public:
    explicit BackEnd(QObject *parent = nullptr);

    QString userName();
    void setUserName(const QString &userName);

signals:
    void userNameChanged();

private:
    QString m_userName;
    QNetworkAccessManager *manager;
    //also tried: void RequestReceived(QNetworkReply* reply); << without space after QNetworkReply
    void RequestReceived(QNetworkReply * reply);
};
#endif // BACKEND_H

我的.cpp

#include "backend.h"
#include <string>
#include <iostream>
#include <QtNetwork>

BackEnd::BackEnd(QObject *parent) :
    QObject(parent)
{
    manager = new QNetworkAccessManager(this);


}
void BackEnd::RequestReceived(QNetworkReply * reply){
    QByteArray rawData = reply->readAll();
    QString textData(rawData);
    qDebug() << textData;
}

QString BackEnd::userName()
{
    return m_userName;
}

void BackEnd::setUserName(const QString &userName)
{
    if (userName == m_userName)
        return;

    m_userName = userName;
    connect(manager, SIGNAL(finished(QNetworkReply*)),
            this, SLOT(RequestReceived(QNetworkReply*)));
    manager->get(QNetworkRequest(QUrl("https://google.com")));
    emit userNameChanged();

}

我发现了similair问题,但几乎所有答案都说应该添加Q_OBJECT(我有)。我对qt很新,但如果我理解正确,错误表明我没有

  

后端:: RequestReceived(QNetworkReply *)

方法,我有。欢迎任何帮助。

1 个答案:

答案 0 :(得分:2)

您需要将void RequestReceived(QNetworkReply * reply);标记为广告位:

Q_SLOT void RequestReceived(QNetworkReply * reply);