Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Multithreading Qt向不同的线程发送信号_Multithreading_Qt_Signals_Slot - Fatal编程技术网

Multithreading Qt向不同的线程发送信号

Multithreading Qt向不同的线程发送信号,multithreading,qt,signals,slot,Multithreading,Qt,Signals,Slot,我一直在寻找这个问题,但它们和我的有点不同。我的问题是,我不想从另一个线程接收信号,但我想发送一个。接收在我的应用程序中起作用,但当尝试发送时,我收到一个错误,我正试图发送到另一个线程。。。我不知道如何解决这个问题。 这就是我的情况: 我有一个Gui应用程序。在MainWindow类中,我创建了一个myThread对象,它继承自Qthread。 然后我开始新的线程。在主窗口中存在信号,在myThread中存在插槽(此代码在主窗口中运行)。当我尝试这样做时: connect(this, SIGNA

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

connect(this, SIGNAL(disconnect(),
        connectionThread, SLOT(stop()));
stop()是连接线程中的插槽,disconnect()是来自主窗口的信号。当信号发出时,我得到:

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
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
\ifndef TCPTHREAD\u H
#定义TCPTHREAD_H
#包括
#包括
#包括“tcpconnection.h”
类TcpThread:publicqthread
{
Q_对象
公众:
显式TcpThread(QString和address,
int和port,conType\u t conType,QObject*parent=0);
void run();//继承
信号:
无效连接状态OK();
无效连接已建立();
无效连接notestablished();
专用插槽:
无效停止();
无效连接关闭();
无效连接开放();
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();
}
#包括“tcpthread.h”
TcpThread::TcpThread(QString和address,
int和port,conType\u t conType,QObject*父项):
QThread(父线程)
{
这个->地址=地址;
此->端口=端口;
此->conTypeRequested=conType;
}
void TcpThread::stop()
{
qDebug()doDisconnect();
}

QThread(connectionThread)实例位于实例化它的线程(主线程)中,而不是位于调用run()的线程中,因此如果要调用slot,则需要使用文档中介绍的第一种方法,请查看辅助对象方法。

PS:我尝试了直接链接。对不起,我指的是连接:connect(这个,信号(disconnectFromOven()),connectionThread,SLOT(stop()),Qt::DirectConnection);这就是问题所在。只需删除Qt::DirectConnection即可。问题解决。这解决了我从ConnectionRead接收信号时的问题。反之亦然,当然行。或者你有其他问题。在线程之间发送和接收信号/插槽可能是Qt中最常用的线程技术之一。另外,我还提出了这个文档:嗨,我添加了这个->moveToThread(connectionThread);指针分配后,这也没有解决我的问题:/。我做得对吗?这不是主窗口吗?您不需要将主窗口移动到线程,请尝试以下操作:connectionThread->moveToThread(connectionThread);或者最好使用文档中提供的worker对象方法。不要忘记将finished()信号连接到ConnectionRead的deleteLater()插槽,这样就不会泄漏内存。我否决了这篇文章,因为它模糊地将解释推迟到文档中,甚至没有明确说明哪一部分。
#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();
}