PDA

View Full Version : QDialog and closeEvent



ricardo
10th July 2009, 22:16
Hi dudes!

Is there anyway to execute some code after the user press ESC key? I override closeEvent but it does not work (but it works if I press X window button)

I read on docs if ESC key is pressed, closeEvent can not be ignored, but I just want to execute some code.


Thanks a lot.

javimoya
11th July 2009, 17:59
override reject()

if you close a dialog with ESC, X window button or alt+f4 goes to reject()

ricardo
12th July 2009, 00:45
override reject()

if you close a dialog with ESC, X window button or alt+f4 goes to reject()

Thanks.

By the way, should I do something like this:




void CDialogLevelProperties::reject() {
// some code before canceling this dialog
// bla bla bla
QDialog::reject();
}

nish
12th July 2009, 04:18
ya ... this should work..

codeslicer
12th July 2009, 19:36
What I did was override the keyPressEvent:


void myClass::keyPressEvent(QKeyEvent* event) {
if (event->key() != Qt::Key_Escape) {
QDialog::keyPressEvent(event);
}
else {
close();
}
}

Then this fires the closeEvent() which you can then work with there. But perhaps the above solution is easier.

ricardo
12th July 2009, 21:05
What I did was override the keyPressEvent:


void myClass::keyPressEvent(QKeyEvent* event) {
if (event->key() != Qt::Key_Escape) {
QDialog::keyPressEvent(event);
}
else {
close();
}
}

Then this fires the closeEvent() which you can then work with there. But perhaps the above solution is easier.

Actually that was my former solution. That code does not work when user press X button. javimoya solution is better. Anyway, thanks.

nish
13th July 2009, 03:07
Actually that was my former solution. That code does not work when user press X button. javimoya solution is better. Anyway, thanks.

you have to accept the event and this will work


void myClass::keyPressEvent(QKeyEvent* event)
{
if (event->key() != Qt::Key_Escape)
{
QDialog::keyPressEvent(event);
}
else
{
event->accept();//Bye Bye event
close();
}
}