我也应该连接到QNetworkReply :: error()吗? [英] Should I connect to QNetworkReply::error() as well?

查看:848
本文介绍了我也应该连接到QNetworkReply :: error()吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个POST请求,并连接到完成()信号:

I have created a POST request and I connect to the finished() signal:

QNetworkReply *reply = manager->post(request, postData.encodedQuery());
connect(reply, SIGNAL(finished()), this, SLOT(accept()));

我希望在POST请求完成时得到通知,无论请求是成功还是失败.

I want to be notified when the POST request has finished, regardless of whether it failed or succeeded.

我在文档中注意到,还有一个 QNetworkReply :: error()信号,我是否也需要连接到它,还是在所有情况下都将调用finished()?

I have noticed in the documentation that there is also a QNetworkReply::error() signal, do I need to connect to it, too, or will finished() be called in all cases?

推荐答案

Qt文档状态:

无效QNetworkReply :: error(QNetworkReply :: NetworkError代码)[信号]

void QNetworkReply::error(QNetworkReply::NetworkError code) [signal]

当回复检测到处理错误时,将发出此信号. finish()信号可能会跟随,表明 连接结束.

This signal is emitted when the reply detects an error in processing. The finished() signal will probably follow, indicating that the connection is over.

根据我在Qt来源中看到的(最近正在检查绝对相同的问题),在error()之后的每个地方,之后都有一个finish()调用.在5.1.0中,我没有找到一个没有错误的地方,而不是Finished()

From what I've seen in Qt sources (was checking absolutely same issue recently), everywhere after error(), there is a finished() call afterwards. In 5.1.0 I haven't found a place where error is not followed by finished()

例如

void QNetworkReplyImpl::close()
{
    Q_D(QNetworkReplyImpl);
    if (d->state == QNetworkReplyImplPrivate::Aborted ||
        d->state == QNetworkReplyImplPrivate::Finished)
        return;

    // stop the download
    if (d->backend)
        d->backend->closeDownstreamChannel();
    if (d->copyDevice)
        disconnect(d->copyDevice, 0, this, 0);

    QNetworkReply::close();

    // call finished which will emit signals
    d->error(OperationCanceledError, tr("Operation canceled"));
    d->finished();
}

这篇关于我也应该连接到QNetworkReply :: error()吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆