我在使用 QNetworkAccessManager 从获取请求中获得回复时遇到了一个奇怪的问题。
这是类的代码:
请求者.h
#ifndef REQUESTER_H
#define REQUESTER_H
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtCore/QtCore>
#include <QVector>
#include <QObject>
#include <QMessageBox>
class Requester : public QObject
{
Q_OBJECT
public:
explicit Requester(QObject *parent = 0);
~Requester();
QString get_last_reply();
void send_request();
private:
QNetworkAccessManager *manager;
QVector<QString> replies;
private slots:
void get_reply(QNetworkReply *reply);
void get_reply_error(QNetworkReply::NetworkError err);
};
#endif // REQUESTER_H
请求者.cpp
#include "requester.h"
Requester::Requester(QObject *p)
: QObject(p)
, manager(new QNetworkAccessManager)
{
QObject::connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(get_reply(QNetworkReply*)));
}
Requester::~Requester() {
delete manager;
}
void Requester::get_reply(QNetworkReply *reply) {
QObject::connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(get_reply_error(QNetworkReply::NetworkError)));
QByteArray res = reply->readAll();
QString data = res.data();
replies.push_back(data);
QObject::disconnect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(get_reply_error(QNetworkReply::NetworkError)));
reply->deleteLater();
}
void Requester::get_reply_error(QNetworkReply::NetworkError err) {
QMessageBox msg;
msg.setText(QString::number(err));
msg.setStandardButtons(QMessageBox::Discard);
msg.exec();
}
QString Requester::get_last_reply() {
if(!(replies.isEmpty())) {
QString res = replies.back();
replies.pop_back();
return res;
}
return "";
}
void Requester::send_request() {
QNetworkRequest request;
request.setUrl(QUrl("http://127.0.0.1"));
request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17");
manager->get(request);
//QMessageBox *msg = new QMessageBox;
//msg->exec();
}
回复是通过这个函数写在 textEdit 中的
void MainWindow::ret_out(QString str) {
ui->out->setText(str);
}
现在 main.cpp
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
Requester req;
req.send_request();
//QMessageBox *msg = new QMessageBox;
//msg->exec();
QString buf = req.get_last_reply();
w.show();
w.ret_out(buf);
return a.exec();
}
使用此代码我有空的文本编辑。但是如果取消注释
QMessageBox *msg = new QMessageBox;
msg->exec();
在 Requester::send_request 或 main.cpp 然后 textEdit 包含服务器答案。
新更新
现在我有了这段代码,它可以工作了。也许这不是最好的变化,但我会非常乐意听取您的建议:)
void Requester::send_request(QUrl url) {
QEventLoop loop;
loop.connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(get_reply(QNetworkReply*)));
loop.connect(this, SIGNAL(done()), &loop, SLOT(quit()));
QNetworkRequest request;
request.setUrl(url);
request.setRawHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.60 Safari/537.17");
manager->get(request);
loop.exec(QEventLoop::AllEvents);
loop.disconnect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(get_reply(QNetworkReply*)));
loop.disconnect(this, SIGNAL(done()), &loop, SLOT(quit()));
}