<pre lang="C++">
#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"; emit newClientConnectedSig();
}
void ServerStart::run()
{
qInfo() << "\nthread is running\n";
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();
infiniteLoop();
this->deleteLater();
}
//// 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(){
emit newClientConnectedSig();
}
1) when i connect the newClientConnectedSig() signal inside listenerThread.cpp and run a slot it works fine. so emiting a signal from listenerThread.cpp has no problem.
2) but when connect is inside of serverstartThread as shown in the snipets it doesn't receive the signal from listenerThread().
**is this because of the Thread inside of a thread?**
What I have tried:
1) when i connect the newClientConnectedSig() signal inside listenerThread.cpp and run a slot it works fine. so emiting a signal from listenerThread.cpp has no problem.
2) but when connect is inside of serverstartThread as shown in the snipets it doesn't receive the signal from listenerThread().