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