gpt4 book ai didi

Qt QNetworkAccessManager 和多个QNetworkReply

转载 作者:可可西里 更新时间:2023-11-01 16:25:51 26 4
gpt4 key购买 nike

我有两个 get QNetworkRequest。

我想处理来自不同方法的完成信号。

例如这是

中的代码
  MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);

GetUserData();
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(GetUserDataCompleted(QNetworkReply*)));

GetMessages();
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(GetMessagesCompleted(QNetworkReply*)));
}

这是我的一个方法

我试过 replay->deleteLater();但结果相同

请给我一些有用的建议

void MainWindow::GetUserDataCompleted(QNetworkReply *replay)
{
if(replay->error() == QNetworkReply::NoError)
{
QString getData = replay->readAll();
QMessageBox msg;

if(getData == "1")
{
msg.setText("User Is not Exits");
}
else
{
QDomDocument doc;

if(doc.setContent(getData))
{
QDomElement domElem = doc.documentElement();

QDomNode n = domElem.firstChild();
while(!n.isNull()) {
QDomElement e = n.toElement(); // try to convert the node to an element.
if(!e.isNull()) {
msg.setText(e.namedItem("Image").childNodes().at(0).nodeValue());
msg.exec();
}
n = n.nextSibling();
}
}

replay->deleteLater();
}
}
}

最佳答案

您可以创建一个 RequestSender 类,其作用是处理请求。

每个 RequestSender 对象将处理一个唯一的请求。在创建将要发送的 QNetworkRequest 时,RequestSender 将使用 originatingObject 属性“标记”它自己的请求。此属性指示哪个对象发送了请求。当 RequestSender 对象收到回复时,它将通过 originatingObject 属性查看它是否是请求的发送者。有关 originatingObject 的更多信息,您可以引用此处的文档:http://qt-project.org/doc/qt-4.8/qnetworkrequest.html#originatingObject

以下是您可以执行的操作的示例。

请求发送者.hpp :

class RequestSender {
public:
RequestSender();
~RequestSender();
void createRequest(/* Request parameters */);

public slots:
void endRequest(QNetworkReply* replay);
};

请求发送者.cpp :

RequestSender::RequestSender() {
connect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(endRequest(QNetworkReply*)));
}

RequestSender::~RequestSender() {
disconnect(nam, SIGNAL(finished(QNetworkReply*)), this, SLOT(endRequest(QNetworkReply*)));
}

void RequestSender::createRequest(/* Request parameters */) {
QNetworkRequest * myRequest = 0;

// Build myRequest with the request parameters

myRequest->setOriginatingObject(this);
nam->get(*myRequest);

}

void RequestSender::endRequest(QNetworkReply* replay) {
if (replay->request().originatingObject() != this) {
// That's not the request sent by the object -> stop the method here !
return;
}

// Treatments on replay
}

关于Qt QNetworkAccessManager 和多个QNetworkReply,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12077211/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com