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. Enter a loop until signal is emitted
Forum Updated to NodeBB v4.3 + New Features

Enter a loop until signal is emitted

Scheduled Pinned Locked Moved Solved General and Desktop
8 Posts 4 Posters 653 Views 1 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.
  • K Offline
    K Offline
    kayakaan02
    wrote on last edited by
    #1

    Hi, I catch a disconnected signal from my FTP Client, What I want to do is when disconnected signal is emitted, the program should start a loop to try reconnect.

    There is also a connected signal so I want to enter a loop to send a request for connection to server until the connection is made.

    I Use connect() to catch disconnect signal like this:

    QObject::connect(&controlChannel.m_socket, &QTcpSocket::disconnected,[] (){
           
        qDebug() << "disconnected";
    
        //enter a loop to send connection request until `QTcpSocket::connected` signal is emitted.
    
    });
    

    Thanks for advice.

    JonBJ Christian EhrlicherC 2 Replies Last reply
    0
    • K kayakaan02

      Hi, I catch a disconnected signal from my FTP Client, What I want to do is when disconnected signal is emitted, the program should start a loop to try reconnect.

      There is also a connected signal so I want to enter a loop to send a request for connection to server until the connection is made.

      I Use connect() to catch disconnect signal like this:

      QObject::connect(&controlChannel.m_socket, &QTcpSocket::disconnected,[] (){
             
          qDebug() << "disconnected";
      
          //enter a loop to send connection request until `QTcpSocket::connected` signal is emitted.
      
      });
      

      Thanks for advice.

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

      @kayakaan02 said in Enter a loop until signal is emitted:

      should start a loop to try reconnect.

      You would do this via QEventLoop::exec(). However I would not do this, you would be making the disconnected signal handler not exit until a new connection has been made. This might either not work, or disrupt the normal pattern of slots exiting so that the main Qt event loop is re-entered.

      Send your "request for reconnection" from the slot here by all means, but why wait/block until it has reconnected (which may never even happen) from here? Don't. Let the disconnected handler exit after sending the reconnect request, and handle the reconnection/QTcpSocket::connected signal as when it arrives through a separate slot in code.

      K 1 Reply Last reply
      0
      • K Offline
        K Offline
        kayakaan02
        wrote on last edited by
        #3

        @JonB said in Enter a loop until signal is emitted:

        You would do this via QEventLoop::exec(). However I would not do this, you would be making the disconnected signal handler not exit until a new connection has been made. This might either not work, or disrupt the normal pattern of slots exiting so that the main Qt event loop is re-entered.

        Thanks for the answer, the client program is fully automated relying on the connection so it can be dangerous continuing while there is no connection.

        But can it cause the other connect() functions to stop working?

        JonBJ 1 Reply Last reply
        0
        • K kayakaan02

          @JonB said in Enter a loop until signal is emitted:

          You would do this via QEventLoop::exec(). However I would not do this, you would be making the disconnected signal handler not exit until a new connection has been made. This might either not work, or disrupt the normal pattern of slots exiting so that the main Qt event loop is re-entered.

          Thanks for the answer, the client program is fully automated relying on the connection so it can be dangerous continuing while there is no connection.

          But can it cause the other connect() functions to stop working?

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

          @kayakaan02 said in Enter a loop until signal is emitted:

          so it can be dangerous continuing while there is no connection.

          I don't know exactly what you mean by this, but in that case isn't your "client program" just as much at "danger" of this during whatever you choose to implement for

          //enter a loop to send connection request until QTcpSocket::connected signal is emitted.

          as if you allow it to exit and re-enter the main event loop?

          But can it cause the other connect() functions to stop working?

          You have not allowed Qt to return from the disconnected handler yet in your proposed reconnect code. If there is any "clean up" it would do after emitting the disconnected signal (I don't know, but there might be?) that has not been allowed to take place. I don't know but that might "mess up" your attempt to reconnect.

          1 Reply Last reply
          0
          • K kayakaan02

            Hi, I catch a disconnected signal from my FTP Client, What I want to do is when disconnected signal is emitted, the program should start a loop to try reconnect.

            There is also a connected signal so I want to enter a loop to send a request for connection to server until the connection is made.

            I Use connect() to catch disconnect signal like this:

            QObject::connect(&controlChannel.m_socket, &QTcpSocket::disconnected,[] (){
                   
                qDebug() << "disconnected";
            
                //enter a loop to send connection request until `QTcpSocket::connected` signal is emitted.
            
            });
            

            Thanks for advice.

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by Christian Ehrlicher
            #5

            @kayakaan02 said in Enter a loop until signal is emitted:

            I Use connect() to catch disconnect signal like this:
            QObject::connect(&controlChannel.m_socket, &QTcpSocket::disconnected,[] (){

            qDebug() << "disconnected";
            
            //enter a loop to send connection request until `QTcpSocket::connected` signal is emitted.
            

            });

            As said in the other post - don't do this, fix your code.

            /edit: here: https://forum.qt.io/topic/141738/qeventloop-occupy-cpu-would-qwaitcondition-will-release-cpu-and-also-be-waked

            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
            1
            • JonBJ JonB

              @kayakaan02 said in Enter a loop until signal is emitted:

              should start a loop to try reconnect.

              You would do this via QEventLoop::exec(). However I would not do this, you would be making the disconnected signal handler not exit until a new connection has been made. This might either not work, or disrupt the normal pattern of slots exiting so that the main Qt event loop is re-entered.

              Send your "request for reconnection" from the slot here by all means, but why wait/block until it has reconnected (which may never even happen) from here? Don't. Let the disconnected handler exit after sending the reconnect request, and handle the reconnection/QTcpSocket::connected signal as when it arrives through a separate slot in code.

              K Offline
              K Offline
              kayakaan02
              wrote on last edited by
              #6

              @JonB Sorry for disturbing again but, I couldn't find out how can I do that with QEventLoop, All the examples on the internet I found just waits for something to happen but I want to make a loop(also wait a couple of seconds after each iteration for the sake of my CPU) to send requests, and then exit the loop if I catch a signal as I said.

              It could be too much but can you provide an example on how to do that?

              Thanks in advance.

              JonBJ 1 Reply Last reply
              0
              • K kayakaan02

                @JonB Sorry for disturbing again but, I couldn't find out how can I do that with QEventLoop, All the examples on the internet I found just waits for something to happen but I want to make a loop(also wait a couple of seconds after each iteration for the sake of my CPU) to send requests, and then exit the loop if I catch a signal as I said.

                It could be too much but can you provide an example on how to do that?

                Thanks in advance.

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

                @kayakaan02
                If you insist on doing things as you show, where you want to

                //enter a loop to send connection request until `QTcpSocket::connected` signal is emitted.
                

                inside lambda you would need something like

                QEventLoop loop;
                QObject::connect(&controlChannel.m_socket, &QTcpSocket::connected, [&loop] () { qDebug() << "connected"; loop.quit(); });
                loop.exec();
                

                inside your current &QTcpSocket::disconnected lambda.

                But as I said I don't think you should do it this way, and I don't even know if it would work while still inside disconnected slot. I would not put anything inside the disconnected slot, I would let that exit, and return to the main Qt event loop, and do your waits/loops/sends/receives via signals/slots from the main event loop processing without trying to "block" or do it inside the disconnect slot code.

                1 Reply Last reply
                1
                • Kent-DorfmanK Offline
                  Kent-DorfmanK Offline
                  Kent-Dorfman
                  wrote on last edited by
                  #8

                  No loops! they are not the Qt way...

                  Your disconnect catch slot should set a qtimer that tries reconnect on expire and keep a counter of how many times you intend to retry the connect timer. No futzing with sub-event-loops.

                  1 Reply Last reply
                  1

                  • Login

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