Вопрос

Hi I have a QNetworkAccessManager which I use to send request to get image data from server. This call is asynchronous. I do multiple calls with it. Each call is done by opening a new instance of QNetworkAccessManager So when a specific condition occurs I want to stop the QNetworkAccessManager from receiving the replies from its network requests. Is there any way to do it? Thanks.

Это было полезно?

Решение

Don't use a new QNetworkAccessManager for each request but share the manager. It's usually fine to have just one. Of course one can have multiple if the application design suggests it - but e.g. managing multiple of them in a single controlling object is usually unnecessary. Just have one manager with the same lifetime as the object controlling the network requests.

To cancel running operations, keep the QNetworkReply* pointers QNetworkAccessManager::get/put/post return and call abort() when your condition occurs. Connect to the finished() signal to remove them from the bookkeeping (as otherwise you would end up with dangling pointers).

If that becomes too complicated, think about using the command pattern. In this answer I describe why I find it particularly useful in this context.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top