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. Why is event->ignore() ignored when I use QMessageBox in closeEvent while MainWindow is hidden?
Forum Updated to NodeBB v4.3 + New Features

Why is event->ignore() ignored when I use QMessageBox in closeEvent while MainWindow is hidden?

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 283 Views
  • 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.
  • namakoN Offline
    namakoN Offline
    namako
    wrote on last edited by
    #1

    Hi everyone.
    There is something I don't understand about MainWindos::closeEvent.
    If I use QMessageBox in MainWindow::closeEvent while MainWindow is hidden, event->ignore() does not work.
    Why is that?

    I have prepared a simple sample below.

    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    namespace Ui {
    class MainWindow;
    }
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    protected:
        void closeEvent(QCloseEvent* event);
    
    private slots:
        void on_pushButton_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    

    mainwindow.cpp

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QCloseEvent>
    #include <QMessageBox>
    #include <QDebug>
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    void MainWindow::closeEvent(QCloseEvent *event)
    {
      QMessageBox(QMessageBox::NoIcon, "title", "text").exec();
      event->ignore();
      qDebug() << event->isAccepted();
    }
    
    void MainWindow::on_pushButton_clicked()
    {
            hide();
            close();
    }
    
    

    There is only one QPushButton in the UI, named 'pushButton'.

    For some reason, pressing the button terminates the program.
    However, qDebug() will output 'false'.
    If there is no QMessageBox, it works fine, and if the MainWindow is shown, it works fine.

    Why does this program terminate?

    best regards,

    Debian 10.10 64bit
    Qt Creator 4.8.2 based on Qt 5.11.3
    QMake version 3.1
    Using Qt version 5.11.3 in /usr/lib/x86_64-linux-gnu

    JonBJ 1 Reply Last reply
    0
    • namakoN namako

      Hi everyone.
      There is something I don't understand about MainWindos::closeEvent.
      If I use QMessageBox in MainWindow::closeEvent while MainWindow is hidden, event->ignore() does not work.
      Why is that?

      I have prepared a simple sample below.

      mainwindow.h

      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      #include <QMainWindow>
      
      namespace Ui {
      class MainWindow;
      }
      
      class MainWindow : public QMainWindow
      {
          Q_OBJECT
      
      public:
          explicit MainWindow(QWidget *parent = nullptr);
          ~MainWindow();
      protected:
          void closeEvent(QCloseEvent* event);
      
      private slots:
          void on_pushButton_clicked();
      
      private:
          Ui::MainWindow *ui;
      };
      
      #endif // MAINWINDOW_H
      
      

      mainwindow.cpp

      #include "mainwindow.h"
      #include "ui_mainwindow.h"
      #include <QCloseEvent>
      #include <QMessageBox>
      #include <QDebug>
      
      MainWindow::MainWindow(QWidget *parent) :
          QMainWindow(parent),
          ui(new Ui::MainWindow)
      {
          ui->setupUi(this);
      }
      
      MainWindow::~MainWindow()
      {
          delete ui;
      }
      
      void MainWindow::closeEvent(QCloseEvent *event)
      {
        QMessageBox(QMessageBox::NoIcon, "title", "text").exec();
        event->ignore();
        qDebug() << event->isAccepted();
      }
      
      void MainWindow::on_pushButton_clicked()
      {
              hide();
              close();
      }
      
      

      There is only one QPushButton in the UI, named 'pushButton'.

      For some reason, pressing the button terminates the program.
      However, qDebug() will output 'false'.
      If there is no QMessageBox, it works fine, and if the MainWindow is shown, it works fine.

      Why does this program terminate?

      best regards,

      Debian 10.10 64bit
      Qt Creator 4.8.2 based on Qt 5.11.3
      QMake version 3.1
      Using Qt version 5.11.3 in /usr/lib/x86_64-linux-gnu

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @namako
      Does changing the value of QApplication::setQuitOnLastWindowClosed() affect the behaviour?

      namakoN 1 Reply Last reply
      2
      • JonBJ JonB

        @namako
        Does changing the value of QApplication::setQuitOnLastWindowClosed() affect the behaviour?

        namakoN Offline
        namakoN Offline
        namako
        wrote on last edited by
        #3

        @JonB Thanks for the reply.
        I gave it a quick try and confirmed that it works as expected.
        I had no idea that such a flag existed.
        Thanks for the great info and for your kindness.
        (^^)

        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