PDA

View Full Version : Handling MessgeBox in CloseEvent



ramin.lich
27th November 2014, 21:10
Hi,
why when i press no button the program will close?


void MainWindow::closeEvent(QCloseEvent *event){
QMessageBox msg(this);
msg.setText("Are you sure you want to Quit?");
QAbstractButton *no=msg.addButton(tr("No"),QMessageBox::ActionRole);
QAbstractButton *yes=msg.addButton(tr("Yes"),QMessageBox::ActionRole);
if(msg.clickedButton()==no){
event->ignore();
}
else{
event->accept();
}
msg.exec();
}

thank u

wysota
27th November 2014, 22:11
The code you posted makes no sense. With the current code, regardless what you do with the message box, the window will close. Try this:


void MainWindow::closeEvent(QCloseEvent *event) {
if(QMessageBox::question("Are you sure you want to quit?") != QMessageBox::Yes) event->ignore();
}

ramin.lich
28th November 2014, 09:00
thanks but
give me an error....

no matching function for call to 'QMessageBox::question(const char [31])'
^

wysota
28th November 2014, 09:58
So lookup the function in the docs and adjust your code to the required signature.