Why signal not work for a thread that is defined in a class?
-
Hi, In a class I defined a thread and I use it for some work.
For example:class TCPServer : public QTcpServer { Q_OBJECT public: explicit TCPServer(QObject *parent = 0); void StartServer(); TCPThread *tcpThread= new TCPThread(0); }
When I want to add in mainwindow.h a TCPServer class,I added a signal of a TCPthread to a slot in mainwindow.cpp,With this section?
But my signal does not emit or not work and so the slot is not executed....?
How can I solve my problem?connect((tcpServer.tcpThread), SIGNAL(tcpThreadTriggerCommandForReactive(bool)), this, SLOT(commandForReactive(bool)));
-
Finally, My problem is solved. Emit signal did not work because the thread was not started when the application is running ...
When a client is connected to a TCP socket, a TCP thread will be started ...This causes my problem...
So I added Connect method (signal slot) after the TCP thread was started ...Now everything is ok.
We should start the thread before using connect method for signal and slots.Thank from all. Good luck
-
@stackprogramer said in Why signal not work for a thread that is defined in a class?:
But my signal does not emit or not work and so the slot is not executed....?
How can I solve my problem?Nobody can tell from this description and the code shown.
We have no idea what the
TCPThread
class looks like. We do not know if/when your code emitstcpThreadTriggerCommandForReactive()
, or whether it does indeed do so from an instance which wasconnect()
ed. You do not use new-styleconnect()
syntax (which has been around for many years and you should always be using by now), and you do not check the return result of yourconnect()
, so we do not even know if thatconnect()
works. And so on.Why don't you start by connecting the thread's signal to a slot in the thread and see whether that gets called?
-
Noone who asks why treading does not work needs a threaded QTcpServer/QTcSocket. QTcpServer is async - no need for a separate thread. Be kind to yourself and don't use threads here - it will make things much easier and won't affect your program at all.
-
@JonB connect signal slot return true, But my question is that when I addressed a thread that is in another class, The pointer that uses connect signal returned true... (tcpServer.tcpThread) but emit signal not work correctly...
This section that should be emitted signal does not work correctly ...
emit tcpThreadTriggerCommandForReactive(true)
@Christian-Ehrlicher
I need the TCP server to create a TCP socket in a different thread because the main thread is busy with other tasks. I should TCP response is very fast so I need a new thread...we do not have a thread TCP, It may be increased to 4-5 TCP servers...
Async TCP slot is not appropriate for our applications.We want to try our luck with Threads:) -
I think when pointing classes are intertwined, the subject of pointers is always a challenging issue in C ++.
-
@stackprogramer said in Why signal not work for a thread that is defined in a class?:
This section that should be emitted signal does not work correctly ...
Qt signals work correctly, including across threads. So your code must be wrong. What did you do towards:
Why don't you start by connecting the thread's signal to a slot in the thread and see whether that gets called?
?
I should TCP response is very fast so I need a new thread
You do not need this/it won't increase speed given that Qt TCP is already asynchronous. If you understand asynchronicity how should threads help?
If you are writing a server to service many clients have you looked at Fortune Server Example? More specifically if you want threads to service each client see Threaded Fortune Server Example:
The Threaded Fortune Server example shows how to create a server for a simple network service that uses threads to handle requests from different clients. It is intended to be run alongside the Fortune Client example.
-
@JonB said in Why signal not work for a thread that is defined in a class?:
Qt signals work correctly, including across threads. So your code must be wrong. What did you do towards:
@JonB
Qt signals work correctly, including across threads. So your code must be wrong. What did you do towards:Yes, Surely my source code has a bug and must be wrong. Anyway I whill think when I fixed it I share my result. Thanks very much
-
@stackprogramer You don't provide enough information. What does your TCPThread look like? In which thread does the QTcpServer live in? Which thread does
this
live in in your connect statement? The most likely error is that the thread that your object with thecommandForReactive(bool)
lives in does not have an event queue. -
Finally, My problem is solved. Emit signal did not work because the thread was not started when the application is running ...
When a client is connected to a TCP socket, a TCP thread will be started ...This causes my problem...
So I added Connect method (signal slot) after the TCP thread was started ...Now everything is ok.
We should start the thread before using connect method for signal and slots.Thank from all. Good luck
-
@stackprogramer said in Why signal not work for a thread that is defined in a class?:
We should start the thread before using connect method for signal and slots.
We should preferably use connect method for signal and slots. before starting a thread, else it might emit the signal(s) before we get the
connect()
done. Basically as soon as we have the instance of the thread/object is the best time to do connections.