Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. show confirmation message and block screen before leave(close or loose focus) main window[SOLVED]
Qt 6.11 is out! See what's new in the release blog

show confirmation message and block screen before leave(close or loose focus) main window[SOLVED]

Scheduled Pinned Locked Moved General and Desktop
windowdesktop
3 Posts 2 Posters 3.8k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • GiorgiG Offline
    GiorgiG Offline
    Giorgi
    wrote on last edited by Giorgi
    #1

    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;
    }
    
    
    1 Reply Last reply
    0
    • HamedH Offline
      HamedH Offline
      Hamed
      wrote on last edited by Hamed
      #2

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

      HamedBabaeyan@yahoo.com
      for more interesting stuff

      1 Reply Last reply
      1
      • GiorgiG Offline
        GiorgiG Offline
        Giorgi
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved