Joe Soul-bringer
Joe Soul-bringer

Reputation: 3304

Using a QNetworkAccessManager.get, how can I decide to abort?

I am attempting to use the QT QNetworkAccessManager class to manage some downloads in a multi-threaded C++/QT application.

On worker thread (edit: the thread is seperate for other reasons aside from doing the download), I'm would like to do a get to an external server and be ready to receive 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 might decide, before the download is finished, that I'm not interested in the result.

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

What suggests itself is:

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

But I don't think that will work because abort is not slot of QNetworkReply.

So how can I set a mechanism where I can stop this download from another thread? I could subclass QNetworkReply and give that class the appropriate slot. But I'd like to understand the situation also.

Upvotes: 4

Views: 4164

Answers (1)

guruz
guruz

Reputation: 1614

You do not need a worker thread for using QNetworkAccessManager. It is asynchronous, so it is OK to use it from your main thread.

In the QThread you implement a abortTheReply() slot and inside that you do m_reply->abort(). Then you connect your do_abort() signal to the abortTheReply().

Upvotes: 2

Related Questions