调试完成后,我确定在调用this-> getNetReply()时没有调用replyFinish()槽。这些是我的文件,在main()fumction中我用这种方式调用getNetReply:Networking a; a.getNetReply(); 我确实在我的qmake中添加了QT + = network。 请帮我。非常感谢你。
我的networking.cpp文件
#include "networking.h"
#include <QUrl>
#include <QNetworkRequest>
// constructor
void Networking::getNetReply(){
QNetworkAccessManager *man = new QNetworkAccessManager(this);
QObject::connect(man, SIGNAL(finished(QNetworkReply *)), this, SLOT(replyFinished(QNetworkReply *)));
qDebug() << "connected";
QNetworkRequest req;
req.setUrl(QUrl("http://www.google.com"));
man->get(req);
}
// this method not called
void Networking::replyFinished(QNetworkReply *reply){
QByteArray data = reply->readAll();
QString str = QString(data);
this->netRep = str;
code = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
}
我的networking.h头文件:
#ifndef NETWORKING_H
#define NETWORKING_H
#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
class Networking : public QObject
{
Q_OBJECT
public:
QString netRep;
int code;
explicit Networking(QObject *parent = 0);
void getNetReply();
public slots:
void replyFinished(QNetworkReply*);
};
#endif // NETWORKING_H
答案 0 :(得分:0)
get()函数返回一个QNetworkReply对象。连接到该对象的错误信号,它将告诉您是否发生错误(以及哪个)。