show confirmation message and block screen before leave(close or loose focus) main window[SOLVED]
-
I want to show confirmation messagebox and block the screen before user leaves(alt + tab (close or loose focus) ) MainWindow. how to do this?
here is my code
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); QMainWindow::showFullScreen(); this->installEventFilter(this); } MainWindow::~MainWindow() { delete ui; } bool MainWindow::eventFilter(QObject *obj, QEvent *event){ if(event->type() == 128){ QMessageBox::information(this, "title", "text", QMessageBox::Ok | QMessageBox::Cancel); return true; } return true; }
-
if you have
QMainWindow
you can subclasscloseEvent
if you haveQDialog
then you should subclassreject
QMainWindow
example :void MainWindow::closeEvent (QCloseEvent *event) { QMessageBox::StandardButton resBtn = QMessageBox::question( this, APP_NAME,tr("You sure?\n"), QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes, QMessageBox::Yes); if (resBtn != QMessageBox::Yes) { event->ignore(); } else { event->accept(); } }
QDialog
example :void MyDialog::reject() { QMessageBox::StandardButton resBtn = QMessageBox::Yes; if (changes) { resBtn = QMessageBox::question( this, APP_NAME, tr("You sure?\n"), QMessageBox::Cancel | QMessageBox::No | QMessageBox::Yes, QMessageBox::Yes); } if (resBtn == QMessageBox::Yes) { QDialog::reject(); } }