You are not logged in.

Dear visitor, welcome to QtForum.org. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Wednesday, June 3rd 2009, 10:45am

closeEvent override not working

Hello,

I'm trying to prevent a dialog from closing with the escape button by overriding the closeEvent.
I added

protected: void closeEvent(QCloseEvent *event);
to the header file, and implemented it as:

void goverDiag::closeEvent(QCloseEvent *event)
{
event->ignore();
}
It's not working though. If I push the escape button, it still closes. I can't think of another way to prevent it from closing.

2

Wednesday, June 3rd 2009, 7:16pm

You will have to capture keypress event for Esc key. Something like this should work:-

Source code

1
2
3
4
5
6
7
8
9
10
11
bool goverDiag::eventFilter( QObject /* *obj*/, QEvent *event )
{
if(QEvent::KeyPress == event->type())
{
if( Qt::Key_Escape == ((QKeyEvent*)event)->key())
{
return true; //ignore the event
}
return false;
}
}

3

Wednesday, June 3rd 2009, 8:13pm

That worked! Thanks :D

4

Friday, August 7th 2009, 4:08pm

You where doing it the right way (IMHO) with overriding closeEvent. The problem is that QEvent::ignore() means that YOU ignore the event so it will probably be propagated to the parent widget. Calling QEvent::accept() will do what you want.