0

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?

HARSH MITTAL
  • 750
  • 4
  • 17

1 Answers1

1

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

}

NoobNoob
  • 80
  • 1
  • 9
  • I had just one doubt in this approach how should I send the data from MyServer class to mainwindow? As the server class is initiated in main.cpp – HARSH MITTAL Apr 27 '21 at 17:44
  • 1
    You can do it in two ways: the first is simply do the connection in the main.cpp w.show(); MyServer server; QObject::connect(&server, SIGNAL(...), &w, SLOT(...)); The second is to passs a pointer to the mainwindow to MyServer constructor and then do the connection inside the constructor, but is ugly MyServer server(&w); Remember to #include – NoobNoob Apr 28 '21 at 07:26
  • Hey, thanks for the reply. I finally tested this out and is working fine. Another method of doing it was that I created a new thread in that I was calling server class. But using connect in main.cpp seems to be a better approach. – HARSH MITTAL May 25 '21 at 19:11