PDA

View Full Version : Wait for key before exit in console app



R-Type
6th July 2012, 12:24
Hello,
I've simple console application and cannot find solution how to wait for some key before exit from it. It's simple with getchar() or std::cin.ignore, but I want to do it with Qt, because for console communication I already use it:


int main() {
QTextStream cout(stdout);
QTextStream cin(stdin);
// ...
cout << "Press <ENTER> to exit." << endl;
forever {
QString Line = cin.readLine();
if (!Line.isNull()) {
break;
}
}
return 0;
}

Code inside forever was taken from stackoverflow, but doesn't work for me. Qt 4.7.3
Do you know some other solutions?

wysota
6th July 2012, 12:40
QSocketNotifier opened on stdin and an event loop to keep your program running until there is something to read in the descriptor monitored by the notifier.

R-Type
6th July 2012, 14:14
Nice idea, but I think overhead of another class and event loop is too much just only to wait for key press. I thought about something simpe. It seems in console it's better to stay with std:: ...
Thanks anyway!

wysota
7th July 2012, 18:10
If you want simple then you have getchar()/getch() :)

amleto
7th July 2012, 19:34
well, getch is non standard and not always available

wysota
8th July 2012, 14:03
getchar() is standard.