0
votes

My code has following structure:

  • include

    -> myserver.h

    -> mythread.h

    -> mainwindow.h

  • src

    -> myserver.cpp

    -> mythread.cpp

    -> mainwindow.cpp

  • main.cpp

MyServer class creates a new thread for each connection. In that thread, I am reading the data from the client and want to send it to mainwindow.cpp. For this, I am thinking of using signal and slots. Since I have not declared MyThread in mainwindow, I am not able to use 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();
}

Is there any way to solve this?

1

1 Answers

1
votes

Create a signal

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

In MyServer class and then connect the signal newDataRecieved form MyThread to the same signal of MyServer. Then in mainwindow connect a slot to the signal form MyServer.

Is there a way trigger a signal from another signal in Qt?

[EDIT]

Something like this:

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

}