QNetworkReply 等待完成 [英] QNetworkReply wait for finished

查看:82
本文介绍了QNetworkReply 等待完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

QStringList userInfo;
QNetworkRequest netRequest(QUrl("http://api.stackoverflow.com/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 等待完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆