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. how to handle asynchronous terminal input properly in a console application?

how to handle asynchronous terminal input properly in a console application?

Scheduled Pinned Locked Moved Unsolved General and Desktop
11 Posts 4 Posters 6.5k Views
  • 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.
  • S Offline
    S Offline
    SpartaWHY117
    wrote on last edited by VRonin
    #1

    Control program state is relatively easy in an UI application because we can add buttons or other UI components to control the state. But when comes to console application, it's really difficult for me because I am really newer of writing a console application. So i have following questions about this topic:

    1. after init arguments set in main function, how to receive the quit command(like "q") form terminal or cmd to stop the current task(periodically run)?does following code suitable?if not what's the usual design for console application control?
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
       //some work code that periodically run 
     while(1){
        in>>order;
        if(order=="show"){
        //some operation
           while(1)
           {
             in>>exit;
            if(exit=="q")
            {
              //stop code 
               break;
              }
           }
        }
    else if(xxxx){}
    
    }
        return a.exec();
    }
    
    1. what is the properly way to exist console application after receive cmd from terminal?
    2. timer in the main function can connect to slots in the other thread ?

    these are current questions i want to ask,maybe more questions will add during the discussion .

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      This Github gist might be an interesting starting point.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      S 2 Replies Last reply
      1
      • SGaistS SGaist

        Hi,

        This Github gist might be an interesting starting point.

        S Offline
        S Offline
        SpartaWHY117
        wrote on last edited by
        #3

        @SGaist I have tried your suggestion code but I'm just having trouble with your code on a Windows setup... I can't write in the console

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          This Github gist might be an interesting starting point.

          S Offline
          S Offline
          SpartaWHY117
          wrote on last edited by
          #4

          @SGaist if i want to our program works like TOP tool, when user some time push Q , the program will quit. so I should read cmd in main thread and do work in other thread?

          jsulmJ 1 Reply Last reply
          0
          • S SpartaWHY117

            @SGaist if i want to our program works like TOP tool, when user some time push Q , the program will quit. so I should read cmd in main thread and do work in other thread?

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @SpartaWHY117 You can do it like this, yes.
            Or, if you only need to do something on a regular basis, you can use a QTimer.

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            S 1 Reply Last reply
            0
            • jsulmJ jsulm

              @SpartaWHY117 You can do it like this, yes.
              Or, if you only need to do something on a regular basis, you can use a QTimer.

              S Offline
              S Offline
              SpartaWHY117
              wrote on last edited by
              #6

              @jsulm after reading the source code of 'TOP', i found it use infinite loop(for(;;)) and STDIN_FILENO to monitor stdin and do some response.

              However, in Qt, a.exec() is much more common than infinite loop, so the way in TOP seems doesn't suit for me. I have already use QCommandLineParser to handle init cmd, but have problem to end application. So what is the appropriate way to end the application when user pressed q?(my console application works flow like 'TOP', it will periodically display some info to termianl )

              jsulmJ 1 Reply Last reply
              0
              • S SpartaWHY117

                @jsulm after reading the source code of 'TOP', i found it use infinite loop(for(;;)) and STDIN_FILENO to monitor stdin and do some response.

                However, in Qt, a.exec() is much more common than infinite loop, so the way in TOP seems doesn't suit for me. I have already use QCommandLineParser to handle init cmd, but have problem to end application. So what is the appropriate way to end the application when user pressed q?(my console application works flow like 'TOP', it will periodically display some info to termianl )

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @SpartaWHY117 said in how to handle arguments from terminal properly in a console application?:

                So what is the appropriate way to end the application when user pressed q?

                Calling http://doc.qt.io/qt-5/qcoreapplication.html#exit

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                S 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @SpartaWHY117 said in how to handle arguments from terminal properly in a console application?:

                  So what is the appropriate way to end the application when user pressed q?

                  Calling http://doc.qt.io/qt-5/qcoreapplication.html#exit

                  S Offline
                  S Offline
                  SpartaWHY117
                  wrote on last edited by
                  #8

                  @jsulm I have learned that doc, maybe my question doesn't describe clear. what i really want to know is that the design pattern to handle anytime keyborad pressed during the Qt main event loop exec()

                  jsulmJ 1 Reply Last reply
                  0
                  • S SpartaWHY117

                    @jsulm I have learned that doc, maybe my question doesn't describe clear. what i really want to know is that the design pattern to handle anytime keyborad pressed during the Qt main event loop exec()

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @SpartaWHY117 Well, you can do it in a thread using the usual blocking stuff like std::cin, or something like this: https://gist.github.com/gjorquera/2576569

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    mrjjM S 2 Replies Last reply
                    1
                    • jsulmJ jsulm

                      @SpartaWHY117 Well, you can do it in a thread using the usual blocking stuff like std::cin, or something like this: https://gist.github.com/gjorquera/2576569

                      mrjjM Offline
                      mrjjM Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on last edited by mrjj
                      #10

                      @jsulm

                      Actually that sample do not accept any input.
                      At least not on win 10/Qt 5.8

                      The QSocketNotifier never seems to signal.

                      Test project.
                      https://www.dropbox.com/s/azqyjfkg81vteev/console.zip?dl=0

                      1 Reply Last reply
                      1
                      • jsulmJ jsulm

                        @SpartaWHY117 Well, you can do it in a thread using the usual blocking stuff like std::cin, or something like this: https://gist.github.com/gjorquera/2576569

                        S Offline
                        S Offline
                        SpartaWHY117
                        wrote on last edited by
                        #11

                        @jsulm on WIN10 , the sample code can't work.Maybe linux works well.

                        I found a solution on How to handle keypress events in a Qt console application?
                        . The final answer works well!

                        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