问题
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