gpt4 book ai didi

c++ - 如何正确中止 Qt 中的网络请求?

转载 作者:行者123 更新时间:2023-11-30 03:17:02 27 4
gpt4 key购买 nike

我有一个类执行对网络的请求并解析数据。我如何正确实现请求中止?

假设我有这样一个类:

class MyClass
{
public:
...
void doRequest()
{
m_reply = m_manager.get(...);

QEventLoop waitForResponse;
connect(m_reply, &QNetworkReply::finished, &waitForResponse, &QEventLoop::quit);
waitForResponse.exec();

// Check if request was aborted (otherwise, application will crash)
if (m_reply == nullptr)
return;

// Check for network errors, write result to m_data and delete m_reply;
...
}
void abort()
{
if (m_reply != nullptr)
m_reply->abort();
}
QString data()
{
return m_data;
}
...
private:
QNetworkAccessManager *m_manager;
QPiinter<QNetworkReply> m_reply;
QString m_data;
}

这是按下按钮的使用示例:

class MainWindow : public QMainWindow
{
...
private slots:
MainWindow::on_myButton_pressed()
{
m_myClass->abort();
m_myClass->doRequest();
ui->myTextEdit->setText(m_myClass->data());
}

private:
MyClass m_myClass;
}

当你按下按钮时,如果之前的请求没有完成,那么它就被取消了。这行得通。但在这种情况下,在这种情况下,一个新请求将数据写入 QTextEdit 并退出该函数,然后旧请求从它自己的循环返回并将相同的 m_data 写入 QTextEdit 再次。

是否正确?也许有更正确的方法来实现这个?

最佳答案

嵌套事件循环是 the root of all evil .编写类似 doRequest 的函数要简单得多,而无需向用户假装它是一个同步函数。看起来您已经跟踪了调用 abort() 时发生的错综复杂的控制流,并且您了解了对 doRequest() 的后续调用是如何最终成为嵌套调用的重新进入事件循环。如果您多次重新启动您的请求,您的堆栈将类似于(堆栈向下增长):

1.  main function
2. main event loop
3. [...] (Qt functions)
4. MainWindow::on_myButton_pressed()
5. MyClass::doRequest()
6. QEventLoop::exec()
7. [...] (Qt functions)
8. MainWindow::on_myButton_pressed()
9. MyClass::doRequest()
10. QEventLoop::exec()
11. [...] (Qt functions)
12. MainWindow::on_myButton_pressed() and so on...

MainWindow::on_myButton_pressed() 的每个调用都需要在其嵌套事件循环退出时调用 ui->myTextEdit->setText()

如果您需要在特定操作完成时执行某些操作,另一种方法是使您的函数完全异步并依赖于信号/槽。这是您要实现的目标的最小实现:

#include <QtNetwork>
#include <QtWidgets>

/// A class responsible for communication with the web backend
class Backend : public QObject {
Q_OBJECT
public:
explicit Backend(QObject *parent = nullptr)
: QObject(parent), m_reply(nullptr) {}
public slots:
void doRequest() {
// abort and delete ongoing request (if any)
if (m_reply) {
m_reply->abort();
delete m_reply;
m_reply = nullptr;
}
emit textUpdated(QString());
// send request
QUrl url("http://wtfismyip.com/text");
QNetworkRequest request(url);
m_reply = m_manager.get(request);
// when the request is finished,
QObject::connect(m_reply, &QNetworkReply::finished, [this] {
// if the request ended successfully, read the received ip into m_lastData
if (m_reply->error() == QNetworkReply::NoError)
m_lastData = QString::fromUtf8(m_reply->readAll());
// otherwise, emit errorOccured() signal (if the request has not been
// actively canceled)
else if (m_reply->error() != QNetworkReply::OperationCanceledError)
emit errorOccured(m_reply->errorString());
// in all cases, emit updateText and do cleanup
emit textUpdated(m_lastData);
m_reply->deleteLater();
m_reply = nullptr;
});
}

void abort() {
if (m_reply != nullptr)
m_reply->abort();
}

signals:
void textUpdated(const QString &);
void errorOccured(const QString &);

private:
QNetworkAccessManager m_manager;
QNetworkReply *m_reply;
QString m_lastData;
};

/// A minimal widget that contains a QPushButton and a QLabel
class Widget : public QWidget {
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr) : QWidget(parent) {
m_layout.addWidget(&m_pushButton);
m_layout.addWidget(&m_label);

connect(&m_pushButton, &QPushButton::clicked, this, &Widget::buttonClicked);
}

signals:
void buttonClicked();

public slots:
void updateText(const QString &text) { m_label.setText(text); }
void showError(const QString &error) {
QMessageBox::warning(this, tr("Error"), error);
}

private:
QVBoxLayout m_layout{this};
QPushButton m_pushButton{"Retrieve Name"};
QLabel m_label;
};

int main(int argc, char *argv[]) {
QApplication a(argc, argv);

Backend backend;
Widget widget;

// connect components
QObject::connect(&backend, &Backend::textUpdated, &widget,
&Widget::updateText);
QObject::connect(&backend, &Backend::errorOccured, &widget,
&Widget::showError);
QObject::connect(&widget, &Widget::buttonClicked, &backend,
&Backend::doRequest);
widget.show();

return a.exec();
}

#include "main.moc"

关于c++ - 如何正确中止 Qt 中的网络请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55777381/

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