Question

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()));

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

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?

Was it helpful?

Solution

Qt documentation states:

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

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

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()

for example

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();
}

OTHER TIPS

The documentation for error() says, "the finished() signal will probably follow", so no, finished() ought to be sufficient. Don't forget to check the error() getter in the signal handler, though.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top