Qt发送信号到不同的线程

Bre*_*men 8 qt multithreading signals slot

我搜索过这个问题,但他们和我的有点不同.我的问题是我不想从另一个线程接收信号,但我想发送一个.接收在我的应用程序中工作,但在尝试发送时,我收到错误,我试图发送到另一个线程...我不知道如何解决这个问题.这是我的情况:我有一个Gui应用程序.在MainWindow类中,我创建了一个继承自Qthread的myThread对象.然后我开始新的线程.在MainWindow中存在一个信号,在myThread中存在一个插槽(此代码在主窗口中运行).当我尝试做的时候:

connect(this, SIGNAL(disconnect(),
        connectionThread, SLOT(stop()));
Run Code Online (Sandbox Code Playgroud)

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.
Run Code Online (Sandbox Code Playgroud)

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

PS:我试过直接链接.

connect(this, SIGNAL(disconnectFromOven()),
            connectionThread, SLOT(stop()), Qt::DirectConnection);
Run Code Online (Sandbox Code Playgroud)

这是代码: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
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

Zla*_*mir -1

QThread (connectionThread) 实例存在于实例化它的线程(主线程)中,而不是调用 run() 的线程中,因此如果您想调用槽,您需要使用此处文档中介绍的第一种方法,请查看工人-对象方法

  • 我对这篇文章投了反对票,因为它模糊地推迟了对文档的解释,甚至没有具体说明哪一部分。 (3认同)