[SOLVED] Why QDialog does not receive a closeEvent on hitting Esc?
-
wrote on 31 Mar 2014, 08:58 last edited by
I've reimplemented the closeEvent method in my QDialog subclass. When the dialog is closed by clicking on the 'close' button with the mouse, this method is called OK. But if it is closed by hitting Esc key on the keyboard, the QCloseEvent is not delivered. Why?
I'm using Qt 4.8.5 on openSuSE 13.1 x64, Qt Creator 2.8.1 and gcc version 4.8.1 20130909. -
wrote on 31 Mar 2014, 09:00 last edited by
Hi and welcome to Devnet,
can you show us your closeEvent implementation?
-
wrote on 31 Mar 2014, 09:01 last edited by
@void SettingsDialogClass::closeEvent(QCloseEvent *event)
{
emit WindowClosed(); // emit a signal
this->QDialog::closeEvent(event);
}@
[quote author="Eddy" date="1396256429"]Hi and welcome to Devnet,can you show us your closeEvent implementation?[/quote]
-
wrote on 31 Mar 2014, 09:25 last edited by
In order to modify your dialog's close behavior, you can reimplement the functions accept(), reject() or done(). The closeEvent() function should only be reimplemented to preserve the dialog's position or to override the standard close or reject behavior
Hope it helps
PS :- From Qt Docs
-
wrote on 31 Mar 2014, 10:22 last edited by
Hi,
you should only connect the buttons clicked signal with the dialog's close Slot.
no need to subclass closeEvent.
on a side note, suppose you need to subclass, you only need to use
@
QDialog::closeEvent(event);
@without "this"
hope it helps
-
wrote on 31 Mar 2014, 11:42 last edited by
To answer to @ScumCoder question:
closeEvent is not called on Esc key press. It's not a bug, it's documented:
"Escape Key":http://qt-project.org/doc/qt-4.8/qdialog.html#escape-key -
wrote on 31 Mar 2014, 12:06 last edited by
Thanks all.
I didn't quite understand why the QCloseEvent is not delivered, but I just switched to hooking the QHideEvent instead and everything works now. -
wrote on 31 Mar 2014, 12:28 last edited by
Hello ScumCoder..
This is very easy to understandby default QDialog has Escape button in order to close the dialog.
if you use closeEvent() the it will not have default behavior like escaping button.[quote author="ScumCoder" date="1396256479"]@void SettingsDialogClass::closeEvent(QCloseEvent *event)
{
emit WindowClosed(); // emit a signal
this->QDialog::closeEvent(event);
}@
[quote author="Eddy" date="1396256429"]Hi and welcome to Devnet,can you show us your closeEvent implementation?[/quote]
[/quote]
-The reason is --Here you are reimplementing closeEvent() which will override the standard close or reject behavior.
hope you would understand- . -
wrote on 31 Mar 2014, 12:40 last edited by
bq. Here you are reimplementing closeEvent() which will override the standard close or reject behavior.
That's not correct since he is calling the default implementation in his custom closeEvent. The only thing extra here is emitting a signal which is not needed.
@
void Dialog::closeEvent(QCloseEvent * event)
{
emit WindowClosed();
QDialog::closeEvent(event); // calls the default
}
@This code should works. The ESC is not blocked by this. You can test it by making a small project with a QDialog and use the same closeEvent as above. But as already said it's not needed to subclass closeEvent.
There must be something else blocking the ESC key.
-
wrote on 31 Mar 2014, 14:31 last edited by
Thanks Eddy,
I tested by making simple project ans it's working and i foundNo need to use subclass closeevent/protected void closeEvent(QCloseEvent *);.
it is bydefault(Escape).
But my question : what is it mean ?
from docs.
The closeEvent() function should only be reimplemented to preserve the dialog’s position or to override the standard close or reject behavior(QDialog). -
wrote on 31 Mar 2014, 14:51 last edited by
It means that you should only reimplement it if
- you want to save somehow the last position of the dialog for later use.
- close the dialog in another way
- cancel the dialog in another way
Hope this helps
-
wrote on 20 May 2014, 12:13 last edited by
I have a related problem? On Ubuntu13.10 (Unity) and 14.04 Gnome, Qt5.2.1 and PyQt5, the close icon of the main window stops working after the user operates on the view (e.g. zoom). It could be something I am doing wrong, but I am reimplementing closeEvent().
Is the same advice pertinent for QMainWindow, to not reimplement closeEvent()? (I haven't studied what other signal I might connect.)
I'm thinking it is a problem with interfacing with the window manager, since this seems to have started happening since I upgraded from earlier Ubuntu. And that I should start a new thread.
@def closeEvent(self, event):
''' Handler for closeEvent from window manager. '''
if self.reallyQuit:
event.accept()
# Do not call _writeWindowAttributeSettings()
# Do not call app.quit(). Since WA_QuitOnClose is set, Qt will emit lastWindowClosed(), which will invoke quit().
else:
config.guiApp.closeDocument() # windowModal sheet on OSX, modal elsewhere
event.ignore() # wait to be called again after result of MaybeSave@