How to use QNetworkAccessManager.get, how can I refuse interruption?

I am trying to use the QT QNetworkAccessManager class to manage some downloads in a multithreaded C ++ / QT application.

In a work thread (editing: a thread separately for other reasons than loading), I would like to access an external server and be ready to get the results with the code:

... m_nam = new QNetworkAccessManager(this); QNetworkReply *reply = m_nam->get(request); connect(m_nam, SIGNAL(finished(QNetworkReply *)), this, SIGNAL(finished(QNetworkReply *))); ... 

But I can decide, before the download is complete, that I am not interested in the result.

So, I would like to set up a way to disconnect a connection from another thread by emitting a do_abort () signal.

What prompts:

 connect(this, SIGNAL(do_abort()), reply, SLOT(abort())); 

But I do not think this will work, because the interrupt is not a QNetworkReply slot.

So, how can I install a mechanism where I can stop this download from another thread? I could subclass QNetworkReply and give this class an appropriate slot. But I would also like to understand the situation.

+4
source share
1 answer

You do not need a workflow to use QNetworkAccessManager. It is asynchronous, so it can be used from the main thread.

In QThread, you implement the abortTheReply () slot, inside which you execute m_reply-> abort (). Then you connect the do_abort () signal to abortTheReply ().

+2
source

Source: https://habr.com/ru/post/1300494/


All Articles