How to catch QDockWidget close window event?



  • Hi!

    I am working on an application that handles several QDockWidget objects. When a user clicks on the Close button [x] of any of those widgets, I can trace this method:

    void QDockWidget::closeEvent(QCloseEvent *event)
    

    That's great but my requirement is different: I need to avoid that the QDockWidget get closed directly and instead, to show a warning dialog.

    Is this possible? Thanks!


  • Qt Champions 2017

    @xtingray said:
    Hi
    Im not completely sure why

    void MyDockWidget::closeEvent(QCloseEvent *event)
    {
    if (USERCONFIRM()) {
    event->accept();
    } else {
    event->ignore();
    }
    }

    Do not works for Docks as it does for say mainwindow?



  • @mrjj As far as I understand, the closeEvent() method is called AFTER the QDockWidget is already closed, that could be the reason your code doesn't work (just an idea).


  • Qt Champions 2017

    @xtingray
    Ok, so that is different from other widgets as there you
    can event->ignore() to avoid it closing completely.
    I have never tried with Dock though.



  • @mrjj I was wrong, you were right!!! I was testing this and it's working!

    void MyDockWidget::closeEvent(QCloseEvent *event)
    {
        event->ignore();
    }
    

    Thanks! :D


  • Qt Champions 2017

    Oh, so it was like other widgets. Good testing!
    Docks are very special so you never know :)
    super!


  • Qt Champions 2017

    @xtingray
    To stop the event, you're supposed to call accept(), meaning you've handled it. Here probably it's not very important, but I'd do it like this:

    void MyDockWidget::closeEvent(QCloseEvent * event)
    {
        event->accept();
        QDockWidget::closeEvent(event);
    }
    

    Kind regards.


  • Moderators

    @kshegunov Actually closeEvent is kinda special in that way and ignoring it is the way to prevent window from closing. It's different from other events but documented.


  • Qt Champions 2017

    @Chris-Kawa
    Oops! Good catch!


Log in to reply
 

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