Qt发送信号到不同的线程

Qt send signal to different thread

我在 SO 上搜索了这个问题,但它们与我的有点不同。我的问题是我不想从另一个线程接收信号,但我想发送一个。接收在我的应用程序中工作,但是当尝试发送时,我收到错误消息,我正在尝试发送到另一个线程......我不知道如何解决这个问题。 这是我的情况: 我有一个 Gui 应用程序。在 MainWindow class 我创建了一个 myThread 对象,它继承自 Qthread。 然后我开始新线程。在 MainWindow 中存在信号,在 myThread 中存在槽(此代码在 main window 中为 运行)。当我尝试这样做时:

connect(this, SIGNAL(disconnect(),
        connectionThread, SLOT(stop()));

stop() 是连接线程中的槽,而 disconnect() 是来自 MainWindow 的信号。当信号发出时,我得到:

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.

有没有办法克服这个问题?我需要能够通过信号和槽向我创建的线程发送和接收事件。我真的会感谢所有帮助!

PS:我试过直接链接。

connect(this, SIGNAL(disconnectFromOven()),
            connectionThread, SLOT(stop()), Qt::DirectConnection);

这是代码: 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();

    // 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();
}

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();
}

QThread (connectionThread) 实例存在于实例化它的线程(主线程)中,而不是在调用 运行() 的线程中,所以如果你想调用槽你需要使用第一种方法在文档 here 中介绍,查看 工作对象方法