Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Why signal not work for a thread that is defined in a class?

Why signal not work for a thread that is defined in a class?

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 4 Posters 860 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • stackprogramerS Offline
    stackprogramerS Offline
    stackprogramer
    wrote on last edited by
    #1

    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)));
    
    JonBJ 1 Reply Last reply
    0
    • stackprogramerS Offline
      stackprogramerS Offline
      stackprogramer
      wrote on last edited by
      #9

      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

      JonBJ 1 Reply Last reply
      0
      • stackprogramerS stackprogramer

        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)));
        
        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #2

        @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 emits tcpThreadTriggerCommandForReactive(), or whether it does indeed do so from an instance which was connect() ed. You do not use new-style connect() 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 your connect(), so we do not even know if that connect() 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?

        stackprogramerS 1 Reply Last reply
        3
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #3

          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.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3
          • JonBJ JonB

            @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 emits tcpThreadTriggerCommandForReactive(), or whether it does indeed do so from an instance which was connect() ed. You do not use new-style connect() 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 your connect(), so we do not even know if that connect() 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?

            stackprogramerS Offline
            stackprogramerS Offline
            stackprogramer
            wrote on last edited by stackprogramer
            #4

            @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:)

            JonBJ 1 Reply Last reply
            0
            • stackprogramerS Offline
              stackprogramerS Offline
              stackprogramer
              wrote on last edited by
              #5

              I think when pointing classes are intertwined, the subject of pointers is always a challenging issue in C ++.

              1 Reply Last reply
              0
              • stackprogramerS stackprogramer

                @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:)

                JonBJ Offline
                JonBJ Offline
                JonB
                wrote on last edited by
                #6

                @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.

                1 Reply Last reply
                2
                • stackprogramerS Offline
                  stackprogramerS Offline
                  stackprogramer
                  wrote on last edited by
                  #7

                  @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

                  1 Reply Last reply
                  0
                  • S Offline
                    S Offline
                    SimonSchroeder
                    wrote on last edited by
                    #8

                    @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 the commandForReactive(bool) lives in does not have an event queue.

                    1 Reply Last reply
                    0
                    • stackprogramerS Offline
                      stackprogramerS Offline
                      stackprogramer
                      wrote on last edited by
                      #9

                      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

                      JonBJ 1 Reply Last reply
                      0
                      • stackprogramerS stackprogramer

                        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

                        JonBJ Offline
                        JonBJ Offline
                        JonB
                        wrote on last edited by
                        #10

                        @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.

                        1 Reply Last reply
                        0

                        • Login

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved