Important: Please read the Qt Code of Conduct -

Catch emitted signal from multiple class

  • Hi everyone,

    I am developing an tcpserver application. I have newDataReceived slot and i emit a signal in it like,

    void myclass::newDataReceived()

    char data_received[1024] = {0};
    client->read(data_received, client->bytesAvailable());
    QString msg = data_received;
    QString client_ip = client->peerAddress().toString();
    emit dataReceived(msg,client_ip);

    i have catch the signal from MainWindow, there is no problem. But, i have another class which is QThread i want this class to catch this signal, too. But it does not do it. I connected the signal to my slot like,

    srv_thread = new myclass();

    What am i missing ?

    Thanks in advance

  • If objects that "live" in different threads are connected, a queued connection will be created, by default. However an object will only be able to process queued signals, if the thread to which that object belongs, is running an event loop! That's because in contrast to a direct connection (where the slot is called directly from the emit statement and thus from the context of the thread which called the emit), with a queued signal, the event loop of the thread where the receiver object "lives" will pick up the signal and then call the slot (i.e. the slot is now called from the context of the thread where the receiver object "lives", not from the sender's thread). So if you re-implemented QThead::run(), call QThead::exec() there in order to start an event loop in that thread...

Log in to reply