Timeout reply from QNetworkReply
-
Connect it a slot to it and there you can check whatever you want from the reply. The query will be done at that point.
-
Connect it a slot to it and there you can check whatever you want from the reply. The query will be done at that point.
-
@SGaist It may not always be the case. If the reply comes instantly, the slot will never be called. Therefore, check the reply first and then connect.
-
@JoeCFD said in Timeout reply from QNetworkReply:
If the reply comes instantly, the slot will never be called. Therefore, check the reply first and then connect.
?
-
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { /* it may have been done already */ call slot(); } else { connect(); } }
-
@JoeCFD
Have you tested to see whether, with aconnect()
, thefinished
signal would still actually be received and trigger the slot (so no need to testisFinished()
)? My understanding is that is how slot connections work. -
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { /* it may have been done already */ call slot(); } else { connect(); } }
@JoeCFD said in Timeout reply from QNetworkReply:
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { /* it may have been done already */ call slot(); } else { connect(); } }
slot() --> it is my NetworkWorker::readyRead() ?
connect() --> what connect() are you talking about?
Still I do not know when I should emit signal about timeout recived data ....
-
@JoeCFD said in Timeout reply from QNetworkReply:
QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { /* it may have been done already */ call slot(); } else { connect(); } }
slot() --> it is my NetworkWorker::readyRead() ?
connect() --> what connect() are you talking about?
Still I do not know when I should emit signal about timeout recived data ....
@Damian7546 said in Timeout reply from QNetworkReply:
connect() --> what connect() are you talking about?
connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead);
slot() is your NetworkWorker::readyRead.
-
And where timeout is expired ?
I still understand where I can give for example :qDebug() << "timeout has expired to receive data"
void NetworkWorker::get(QString location) { QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { readyRead(); } else { connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead); } } }
void NetworkWorker::readyRead() { QByteArray data; QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ); if ( !statusCode.isValid() ) return; int status = statusCode.toInt(); if ( status != 200 ) { QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString(); } else { if (reply) { data = reply->readAll(); } networkReply(data); } }
-
And where timeout is expired ?
I still understand where I can give for example :qDebug() << "timeout has expired to receive data"
void NetworkWorker::get(QString location) { QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); if ( nullptr != reply ) { if ( reply->isFinished()) { readyRead(); } else { connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead); } } }
void NetworkWorker::readyRead() { QByteArray data; QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender()); QVariant statusCode = reply->attribute( QNetworkRequest::HttpStatusCodeAttribute ); if ( !statusCode.isValid() ) return; int status = statusCode.toInt(); if ( status != 200 ) { QString reason = reply->attribute( QNetworkRequest::HttpReasonPhraseAttribute ).toString(); } else { if (reply) { data = reply->readAll(); } networkReply(data); } }
@Damian7546 Call https://doc.qt.io/qt-6/qnetworkreply.html#error in your readyRead() slot and check it return value.
-
@Damian7546 Call https://doc.qt.io/qt-6/qnetworkreply.html#error in your readyRead() slot and check it return value.
@jsulm Can you give me an example ?
After
QNetworkReply *reply = qobject_cast<QNetworkReply*>(sender());
I added:
qDebug() << "http error: " << reply->error();
And when I have connection to http server the shows message:
http error: QNetworkReply::NoError
But nothing shows up when there is no connection, because
readyread
doesn't appear.I should in this function check timeout:
void NetworkWorker::get(QString location) { qInfo() << "Getting from server...."; QNetworkReply *reply = manager.get(QNetworkRequest(QUrl(location))); connect(reply,&QNetworkReply::readyRead,this,&NetworkWorker::readyRead); }
So in abov funnction added :
connect(reply,&QNetworkReply::errorOccurred,this,&NetworkWorker::errorOccurred);
but never occurred....