Qt Connectivity Issues on Network State Change

2019/10/23

Categories: c++ Software Tags: c++ Qt QNetworkAccessManager connectivity change network state change Qt bug

This is a small note post in which I’ll write down a small issue that I had when using the QNetworkAccessManager class for handling network requests.

The general recommendation from Qt, for the QNetworkAccessManager is:

One QNetworkAccessManager instance should be enough for the whole Qt application.

However, there are some issues with connectivity state changes when using this approach.

The scenario that I encountered is related to network changes from wireless to wired and the other way around (user plugs and unplugs the ethernet cable in a machine).

In this case, it seems that the QNetworkAccessManager does not see the new network change, and all subsequent requests using the QNetworkAccessManager object will result in timeouts or other errors. Sometimes the change is detected after a few minutes and sometimes it is never detected and the application remains unable to make network requests until it is restarted.

For handing this scenario, an easy way to bypass this behavior is to create a new QNetworkAccessManager object on each new network request. I know that this goes agains the Qt recommendations and would probably create a performance penalty, but it does the job.

So, in case you have this use case in your application - having multiple network changes - and you are not performance critical with the network communication, you might want to use QNetworkAccessManager like pointed out above in order to make the application respond better to network changes.