I have this code, written in qt 5.15:
Widget::Widget()
{
manager = new QNetworkAccessManager(this);
QNetworkReply *reply = manager->get(QNetworkRequest(QUrl("http://example.com")));
connect(reply, &QNetworkReply::readyRead, this, &Widget::replyFinished));
}
void Widget::replyFinished(QNetworkReply* reply)
{
// some processing here
qDebug() << reply.readAll();
}
It works as expected, but I wonder, is there a risk to miss the signal emmited by QNetworkReply?
I tested that if I had some delay (with QThread::sleep(1);) between the get and the connect, replyFinished is not called.
Is there a method to ask an object to resend a signal if it has been missed?
>Solution :
If QNetworkReply sends the signal from another thread, it will go through event loop. So if you don’t do something "funny", like force direct connection type, or call processEvents, the signal can not be missed even if the other thread manages to send it before the connect.
If QNetworkReply sends signal from the same thread, the it doesn’t get a chance to even send the signal before your code returns to event loop (or calls processEvents).
Conclusion: this code is robust and can’t miss the signal.