how to handle asynchronous terminal input properly in a console application?
-
wrote on 15 Jul 2017, 10:09 last edited by VRonin
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:
- 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(); }
- what is the properly way to exist console application after receive cmd from terminal?
- 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 .
-
Hi,
This Github gist might be an interesting starting point.
-
Hi,
This Github gist might be an interesting starting point.
wrote on 19 Jul 2017, 02:22 last edited by@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
-
Hi,
This Github gist might be an interesting starting point.
wrote on 24 Jul 2017, 07:22 last edited by@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? -
@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?@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. -
@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.wrote on 24 Jul 2017, 08:35 last edited by@jsulm after reading the source code of 'TOP', i found it use infinite loop
(for(;;))
andSTDIN_FILENO
to monitor stdin and do some response.However, in Qt,
a.exec()
is much more common than infinite loop, so the way inTOP
seems doesn't suit for me. I have already useQCommandLineParser
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 ) -
@jsulm after reading the source code of 'TOP', i found it use infinite loop
(for(;;))
andSTDIN_FILENO
to monitor stdin and do some response.However, in Qt,
a.exec()
is much more common than infinite loop, so the way inTOP
seems doesn't suit for me. I have already useQCommandLineParser
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 )@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?
-
@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?
wrote on 24 Jul 2017, 08:46 last edited by@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()
-
@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()
@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
-
@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
Actually that sample do not accept any input.
At least not on win 10/Qt 5.8The QSocketNotifier never seems to signal.
Test project.
https://www.dropbox.com/s/azqyjfkg81vteev/console.zip?dl=0 -
@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
wrote on 24 Jul 2017, 09:30 last edited by@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/11