I was looking for SO for this question, but they were slightly different from mine. My problem is that I do not want to receive a signal from another stream, but I want to send it. Getting work in my application, but when I try to send, I get an error that I try to send to another thread ... I do not know how to fix it. This is my situation: I have a Gui application. In the MainWindow class, I create a myThread object that is inherited from Qthread. Then I start a new thread. A signal exists in MainWindow, and a slot exists in myThread (this code runs in the main window). When I try to do:
connect(this, SIGNAL(disconnect(), connectionThread, SLOT(stop()));
stop () is the slot in the connection stream, and disconnect () is the signal from MainWindow. When a signal is emitted, I get:
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.
Is there any way to overcome this? I need to be able to send and receive events through signals and slots in and out of the stream that I created. I would really seduce all the help!
PS: I tried the direct link.
connect(this, SIGNAL(disconnectFromOven()), connectionThread, SLOT(stop()), Qt::DirectConnection);
This is the code: Mainwindow.h:
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> 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
mainwindow.cpp:
#include "mainwindow.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); initComponents(); QString ip = "10.10.10.17"; int port = 5432; connectionThread = new TcpThread(ip, port, connectAndStay); show();
tcpthread.h:
#ifndef TCPTHREAD_H #define TCPTHREAD_H #include <QThread> #include <QDebug> #include "tcpconnection.h" class TcpThread : public QThread { Q_OBJECT public: explicit TcpThread(QString& address, int& 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
tcpthread.cpp:
#include "tcpthread.h" TcpThread::TcpThread(QString& address, int& port, conType_t conType, QObject *parent) : QThread(parent) { this->address = address; this->port = port; this->conTypeRequested = conType; } void TcpThread::stop() { qDebug() << "Thread stop called, requesting disconnect"; tcpCon->doDisconnect(); }