Important: Please read the Qt Code of Conduct -

[SOLVED] QDialog how to catch all close events?

  • I have qDialog and on the @closeEvent(QCloseEvent *e) {}@ I can only catch if the user close the dialog from close button (x) only and not if the user cliched Ok or Cancel or hit the esc key. what event will do all this? I wist to emit a signal on the close event but reading the doc i've realised that the dialog will be hidden and not close on esc, Ok - accept(), or Cancel - reject(). hmm.. any good advise on how to catch all?

  • ok I've solved myself I do not know if this is the best option but here it is a work around or a solution :P

    in dialog construct I have
    @connect(this, SIGNAL(accepted()), this, SLOT(onCloseDialog()));
    connect(this, SIGNAL(rejected()), this, SLOT(onCloseDialog()));
    connect(this, SIGNAL(dialogIsClosing()), this, SLOT(onCloseDialog()));@

    and this
    @void DialogName::closeEvent(QCloseEvent *event){
    emit dialogIsClosing();

    and then
    @void DialogName::onCloseDialog{
    //what ever you want on the form hide or closing or rejected or accepted

    do not forget to add in the h file the emited signal for the close event.

  • If, when your dialog opens, you do this...


    ...then it will be closed, not hidden on any close event, etc., and in the...


    ...function, you can do some closing things and they will be reliably carried out.

  • @fyngyrz
    Yes, but readers should beware that you have to be very careful about what you do in a destructor. Depending on your needs, you may be better dealing with signals & slots.

Log in to reply