can't emit a signal from a Thread that is inside of another Thread in QT(GUI)
-
/////////// 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(); //infinite loop 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.
what i think
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?
-
/////////// 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(); //infinite loop 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.
what i think
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?
@solidtyper
what's does your//functionthathasinfiniteloop();
actually look like, because its almost certainly the problem. -
it's a:
while(true){
//look in a vector then msleep(50).
}i though that this kinfd of thread is detached, so it doesn't matter if the thread never ends.
-
it's a:
while(true){
//look in a vector then msleep(50).
}i though that this kinfd of thread is detached, so it doesn't matter if the thread never ends.
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
while(true){
//look in a vector then msleep(50).
}Why don't you simply call exec() instead of functionthathasinfiniteloop()? Then you will have a working event loop in your thread.
-
it's a:
while(true){
//look in a vector then msleep(50).
}i though that this kinfd of thread is detached, so it doesn't matter if the thread never ends.
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
it's a:
while(true){
//look in a vector then msleep(50).
}i though that this kinfd of thread is detached, so it doesn't matter if the thread never ends.
and there's your problem, for slots to work properly, an event loop needs to run.
call exec() instead of while true and it should work fine.
You should potentially look into the worker approach of threading, may be more suited here.
Here if you're looking for working examples:
https://github.com/DeiVadder/QtThreadExample -
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
while(true){
//look in a vector then msleep(50).
}Why don't you simply call exec() instead of functionthathasinfiniteloop()? Then you will have a working event loop in your thread.
@jsulm said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
l exec() i
how can i call thread::exec(); inside my class that runs in qthread, it seems to be protected. is there a way without overriding the run() method of QThread.
-
@jsulm said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
l exec() i
how can i call thread::exec(); inside my class that runs in qthread, it seems to be protected. is there a way without overriding the run() method of QThread.
@solidtyper Subclass QThread instead of QObject and remove your "thread" member variable. Same applies to listenerThread class.
-
@jsulm said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
l exec() i
how can i call thread::exec(); inside my class that runs in qthread, it seems to be protected. is there a way without overriding the run() method of QThread.
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
how can i call thread::exec(); inside my class that runs in qthread, it seems to be protected. is there a way without overriding the run() method of QThread.
Multithreading is always complicated task, are you really sure you need multi-thread?
If you want to use multi-threading, then avoid to specialize the
QThread
class but create aQObject
based worker class and useQObject::moveToThread()
to move the class instance to the desired thread.
This is the Qt recommanded way.Take a look at this article which explaint it much better as I can do:
-
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
how can i call thread::exec(); inside my class that runs in qthread, it seems to be protected. is there a way without overriding the run() method of QThread.
Multithreading is always complicated task, are you really sure you need multi-thread?
If you want to use multi-threading, then avoid to specialize the
QThread
class but create aQObject
based worker class and useQObject::moveToThread()
to move the class instance to the desired thread.
This is the Qt recommanded way.Take a look at this article which explaint it much better as I can do:
@KroMignon i found a video of KDAB "Multithreading with Qt (Part 3) - QThread with an event loop".
that talks about event loop where he moves the object to the thread inside the constructor of the object and clean up in the deconstructor.
i'm very beginner, but i don't see how this is going to help me when what i need to do in the thread is lokking in the queue of received messages in a socket connection and doing something according to it as long as the app is runing.
-
@KroMignon i found a video of KDAB "Multithreading with Qt (Part 3) - QThread with an event loop".
that talks about event loop where he moves the object to the thread inside the constructor of the object and clean up in the deconstructor.
i'm very beginner, but i don't see how this is going to help me when what i need to do in the thread is lokking in the queue of received messages in a socket connection and doing something according to it as long as the app is runing.
@solidtyper
Just an observation: if you're going to have thread sleep and poll every 50 milliseconds, you can presumably do same withQTimer
without needing a thread for that.i'm very beginner
My suggestion is if you can avoid threads, do so.
-
@KroMignon i found a video of KDAB "Multithreading with Qt (Part 3) - QThread with an event loop".
that talks about event loop where he moves the object to the thread inside the constructor of the object and clean up in the deconstructor.
i'm very beginner, but i don't see how this is going to help me when what i need to do in the thread is lokking in the queue of received messages in a socket connection and doing something according to it as long as the app is runing.
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
i'm very beginner, but i don't see how this is going to help me when what i need to do in the thread is lokking in the queue of received messages in a socket connection and doing something according to it as long as the app is runing.
You are looking in the wrong direction ;)
If you want to useQObject
and signals/slots, you must have a working event loop, because it is there where the signals will be processed (cf. https://doc.qt.io/qt-5/signalsandslots.html).Qt is a asynchronous framework, so you have to think is this way: avoid using active wait or forever loops. Because this will lock the event loop and no signals could be processed!
As Qt is asynchronous, there is no really need to use threads to handle TCP/UPD sockets. You only have to connect to according signals to be informed when there is something to be done.
If you don't do heavy processing, you don't really need to create an additional thread to handle sockets.
-
@solidtyper
Just an observation: if you're going to have thread sleep and poll every 50 milliseconds, you can presumably do same withQTimer
without needing a thread for that.i'm very beginner
My suggestion is if you can avoid threads, do so.
@JonB what i can do is to run only one thead instead of one inside another, this single thread would do everything that both of these threads do.
but it's a pain to change all the code that prcess packets and such. that's why i asked for a method to do so instead. -
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
i'm very beginner, but i don't see how this is going to help me when what i need to do in the thread is lokking in the queue of received messages in a socket connection and doing something according to it as long as the app is runing.
You are looking in the wrong direction ;)
If you want to useQObject
and signals/slots, you must have a working event loop, because it is there where the signals will be processed (cf. https://doc.qt.io/qt-5/signalsandslots.html).Qt is a asynchronous framework, so you have to think is this way: avoid using active wait or forever loops. Because this will lock the event loop and no signals could be processed!
As Qt is asynchronous, there is no really need to use threads to handle TCP/UPD sockets. You only have to connect to according signals to be informed when there is something to be done.
If you don't do heavy processing, you don't really need to create an additional thread to handle sockets.
@KroMignon i have a thread that handles accepts cnx from listener sock and creates another one that takes care of that socket and all packets that it sends. i think this way is not going to fail when receiving many connections.
i wanted to run only thread that listens on any sock including listener and add tasks to the queue(adding new cnx & printing msg) then another thread takes care of accepting new cnx or if it's a message it prints is out in the UI. in this way i'm gonna have two threads in parrallel instead. which is easier.
if this doesn't work, or if it's too complicated for me that's hat i'm gonig to do.
but skipping threads all together!! i don't think that's possible for a multi-chat server.
-
@KroMignon i have a thread that handles accepts cnx from listener sock and creates another one that takes care of that socket and all packets that it sends. i think this way is not going to fail when receiving many connections.
i wanted to run only thread that listens on any sock including listener and add tasks to the queue(adding new cnx & printing msg) then another thread takes care of accepting new cnx or if it's a message it prints is out in the UI. in this way i'm gonna have two threads in parrallel instead. which is easier.
if this doesn't work, or if it's too complicated for me that's hat i'm gonig to do.
but skipping threads all together!! i don't think that's possible for a multi-chat server.
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
but skipping threads all together!! i don't think that's possible for a multi-chat server.
In that case have you looked at Threaded Fortune Server Example ?
-
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
but skipping threads all together!! i don't think that's possible for a multi-chat server.
In that case have you looked at Threaded Fortune Server Example ?
@JonB i guess this will really help, since it uses one thread for all. i'll give it a try.
-
@JonB i guess this will really help, since it uses one thread for all. i'll give it a try.
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
one thread for all
Depends what you mean by that. One thread of each client --- like your "multi-chat server."? This example is for multi-threaded:
The implementation of this example is similar to that of the Fortune Server example, but here we will implement a subclass of QTcpServer that starts each connection in a different thread.
-
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
multi-chat server.
Hi
If that is the goal, I must point to
https://wiki.qt.io/WIP-How_to_create_a_simple_chat_applicationAs it explains in detail the design of such app
-
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
one thread for all
Depends what you mean by that. One thread of each client --- like your "multi-chat server."? This example is for multi-threaded:
The implementation of this example is similar to that of the Fortune Server example, but here we will implement a subclass of QTcpServer that starts each connection in a different thread.
@JonB i didn't read the code carefully:
"QTcpServer::incomingConnection() creates a FortuneThread object, passing the incoming socket descriptor"i thought it only runs one thread that loops through each socket and prints msg if there is any and accpet cnx if there is any.
that's what i did right now using (fd_set, _clr, etc). it runs on one thread and waits for a connection then checks if its comming from listener sock(new cnx) or an existing sock(msg), and acts accordingly.
however i find it not very efficient.
what i think is a better solution is two threads in parallel: one listens for any cnx(new cnx/msg) and add tasks to a queue then the second one continuesly checks for tasks in the queue and do what it says.
i don't know if this also going to cause the same issue as before with nested threads in case i needed to communicate between them though.
-
@JonB i didn't read the code carefully:
"QTcpServer::incomingConnection() creates a FortuneThread object, passing the incoming socket descriptor"i thought it only runs one thread that loops through each socket and prints msg if there is any and accpet cnx if there is any.
that's what i did right now using (fd_set, _clr, etc). it runs on one thread and waits for a connection then checks if its comming from listener sock(new cnx) or an existing sock(msg), and acts accordingly.
however i find it not very efficient.
what i think is a better solution is two threads in parallel: one listens for any cnx(new cnx/msg) and add tasks to a queue then the second one continuesly checks for tasks in the queue and do what it says.
i don't know if this also going to cause the same issue as before with nested threads in case i needed to communicate between them though.
@solidtyper said in can't emit a signal from a Thread that is inside of another Thread in QT(GUI):
however i find it not very efficient.
How many sockets do you want to handle?
Are you really sure that using multiple thread will increase application performances?If you want to do multi-threading, please take this hints in account:
- avoid creating too many threads, the best is not to create more than your CPU has core (cf
QThread::idealThreadCount()
) - override
QTcpServer::incomingConnection()
to be able to create a QTcpSocket instance and move it in the working thread (as descibred in https://wiki.qt.io/WIP-How_to_create_a_simple_chat_application)
- avoid creating too many threads, the best is not to create more than your CPU has core (cf