Click here to Skip to main content
15,881,811 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
//// serverstartThread.h
<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
C++
#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();


    infiniteLoop();

    this->deleteLater();
}



//// listenerThread.h
C++
#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

C++
#include "listenerthread.h"

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

listenerThread::~listenerThread()
{
}

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


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().
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900