Reputation: 55
I want to create a message which appears in a new window when the user presses the exit button. Therefor I create a QCloseEvent, but the MainWindow actually doesn't close at all. What am I doing wrong?
Mainwindow.h
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
//Constructor
explicit MainWindow(QWidget *parent = 0);
//Destructor
~MainWindow();
public slots:
void closeMainWindow();
private:
QCloseEvent *event;
MainWindow.cpp
void MainWindow::closeMainWindow(){
event = new QCloseEvent();
QMessageBox::StandardButton answer = QMessageBox::question(
this,
tr("Close the Window"),
tr("Do you want to close the window?"),
QMessageBox::Yes | QMessageBox::No);
if(answer == QMessageBox::Yes){
event->accept();
}
else
event->ignore();
}
Upvotes: 3
Views: 1607
Reputation: 271
You are missing close() function
if(answer == QMessageBox::Yes)
{
event->accept();
close();
}
or override closeEvent() function of MainWindow
Refer below link: Generating a QCloseEvent won't close QMainWindow
Upvotes: 0
Reputation: 683
You must implement closeEvent function of mainwindow!
e.g
void MyMainWindow::closeEvent(QCloseEvent *event)
{
QMessageBox::StandardButton answer = QMessageBox::question(
this,
tr("Close the Window"),
tr("Do you want to close the window?"),
QMessageBox::Yes | QMessageBox::No);
if(answer == QMessageBox::Yes){
event->accept();
}
else
event->ignore();
}
Upvotes: 4