Qt synchronous QNetworkAccessManager get

狂风中的少年 提交于 2019-12-21 17:11:02

问题


What's the proper way to do a synchronous QNetworkAccessManager::get ?

The qt wiki offers an approach, but states "it is not recommended to use this in real applications." The mailinglist offers a similar solution to the wiki.


回答1:


Yum may use something like this:

QEventLoop loop;
connect(_netReply, SIGNAL(finished()), &loop, SLOT(quit()));
loop.exec();



回答2:


The simple solution mentioned in the wiki and in the answer from yttrium is quite fragile since it doesn't handle all possible failure scenarios (such as proxy) and therefore shouldn't be used in a production environment, and unfortunately it has become quite prolific, so anyone asking for synchronous QNAM simply get stumped by "use it asynchronoysly [stupid]" or this simple piece of code that will eventually fail.

I haven't found a "proper" solution made by the Qt team themselves, but this guy on codeproject has been decent enough to make a more comprehensive wrapper that should be a lot safer:
http://www.codeproject.com/Articles/484905/Use-QNetworkAccessManager-for-synchronous-download



来源:https://stackoverflow.com/questions/11828322/qt-synchronous-qnetworkaccessmanager-get

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!