Note that there are some explanatory texts on larger screens.

plurals
  1. PORun several url requests in a loop in Qt
    text
    copied!<p>In Qt I need to connect and check for updates on several servers and for this I'm using QNetworkAccessManager. The problem is that I don't want to connect to the next url until the current request has replied and is finished. My first idea was to use a loop like below, but then I have the problem with that I don't want to connect to the next url until the current is finished.</p> <pre><code>void connect() { for (int index = 0; index &lt; 20; index++) { //I need to use this QSqlQuery to get some data here QSqlQuery query; QString sqlString = getSql(index); query.exec(sqlString); ..... if (needUpdate(index)) { //Check if I need to connect to this url QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(this); connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(finishedSlot(QNetworkReply*))); QUrl url = getUrl(index); //Get the url based on the current index networkAccessManager-&gt;get(QNetworkRequest(url)); } } } void finishedSlot(QNetworkReply* reply) { </code></pre> <p>//Do more stuff }</p> <p>My other idea to solve the problem was to build it like this instead:</p> <pre><code>int index; void connect() { /*I need to use this QSqlQuery to get some data here, it works perfect when just looping like before, but when using this solution I get memory problems, it seems like it doesn't delete the query*/ QSqlQuery query; QString sqlString = getSql(index); query.exec(sqlString); ..... if (needUpdate(index)) { //Check if I need to connect to this url QNetworkAccessManager *networkAccessManager = new QNetworkAccessManager(this); connect(networkAccessManager, SIGNAL(finished(QNetworkReply*)),this, SLOT(finishedSlot(QNetworkReply*))); QUrl url = getUrl(index); //Get the url based on the current index networkAccessManager-&gt;get(QNetworkRequest(url)); } else { index++; connect(); //Request next url } } void finishedSlot(QNetworkReply* reply) { //Do more stuff index++; connect(); //Request next url } </code></pre> <p>Now the next url will not be requested before the current url is finished. The problem with this solution is that I have some problems with that as many connection() and finishedSlot() will be opened at the same time the stuff that I create in these methods wont get deleted and this will cause memory problems. I know it has something to do with query.exec(sqlString), because without this everything works like it should. But why would this differ so much between these two solutions?</p> <p>How would you solve this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload