Solved How to catch QDockWidget close window event?
-
Hi!
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!
-
@xtingray said:
Hi
Im not completely sure whyvoid 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).
-
@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
-
Oh, so it was like other widgets. Good testing!
Docks are very special so you never know :)
super! -
@xtingray
To stop the event, you're supposed to callaccept()
, 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.
-
@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. -
@Chris-Kawa
Oops! Good catch!