本文介绍了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 等待完成
发布评论