Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Qt - Esc should not close the dialog

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

like image 587
Narek Avatar asked Jun 27 '10 11:06

Narek


3 Answers

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.

like image 108
mosg Avatar answered Oct 09 '22 14:10

mosg


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_

like image 21
Al_ Avatar answered Oct 09 '22 13:10

Al_


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
    }
}
like image 33
Andres Peralta Avatar answered Oct 09 '22 13:10

Andres Peralta