I have doubts how should I use QEventLoop
. I have 2 pieces of code, both of them work for me (get web resource downloaded).
First one:
QNetworkAccessManager *manager = new QNetworkAccessManager( this );
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "Mozilla Firefox");
connect(manager, SIGNAL(finished(QNetworkReply*)),this,SLOT(replyFinished(QNetworkReply*)));
manager->get( request ) ;
QEventLoop loop;
connect(manager, SIGNAL(finished(QNetworkReply*)),&loop, SLOT(quit()));
loop.exec();
Second one:
QNetworkAccessManager *manager = new QNetworkAccessManager( this );
QNetworkRequest request;
request.setUrl(QUrl(url));
request.setRawHeader("User-Agent", "Mozilla Firefox");
manager->get( request ) ;
QEventLoop loop;
connect(manager, SIGNAL(finished(QNetworkReply*)),this, SLOT(replyFinished(QNetworkReply*)));
loop.exec();
What I want to know is which one should I use. I mean, does the event loop quit in the second one after signal is emmited? Or do I have to call quit()
like in the first one? I found the second solution somewhere but it didn't seem proper to me so I modified it into first piece of code.
QEventLoop
is designed for such cases. When you don't want to make your code complex (with a lot of signals/slots) and need a single flow with support of event-driven logic. – Canopy