Why is event->ignore() ignored when I use QMessageBox in closeEvent while MainWindow is hidden?
-
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 -
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 -
@namako
Does changing the value of QApplication::setQuitOnLastWindowClosed() affect the behaviour?