Question about re-implementing closeEvent(QCloseEvent*)
-
Hi All,
I have a QWidget as the main window of my application (not a QMainWindow).
As you know this widget has a 'x' button on the top right corner that the user can press to exit the application.For my understanding pressing the 'x' button does not emit a signal therefor we have no ability to connect a SLOT to the event of the user pressing it.
My question is:
Can I implement the closeEvent() method as follows:void Base::closeEvent(QCloseEvent*) { if (QMessageBox::question(this,"shutdown",tr("are you sure to shutdown?"), QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::Yes) { close(); } }
And prevent the closing of the widget / app - if the user presses 'NO' ?
To be more precise I performed a check and pressing the 'x' button will show the popup but pressing 'NO' will not avoid the termination of the application.
Any suggestions how to solve this ?
Thanks -
@walle19 You should call accept() on the event object if you do not want it to be propagated further (which happens now, so the widget is closed).
See https://doc.qt.io/qt-5/qwidget.html#closeEvent -
@walle19
Further to @jsulm . I suggest you look at the following 2 patterns:-
https://stackoverflow.com/questions/13129380/show-a-dialog-before-closing-the-program-in-qt
-
https://forum.qt.io/topic/76053/proper-ask-on-close-implementation
The Accepted Solution on the first one (https://stackoverflow.com/a/38894383/489865) should be all you need.
-