使用 QNetworkAccessManager.get,我如何决定中止?
我正在尝试使用 QT QNetworkAccessManager 类来管理多线程 C++/QT 应用程序中的一些下载。
在工作线程上(编辑:除了下载之外,该线程由于其他原因是独立的),我想访问外部服务器并准备好接收代码结果:
...
m_nam = new QNetworkAccessManager(this);
QNetworkReply *reply = m_nam->get(request);
connect(m_nam, SIGNAL(finished(QNetworkReply *)), this,
SIGNAL(finished(QNetworkReply *)));
...
但在下载完成之前,我可能会决定我对结果不感兴趣。
因此,我想设置一种方法,通过发出信号 do_abort() 来断开与另一个线程的连接。
其本身的含义是:
connect(this, SIGNAL(do_abort()), reply, SLOT(abort()));
但我认为这不会起作用,因为 abort 不是 QNetworkReply 的插槽。
那么如何设置一种机制来阻止另一个线程的下载呢?我可以子类化 QNetworkReply 并为该类提供适当的插槽。但我也想了解一下情况。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 QNetworkAccessManager 不需要工作线程。它是异步的,因此可以从主线程使用它。
在 QThread 中,您实现 abortTheReply() 槽,并在其中执行 m_reply->abort()。然后将 do_abort() 信号连接到 abortTheReply()。
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().