QNetworkReply等待完成

编程入门 行业动态 更新时间:2024-10-06 20:37:53
本文介绍了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.

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

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:49:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1583221.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:QNetworkReply

发布评论

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

>www.elefans.com

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