0

How to get out of infinite while loop by pressing esc in Qt?

while(1){
    //doing some task
    if(ESC_PRESSED)
        break;
}
Garf365
  • 3,619
  • 5
  • 29
  • 41
  • 1
    Do you have console or GUI application? Do you use threads? – Dmitry Sazonov May 26 '16 at 10:00
  • 2
    Possible duplicate of [How to handle keypress events in a Qt console application?](http://stackoverflow.com/questions/7543313/how-to-handle-keypress-events-in-a-qt-console-application) – demonplus May 26 '16 at 10:17

1 Answers1

2

I have little experience with qt but in general I would guess that you have to register somewhere to recieve something like a ESCKeyPressedEvent. When you recieve the event you would do something like this:

bool esc_was_pressed = false;
void on_esc_pressed(){
    esc_was_presed = true;
}

and in your loop

while(!esc_was_pressed){
    //doing some task
}

Actually I think you will just recieve a KeyPressedEvent and have to do something like:

void on_key_pressed(KEY key){
    if (key == ESC_KEY) esc_was_pressed = true;
}
463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185