在 main class 中未定义对象时使用 Qt Signal and Slots

Use Qt Signal and Slots when object is not defined in main class

我的代码结构如下:

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

mythread.h:

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

mythread.cpp:

void MyThread::func(){
   .
   .
   .
   emit newDataRecieved(x,yC);
}

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

mainwindow.h:

public slots:
    void newValues(QVector<double> x,QVector<double> y);

main.cpp:

.
.
#include "myserver.h"
int main(int argc, char *argv[])
{
    .
    .
    w.show();
    MyServer server;
    server.startServer();
    return a.exec();
}

有什么办法可以解决吗?

创建信号

void newDataRecieved(QVector<double> x,QVector<double> y);

在 MyServer class 中,然后将来自 MyThread 的信号 newDataRecieved 连接到 MyServer 的相同信号。然后在主窗口中将一个插槽连接到信号形式 MyServer。

[编辑]

像这样:

myserver.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();

}