C++ 当对象未在主类中定义时,使用Qt信号和插槽

C++ 当对象未在主类中定义时,使用Qt信号和插槽,c++,qt,networking,qt5,C++,Qt,Networking,Qt5,我的代码具有以下结构: 包括 ->myserver.h ->mythread.h ->主窗口 src ->myserver.cpp ->mythread.cpp ->mainwindow.cpp main.cpp MyServer类为每个连接创建一个新线程。在该线程中,我正在从客户端读取数据,并希望将其发送到mainwindow.cpp。为此,我考虑使用信号和插槽。由于我没有在mainwindow中声明MyThread,因此无法使用connect() mythread.h: signa

我的代码具有以下结构:

  • 包括

    ->myserver.h

    ->mythread.h

    ->主窗口

  • src

    ->myserver.cpp

    ->mythread.cpp

    ->mainwindow.cpp

  • main.cpp

MyServer类为每个连接创建一个新线程。在该线程中,我正在从客户端读取数据,并希望将其发送到mainwindow.cpp。为此,我考虑使用信号和插槽。由于我没有在mainwindow中声明MyThread,因此无法使用connect()

mythread.h:

signals:
    void newDataRecieved(QVector<double> x,QVector<double> y);
myserver.cpp:

void MyServer::incomingConnection(qintptr socketDescriptor)
    {
    // We have a new connection
    qDebug() << socketDescriptor << " Connecting...";

    MyThread *thread = new MyThread(socketDescriptor, this);

    // connect signal/slot
    // once a thread is not needed, it will be beleted later
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

    connect(thread, SIGNAL(newDataRecieved(QVector<double>, QVector<double>)), this, SIGNAL(newDataRecieved(QVector<double>, QVector<double>)));

    thread->start();
void MyServer::incomingConnection(qintpttr socketDescriptor)
{
//我们有了新的联系
qDebug()创建一个信号

void newDataRecieved(QVector<double> x,QVector<double> y);
void newdatareceived(QVector x,QVector y);
在MyServer类中,然后将从MyThread接收的信号newdatareceived连接到MyServer的同一信号。然后在main窗口中,将插槽连接到MyServer的信号

[编辑]

大概是这样的:

myserver.h:

signals:
    void newDataRecieved(QVector<double> x,QVector<double> y);
信号:
接收的无效新数据(QVector x,QVector y);
myserver.cpp:

void MyServer::incomingConnection(qintptr socketDescriptor)
    {
    // We have a new connection
    qDebug() << socketDescriptor << " Connecting...";

    MyThread *thread = new MyThread(socketDescriptor, this);

    // connect signal/slot
    // once a thread is not needed, it will be beleted later
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

    connect(thread, SIGNAL(newDataRecieved(QVector<double>, QVector<double>)), this, SIGNAL(newDataRecieved(QVector<double>, QVector<double>)));

    thread->start();
void MyServer::incomingConnection(qintpttr socketDescriptor)
{
//我们有了新的联系

qDebug()请提供一个我对此方法只有一个疑问:我应该如何将数据从MyServer类发送到mainwindow?由于server类是在main.cpp中启动的,您可以通过两种方式来完成:第一种是在main.cpp w.show()中简单地进行连接;MyServer server;QObject::connect(&server,SIGNAL(…),&w,SLOT(…);第二种方法是将指向主窗口的指针传递给MyServer构造函数,然后在构造函数内部进行连接,但这是MyServer服务器(&w);记住#includeHey,谢谢你的回复。我最终测试了这一点,效果很好。另一种方法是,我创建了一个新线程,调用服务器类。但是在main.cpp中使用connect似乎是一种更好的方法。很高兴它有帮助。请将答案标记为已接受。干杯
signals:
    void newDataRecieved(QVector<double> x,QVector<double> y);
void MyServer::incomingConnection(qintptr socketDescriptor)
    {
    // We have a new connection
    qDebug() << socketDescriptor << " Connecting...";

    MyThread *thread = new MyThread(socketDescriptor, this);

    // connect signal/slot
    // once a thread is not needed, it will be beleted later
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

    connect(thread, SIGNAL(newDataRecieved(QVector<double>, QVector<double>)), this, SIGNAL(newDataRecieved(QVector<double>, QVector<double>)));

    thread->start();