Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How not to miss a signal

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading