title: Qt send signal to different thread
body: <p>I have searched SO for this question, but they were a bit different than mine. My problem is that I dont want to receive a signal from another thread, but I want to send one. Receiving works in my app, but when trying to send, I am getting error that I am trying to send to another thread... I dont know how to fix this.
  This is my situation:
  I have a Gui application. In MainWindow class I create a myThread object, that is inherited from Qthread.
  I then start the new thread. In MainWindow a signal exists and in myThread a slot exists (this code is run in main window). When I try to do:</p>
  
  <pre><code>connect(this, SIGNAL(disconnect(),
          connectionThread, SLOT(stop()));
  </code></pre>
  
  <p>stop() is a slot in connection thread and disconnect() is a signal from MainWindow. When the signal is emited, i get:</p>
  
  <pre><code>ASSERT failure in QCoreApplication::sendEvent: "Cannot send events to objects owned by a different thread. Current thread 128f8250. Receiver '' (of type 'QNativeSocketEngine') was created in thread 14bae218", file kernel\qcoreapplication.cpp, line 521
  Invalid parameter passed to C runtime function.
  Invalid parameter passed to C runtime function.
  </code></pre>
  
  <p>Is there a way to overcome this? I need to be able to send and receive events through signals and slots to and from the thread I created. I Would really aprichiate all help!</p>
  
  <p>PS: I tried directlinking.</p>
  
  <pre><code>connect(this, SIGNAL(disconnectFromOven()),
              connectionThread, SLOT(stop()), Qt::DirectConnection);
  </code></pre>
  
  <p>This is the code:
  MainWindow.h:</p>
  
  <pre><code>#ifndef MAINWINDOW_H
  #define MAINWINDOW_H
  
  #include &lt;QMainWindow&gt;
  
  
  namespace Ui {
  class MainWindow;
  }
  
  class MainWindow : public QMainWindow
  {
      Q_OBJECT
  
  public:
      explicit MainWindow(QWidget *parent = 0);
      ~MainWindow();
  
      void moveUndoView();
  
  signals:
      void disconnectFromOven();
  
  };
  
  #endif // MAINWINDOW_H
  </code></pre>
  
  <p>MainWindow.cpp:</p>
  
  <pre><code>#include "mainwindow.h"
  
  
  MainWindow::MainWindow(QWidget *parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow)
  {
      ui-&gt;setupUi(this);
      initComponents();
  
      QString ip = "10.10.10.17";
      int port = 5432;
      connectionThread = new TcpThread(ip, port, connectAndStay);
  
      show();
  
      // tcp connection
      connect(connectionThread, SIGNAL(connectionEstablished()),
              this, SLOT(on_connectionEstablished()));
  
      connect(connectionThread, SIGNAL(connectionNotEstablished()),
                  this, SLOT(on_connectionNotEstablished()));
  
      connect(this, SIGNAL(disconnectFromOven()),
              connectionThread, SLOT(stop()));
  
  
  }
  
  
  void MainWindow::on_action_Disconnect_triggered()
  {
      emit disconnectFromOven();
  }
  </code></pre>
  
  <p>tcpthread.h:</p>
  
  <pre><code>#ifndef TCPTHREAD_H
  #define TCPTHREAD_H
  
  #include &lt;QThread&gt;
  #include &lt;QDebug&gt;
  
  #include "tcpconnection.h"
  
  class TcpThread : public QThread
  {
      Q_OBJECT
  public:
      explicit TcpThread(QString&amp; address,
                         int&amp; port, conType_t conType, QObject *parent = 0);
      void run(); // inherited
  
  
  signals:
      void connectionStatusOk();
      void connectionEstablished();
      void connectionNotEstablished();
  
  private slots:
      void stop();
      void connectionClosed();
      void connectionOpened();
      void connectionOpenFailed();
  
  };
  
  #endif // TCPTHREAD_H
  </code></pre>
  
  <p>tcpthread.cpp:</p>
  
  <pre><code>#include "tcpthread.h"
  
  TcpThread::TcpThread(QString&amp; address,
                       int&amp; port, conType_t conType, QObject *parent) :
      QThread(parent)
  {
      this-&gt;address = address;
      this-&gt;port = port;
      this-&gt;conTypeRequested = conType;
  }
  
  void TcpThread::stop()
  {
      qDebug() &lt;&lt; "Thread stop called, requesting disconnect";
      tcpCon-&gt;doDisconnect();
  }
  </code></pre>