Handling MessgeBox in CloseEvent



  • 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


  • Moderators

    Hi,

    1. addButton() returns void so no point in using it to compare.
    2. exec() will return the value of what you have clicked.

    Have a look at the example "here":http://qt-project.org/doc/qt-5/qmessagebox.html#the-property-based-api.

    Later you can use the return value of exec() to call ignore() on "No" to avoid closing of the program.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.