Qt - Esc should not close the dialog

20,317

Solution 1

I think you may use this:

void MyDialog::keyPressEvent(QKeyEvent *e) {
    if(e->key() != Qt::Key_Escape)
        QDialog::keyPressEvent(e);
    else {/* minimize */}
}

Also have a look at Events and Event Filters docs.

Solution 2

Escape calls reject(). I override this function (in my case not to minimize the dialog but to prompt to save)

void MyDialog::reject() {if(cleanupIsOK()) done(0);}

Al_

Solution 3

Renaming the reject is correct. But be careful because if you want to close the dialog in other way you cannot call close.

MyDialog::reject(){
    if(some_closing_condition)
    {
        QDialog::reject() //calls the default close.
    }
    else
    {
        //skip reject operation
    }
}
Share:
20,317
Narek
Author by

Narek

Game developer.

Updated on July 09, 2022

Comments

  • Narek
    Narek almost 2 years

    How to make Esc key to minimize a dialog? By default it closes. Should I process KeyEvent or there is a better way?

  • n611x007
    n611x007 over 7 years
    nice docs Tyler! why not override instead of avoid inheriting though?