Results 1 to 6 of 6

Thread: Wait for key before exit in console app

  1. #1
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Wait for key before exit in console app

    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:
    Qt Code:
    1. int main() {
    2. QTextStream cout(stdout);
    3. QTextStream cin(stdin);
    4. // ...
    5. cout << "Press <ENTER> to exit." << endl;
    6. forever {
    7. QString Line = cin.readLine();
    8. if (!Line.isNull()) {
    9. break;
    10. }
    11. }
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 
    Code inside forever was taken from stackoverflow, but doesn't work for me. Qt 4.7.3
    Do you know some other solutions?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Wait for key before exit in console app

    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.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    R-Type (6th July 2012)

  4. #3
    Join Date
    Sep 2010
    Posts
    22
    Thanks
    4
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows Maemo/MeeGo

    Default Re: Wait for key before exit in console app

    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!

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Wait for key before exit in console app

    If you want simple then you have getchar()/getch()
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Wait for key before exit in console app

    well, getch is non standard and not always available
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Wait for key before exit in console app

    getchar() is standard.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. How to exit Qt console application?
    By arcelivez in forum Qt Programming
    Replies: 14
    Last Post: 30th July 2014, 08:51
  2. wait until a variable is set
    By kevindebb in forum Qt Programming
    Replies: 4
    Last Post: 7th May 2012, 06:03
  3. how to set wait condition
    By hema in forum Newbie
    Replies: 5
    Last Post: 25th July 2011, 08:08
  4. Replies: 2
    Last Post: 21st November 2010, 18:03
  5. Wait for a signal
    By adonel in forum Qt Programming
    Replies: 1
    Last Post: 13th May 2008, 19:53

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.