title: When calling multiple C++ functions from QML and the first function contains a connect call, the QML continues before the callback is triggerd
body: <p>For a customer of mine I'm creating a mobile app within the QT framework.</p>
  
  <p>The snippets code will be posted at the bottom of the question.</p>
  
  <p>In the app on the login page I'm doing an API call to a REST API running on phalcon PHP, the call works and returns the data just fine, however I'm using getter and setter functions for the API result so the result is accessible in the QML files. </p>
  
  <p>The problem I'm facing is that when the connect function is called the QML starts calling the next functions in line while the API call is not yet finished.</p>
  
  <p>To get an idea of the flow:
  1 <em>click login button</em>->2 <em>send api call</em>->3 <em>receive API result</em>->4 <em>parse the result and make it accesible globally</em>->5 <em>get the result in QML</em>->6 <em>continue with the QML code and login</em></p>
  
  <p>The incorrect flow it's currently doing:
  1 <em>click login button</em>->2 <em>send api call</em>->3 <em>continue with the QML code</em>->4 <em>stop because you're not yet logged in</em>->5 <em>receive API result</em>->6 <em>parse the result and make it accesible globally</em>->7 <em>Stop</em></p>
  
  <p>I've already tried to put in sleeps and waits after step 2 in the flow to see if the callback get called before, but the callback will wait as well.</p>
  
  <p>Code snippets:</p>
  
  <p>QML:</p>
  
  <pre><code>onClicked: {
      _Login.setUsername(inputBox.text)
      _Login.setPassword(inputBox2.text)
      _Login.buttonClicked()
      _Login.checkThread("derp")
      if (_Login.getResult()) {
          if (_DbManager.addId()) {
              var id = _DbManager.getId()
              _Login.setUniqueID(id);
          }
      }
  }
  </code></pre>
  
  <p>Header file:</p>
  
  <pre><code>#ifndef LOGIN_H
  #define LOGIN_H
  
  #include &lt;QUrl&gt;
  #include &lt;QMutex&gt;
  #include &lt;QObject&gt;
  #include &lt;QString&gt;
  #include &lt;QByteArray&gt;
  #include &lt;QQmlEngine&gt;
  #include &lt;QJsonObject&gt;
  #include &lt;QNetworkReply&gt;
  #include &lt;QJsonDocument&gt;
  #include &lt;QNetworkRequest&gt;
  #include &lt;QNetworkAccessManager&gt;
  
  class Login : public QObject
  {
    Q_OBJECT
  public:
  
      explicit Login(QObject *parent = 0);
      QMutex mutex;
      QString username = "";
      QString password = "";
      QString uniqueId = "";
      bool result = false;
  
  signals:
  
  public slots:
    bool getResult();
    void buttonClicked();
    void setResult(bool result);
    void setUsername(QString username);
    void setPassword(QString password);
    void parseNetworkLoginResponse(QNetworkReply* finished);
  };
  
  #endif // LOGIN_H
  </code></pre>
  
  <p>C++:</p>
  
  <pre><code>    void Login::buttonClicked()
  {
      qDebug() &lt;&lt; "Clicked is called";
      QJsonObject jsonObj;
      jsonObj["username"] = this-&gt;username;
      jsonObj["password"] = this-&gt;password;
  
      QJsonDocument doc(jsonObj);
      QByteArray BAJson = doc.toJson();
  
      QNetworkAccessManager *manager = new QNetworkAccessManager(this);
      connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(parseNetworkLoginResponse(QNetworkReply*)));
  
      QNetworkRequest req(QUrl("API URL"));
      req.setRawHeader("Content-Type", "application/json");
      manager-&gt;post(req, BAJson);
      qDebug() &lt;&lt; "Clicked is finished";
  
  }
  
  void Login::parseNetworkLoginResponse(QNetworkReply* finished)
  {
      qDebug() &lt;&lt; "parse is called"
      QByteArray result = finished-&gt;readAll();
      setResult(newResult);
  }
  
  void Login::setResult(bool result)
  {
      qDebug() &lt;&lt; "set result is called";
      this-&gt;result = result;
  }
  
  bool Login::getResult()
  {
      qDebug() &lt;&lt; "get result is called";
      return this-&gt;result;
  }
  </code></pre>