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
2010-06-27 12:17:12
+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
2010-06-27 15:18:31
You might want to right opposite things in if-else statement.
Narek
2010-06-27 16:30:06
Hm, I think that it's correct: IF (not Excape) {/* ignore */} ELSE {/* hide */}
mosg
2010-06-28 05:43:02
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
2010-07-04 05:02:22
@Narek done it.
mosg
2010-07-05 06:10:37
Change [Key_Escape -> Qt::Key_Escape] and also[myDialog::keyPressEvent -> void MyDialog::keyPressEvent]
Narek
2010-07-05 08:26:47
@Narek Oh, yes, my fault, of course Qt::Key_Escape! :)
mosg
2010-07-05 08:50:17