QNetworkReply 等待完成

编程入门 行业动态 更新时间:2024-10-06 16:26:55
本文介绍了QNetworkReply 等待完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用 Qt 4.6.3 和以下不起作用的代码

I am using Qt 4.6.3 and the following not-working code

QStringList userInfo; QNetworkRequest netRequest(QUrl("api.stackoverflow/1.1/users/587532")); QNetworkReply *netReply = netman->get(netRequest); // from here onwards not working netReply->waitForReadyRead(-1); if (netReply->isFinished()==true) {userInfo << do sth to reply;} return userInfo;

由于此函数返回一个空的 QStringList,应用程序崩溃.如何等待请求完成,然后在一个函数内处理回复

as this function returns an empty QStringList, the app crashes. How to wait until the request has finished and then process the reply within one function

推荐答案

可以使用事件循环:

QEventLoop loop; connect(netReply, SIGNAL(finished()), &loop, SLOT(quit())); loop.exec(); // here you have done.

您还应该考虑添加一些比网络超时更短的时间(20 秒?).即使发生错误,我也不确定是否调用了完成.因此,您也有可能连接到错误信号.

Also you should consider adding some shorter then network timeout (20s?). I'm not sure if finished is called even if an error occured. So it is possible, that you have connect to error signal also.

更多推荐

QNetworkReply 等待完成

本文发布于:2023-11-13 02:50:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583223.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:QNetworkReply

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!