views:

82

answers:

2

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

+1  A: 

I think that to do this, you would basically have to avoid inheriting from QDialog. The documentation for QDialog says:

Escape Key

If the user presses the Esc key in a dialog, QDialog::reject() will be called. This will cause the window to close: The close event cannot be ignored.

Tyler McHenry
+2  A: 

I think you may use this:

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.

mosg
You might want to right opposite things in if-else statement.
Narek
Hm, I think that it's correct: IF (not Excape) {/* ignore */} ELSE {/* hide */}
mosg
Please change your code to the following for I could accept your answer. Thanks! void MyDialog::keyPressEvent(QKeyEvent *e) { if(e->key() != Qt::Key_Escape) QDialog::keyPressEvent(e); else /*minimize*/ }
Narek
@Narek done it.
mosg
Change [Key_Escape -> Qt::Key_Escape] and also[myDialog::keyPressEvent -> void MyDialog::keyPressEvent]
Narek
@Narek Oh, yes, my fault, of course Qt::Key_Escape! :)
mosg