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 subclass closeEvent
    if you have QDialog then you should subclass reject

    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();
        }
    }
    


  • Thank you for reply. I also used

    this->setWindowFlags(Qt::WindowStaysOnTopHint);
    

    and

    QMainWindow::showFullScreen();
    

    to stay my confirmation message on top. that was my goal.


Log in to reply
 

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