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



我的代码有以下结构:

  • 包括

    ->myserver.h

    ->mythread.h

    ->主窗口.h

  • src

    ->myserver.cpp

    ->mythread.cpp

    ->主窗口.cpp

  • main.cpp

MyServer类为每个连接创建一个新线程。在那个线程中,我正在从客户端读取数据,并希望将其发送到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();
}

主窗口.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类中,然后将信号newDataRecived从MyThread连接到MyServer的同一信号。然后在主窗口中,将一个插槽连接到信号窗体MyServer。

有没有办法从Qt中的另一个信号触发信号?

[EDIT]

类似这样的东西:

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

}

相关内容

  • 没有找到相关文章

最新更新