[SOLVED] Why QDialog does not receive a closeEvent on hitting Esc?



  • 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.


  • Moderators

    Hi and welcome to Devnet,

    can you show us your closeEvent implementation?



  • @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]



  • 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


  • Moderators

    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



  • 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



  • 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.



  • Hello ScumCoder..
    This is very easy to understand

    by 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- .


  • Moderators

    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.



  • Thanks Eddy,
    I tested by making simple project ans it's working and i found

    No 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).


  • Moderators

    It means that you should only reimplement it if

    1. you want to save somehow the last position of the dialog for later use.
    2. close the dialog in another way
    3. cancel the dialog in another way

    Hope this helps



  • 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@


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.