如何向正确的处理程序发送任意下载请求
我正在构建一个 Qt 应用程序并使用 QNetworkAccessManager 来管理我的 http 请求。由于 QNetworkAccessManager 的异步性质,您必须绑定一个插槽才能在完成时接收 QNetworkReply。
我是多线程设计的新手,所以我不知道如何处理这个问题。我将有 3 种不同类型的网络回复,需要在绑定槽中进行解析,然后传递给正确的处理程序。第一个将提取链接,第二个将提取图片,第三个将解析发布请求的回复以验证其是否成功。所有这些都发生在 QWidget 子类中。
所以,我想知道通常如何处理这种情况。在我看来,纯粹基于直觉,因为我对这个特定主题的阅读很少,我认为有两种方法可以处理这个问题。一种是根据调用重新绑定网络管理器的 finish() 信号,另一种是使用某种状态标志并检查这些标志以查看预期的答复。首选方法是什么,不一定在这两者之间,这只是我自己能想到的,更有经验的人很可能有更好的解决方案。
现在,我对 C++ 也相当陌生,所以如果后者是更好的方法,那么在这种情况下处理标志的最佳方法是什么?我会使用位集、枚举/标志设置函数还是其他函数?谢谢你们!
I'm building a Qt application and using the QNetworkAccessManager to manager my http requests. Due to the asyncronous nature of QNetworkAccessManager you have to bind a slot to recieve the QNetworkReply when it's done.
I'm new to multithreaded design so I'm not sure how to handle this. I will have 3 seperate types of network replies which need to be parsed in the bound slot and then passed to the correct handler. One will extract a link, the 2nd will extract a picture, and the third will parse a post request's reply to verify that it was successful. All of this takes place within a QWidget subclass.
So, I'm wondering how this is normally handled. As I see it, based purely on intuition as I've done little reading on this specific subject, I would think there are two ways to handle this. One would be to rebind the network manager's finished() signal depending on the call and the other would be to use some sort of state flags and check those to see what the reply is expected to be. What is the preferred method, not necessarily between these two, that's just all I could think of myself, someone more experienced may well have a better solution.
Now, I'm also fairly new to c++, so if the later is the better way what's the best way to handle flags in this case? Would I use a bitset, an enum/flag setting function, or something else? Thanks guys!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您知道执行特定请求后将获得的回复类型,您可以连接 void QNetworkReply::finished () 向合适的处理程序发出信号。
If you know the type of reply you're going to get as a result of doing specific request you can connect void QNetworkReply::finished () signal to suitable handler.