Show dialog before closing a program in Qt

I want to show the dialog before closing the program in Qt, whether the user wants to cancel or save the program, that is, by clicking on cancel, the user has a chance to return to the program with an uncleaned state, for example, with a window paint or notebook in which a warning dialog appears before closing warning users? by the way i'm using qt

+7
source share
2 answers

If your application uses QMainWindow , overload closeEvent() to display the dialog, and only call QMainWindow::closeEvent if the user clicked ok in your dialog box.

If your application uses QDialog , reload the accept() slot and only call QDialog::accept if the user clicked ok in your dialog box.

+12
source

you can use the solution described here: http://www.codeprogress.com/cpp/libraries/qt/HandlingQCloseEvent.php

you can just override the closeEvent function:

 #include <QCloseEvent> #include <QMessageBox> void MainWindow::closeEvent(QCloseEvent *event) // show prompt when user wants to close app { event->ignore(); if (QMessageBox::Yes == QMessageBox::question(this, "Close Confirmation", "Exit?", QMessageBox::Yes | QMessageBox::No)) { event->accept(); } } 
+2
source

All Articles