title: How to wait for QThread eventDispatcher being ready?
body: <p>I'm using <code>QThread</code>'s event loop to communicate with worker threads.</p>
  <p>I noticed that I can't use the thread's event loop just after a <code>start</code>.</p>
  <p>The following code does not work, as <code>threadEventDispatcher</code> appears to be NULL.</p>
  <pre class="lang-cpp prettyprint-override"><code>QThread *thread = new QThread;
  thread-&gt;start();
  auto* threadEventDispatcher = thread-&gt;eventDispatcher();
  Q_ASSERT(threadEventDispatcher != nullptr);
  QMetaObject::invokeMethod(threadEventDispatcher, [] { printf(&quot;Hello world\n&quot;);
  </code></pre>
  <p>According to <a href="https://doc.qt.io/qt-6.2/qthread.html#setEventDispatcher" rel="nofollow noreferrer">Qt doc</a>,</p>
  <blockquote>
  <p>An event dispatcher is automatically created for the main thread when QCoreApplication is instantiated and on start() for auxiliary threads.</p>
  </blockquote>
  <p>By browsing the QThread source code, I'm under the impression that the <code>eventDispatcher</code> is created in the running thread, which may not yet be ready when <code>start</code> exits.</p>
  <p>I managed to access the <code>eventDispatcher</code> by using a <em>sleep</em> after the <code>start</code>, but I could not find an API that would allow me to properly wait for the thread to actually <em>run</em>.</p>
  <p>How can I wait for the thread to actually run, and access <code>eventDispatcher</code> ?</p>