0

/////////// this is the parent thread class //// serverstartThread.h

#ifndef SERVERSTARTTHREAD_H
#define SERVERSTARTTHREAD_H

#include <QObject>
#include <QDebug>
#include "QThread"

#include "listenerthread.h"

class ServerStart : public QObject
{
    Q_OBJECT

signals:
    void newClientConnectedSig();

public:
    explicit ServerStart(QObject *parent = nullptr);
    ~ServerStart();
    listenerThread* listenerthread;
    QThread* thread;

public slots:
    void run();
    void newClientConnectedSig2();

};

#endif // SERVERSTARTTHREAD_H

//// serverstartThread.cpp


#include "serverstartThread.h"

ServerStart::ServerStart(QObject *parent) : QObject(parent)
{

}

ServerStart::~ServerStart(){

}

void ServerStart::newClientConnectedSig2(){
    qInfo() << "helooooooooooooooooo";//this doesn't run
    emit newClientConnectedSig();
}

void ServerStart::run()
{
    qInfo() << "\nthread is running\n";


    //ListenForNewConnection
    listenerthread = new listenerThread();
    thread = new QThread(this);
    listenerthread->moveToThread(thread);
    QObject::connect(thread, &QThread::started, listenerthread, &listenerThread::run);
    QObject::connect(listenerthread, &listenerThread::newClientConnectedSig, this, &ServerStart::newClientConnectedSig2);
    thread->start();


    //functionthathasinfiniteloop();

    this->deleteLater();
}

/////////// this is the child thread class //// listenerThread.h

#ifndef LISTENERTHREAD_H
#define LISTENERTHREAD_H

#include <QObject>
#include <QDebug>
#include <QThread>

#include "clienthandlerThread.h"

class listenerThread : public QObject
{
    Q_OBJECT
public:
    explicit listenerThread(QObject *parent = nullptr);
    ~listenerThread();
    void run();
    clientHandlerThread* clienthandlerthread;
    QThread* thread;

public slots:

signals:
    void newClientConnectedSig();
};

#endif // LISTENERTHREAD_H

//// listenerThread.cpp

#include "listenerthread.h"

listenerThread::listenerThread(QObject *parent) : QObject(parent)
{
}

listenerThread::~listenerThread()
{
}

void listenerThread::run(){
    //does somthing here
    emit newClientConnectedSig();
    //does something here
}

1)inside my parent thread class serverstart.cpp i run a thread that runs the second class listenerthread.cpp (child thread).

2)inside the child thread class listenerthread.cpp i emit a signal.

3)inside serverstart.cpp i connect the signal, but QObject::connect() in the parent thread serverstart.cpp never receives the signal from the child thread listenerthread.cpp.

what i've tried

instead of running listenerthread.cpp in a thread, i made a pointer of it(listenerThread listenerthread = new listenerThread();). then listenerThread->run(); called the run method which emits a signal. and works.

which means

can't emit a signal from a child thread to the parent thread.

i really hope this is enough to be fully understood.

is this because of the Thread inside of a thread?

  • 2
    The implementation of `infiniteLoop` *does* potentially affect the behaviour. Does it return control to the Qt event processing loop at any point? If not then the `ServerStart` object can't receive queued signals while `ServerStart::run` is executing. – G.M. Oct 17 '21 at 10:06
  • Sorry, I find the request for a [mcve] reasonable. You probably debugged your code and failed to understand what happens before you posted your question. But you expect that anybody else just looks at it and tells you the mistake? Chances would be better if anyone could copy/paste your code to explore on her/his own what's happening. I mean multiple threads with inter-thread communication via signals is not that trivial stuff which you easily can debug by eyes... – Scheff's Cat Oct 17 '21 at 10:07
  • @Scheff'sCat no i totally understand what my code does and where it fails. the two threads that are nested are in there i have nothing else to add to this post unless you want my UI XML. i have a thread that is nested as a child of another thread and i want to emit a signal from the child thread to the parent thread, but the parent thread seems to not receive the signal with `QObject::connect();`. – anasouardini5 Oct 17 '21 at 10:46
  • what i've done to debug this is: instead of the child thread i just included the same class that i was moving to the child thread and made a pointer of it then called childThreadClass::run() method then the signal was received. my result is that emitting a signal from a child thread to the parent thread is not working, so there must be another way to do it. – anasouardini5 Oct 17 '21 at 10:46
  • i don't want to start a thread on a signal i just want to send a signal from a child thread to the parent thread so that the parent thread can send it to the mainwindow and change UI. – anasouardini5 Oct 17 '21 at 11:06
  • @G.M. you mean that the infinite loop inside the parent thread should stop in order to receive the signal from the child thread? – anasouardini5 Oct 17 '21 at 11:09
  • @G.M. but i do the same thing with a class instead of the child thread and it works. i just use the pointer of listenerThread and call listenerThread::run() which emits the signal and it works fine. – anasouardini5 Oct 17 '21 at 11:12
  • 2
    Yes, more or less. Queued connections use the event system so if you block the event system you will never receive the signals -- the corresponding events will just back up in the event queue. – G.M. Oct 17 '21 at 11:13
  • @G.M. i really need that loop to continue as long as the program is running. why it's working with class and not threads. – anasouardini5 Oct 17 '21 at 11:25
  • you can't help anyone can you? i'll just run them both from the main thread. – anasouardini5 Oct 17 '21 at 11:48
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Oct 17 '21 at 16:02
  • @Community it is just enough to understand my case. if i trimmed it i would be less understandable. – anasouardini5 Oct 18 '21 at 02:23

0 Answers0