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

6th July 2012, 11:24
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()) {
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?

6th July 2012, 11: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.

6th July 2012, 13: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!

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

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

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