title: Emitting a Qt::signal from worker thread makes the UI update on main thread by default?
body: <p>I am new to <code>Qt</code>. I have worker thread that is an <code>std::thread</code>. The worker thread function continuously fetches some some data in a loop. The size of the data is frequently updated on a <code>Text</code> element on a <code>QML</code> UI. I have a listener callback which is nothing but an <code>std::function</code> and it gets called from the <code>thread's function</code>. It sends me callbacks based on which I updated the <code>Text</code> element on <code>QML</code>. I update it using <code>signal slot</code> mechanism.</p>
  
  <p>Following is the <code>QML : Text</code> element:</p>
  
  <pre><code>Text {
    id: mytext
    objectName: "mytextobject"
  
    function slotUpdateData(someValue){
      mytext = someValue
    }
  }
  </code></pre>
  
  <p><code>SignalUpdateData</code> is connected with <code>slotUpdateData</code> which resides on <code>QML</code> side. Every time I get the data event callback from the <code>std::thread</code>, I <code>emit SignalUpdateData</code> which updates the <code>QML Text element</code> on <code>UI</code>.</p>
  
  <pre><code>void CallBackReceivedFromWorkerThread(float someValue) {
    emit SignalUpdateData(someValue)
  }
  </code></pre>
  
  <p>Following is how I have connected this <code>C++ signal</code> with the <code>QML slot</code></p>
  
  <pre><code>QObject::connect(this, SIGNAL(SignalUpdateData(QVariant)), myTextItemQObject, SLOT(slotUpdateData(QVariant)));
  </code></pre>
  
  <p><strong>And all of this works fine. No crashes, lock-ups, nothing.</strong></p>
  
  <p>As per my understanding, since the worker thread's function is triggering the callback, the execution control is on the worker thread when the callback is received. So when doing <code>emit SignalUpdateData(someValue)</code>, we'er still on the worker thread. <strong>And as far as I know from my previous experience in <code>android</code> &amp; <code>java</code>, we cannot update the UI from anywhere outside the <code>main thread</code> of the application.</strong> </p>
  
  <p>So, <em>How is this working ? Is <code>emit SignalUpdateData(someValue)</code> putting the call into the <code>main UI thread's event loop</code> ? Is <code>Qt</code> still making the UI change on <code>main thread</code> in spite of me calling for it from a <code>worker thread</code> ? If my approach is fine, then does it have performance implications ? What is the best recommendation to do this ?</em></p>
  
  <p><em>I want to be very sure about this &amp; not just lucky about making this to work. Should I use a <a href="http://doc.qt.io/qt-4.8/qt.html#ConnectionType-enum" rel="nofollow noreferrer">Qt::Connection_enum</a> as well for best approach ?</em></p>