title: Post event to Qt GUI thread to simulate key press
body: <p>I want to simulate key presses in Qt. I have found some examples online, but have not got it working yet. I have this method:</p>
  
  <pre><code>void SimKeyEvent::pressTab()
  {
      QKeyEvent *event = new QKeyEvent(QEvent::KeyPress, Qt::Key_Space, Qt::NoModifier);
      QCoreApplication::postEvent(receiver, event);
  }
  </code></pre>
  
  <p>I don't know how to properly address the GUI thread which should be the 'receiver' in the code lines above. What I have tried is passing the 'app':</p>
  
  <pre><code>QGuiApplication app(argc, argv);
  </code></pre>
  
  <p>through the SimKeyEvent class constructer and made a private pointer to it.</p>
  
  <p>In main.cpp:</p>
  
  <pre><code>SimKeyEvent *simKeyEvent = new SimKeyEvent(0, &amp;app);
  </code></pre>
  
  <p>SimKeyEvent.h</p>
  
  <pre><code>private:
      QGuiApplication *app;
  </code></pre>
  
  <p>SimKeyEvent constructor:</p>
  
  <pre><code>SimKeyEvent::SimKeyEvent(QObject *parent, QGuiApplication *app) :
      QObject(parent)
  {
      this-&gt;app = app;
  }
  </code></pre>
  
  <p>Then I change to:</p>
  
  <pre><code>QCoreApplication::postEvent(app, event);
  </code></pre>
  
  <p>This did not work and I don't know if there is something wrong with the code or if its supposed to be done a different way.</p>
  
  <p>main.cpp:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      QGuiApplication app(argc, argv);
      QtQuick2ApplicationViewer viewer;
      viewer.setMainQmlFile(QStringLiteral("qml/GC/MainMenu.qml"));
  
      SimKeyEvent *simKeyEvent = new SimKeyEvent(0, &amp;app);
  
      viewer.showExpanded();
      return app.exec();
  }
  </code></pre>
  
  <p>EDIT:
  I also tried using 'this':
      QCoreApplication::postEvent(this, event);</p>