1

What I want to achieve here is, when I click the close button(X) or the standard buttons(OK|Yes|No|...) on the QMessageBox, instead of closing the QMessageBox, I just want to hide it.

I override the closeEvent(QCloseEvent *event) function, so when I click the close button(X), the QMessageBox will just hide. I did some research online to try to hide the QMessageBox when click the OK button, some people are saying I should override the QDialog::reject() function, but I did not make it work.
So here is a minimal example of what I am trying to do:

MessageBox::MessageBox(QWidget *parent) : QMessageBox(parent)
{
    this->setStandardButtons(QMessageBox::Ok);
    int ret = this->exec();

    switch (ret) {
    case QMessageBox::Ok:
        //confusing part
        break;
    default:
        break;
    }
}

void MessageBox::closeEvent(QCloseEvent *event)
{
    event->ignore();
    this->hide();
}

void MessageBox::reject()
{
    //I am confused about how to do this part to hide the message box when the OK button is clicked.
}

Hope someone can provide me a way to do this? Thanks.

Edit: Why I want to do this?
I have an app mostly running in the background, so most of the time, the windows are hid. When I click the check updates action from the tray icon menu, a QMesssageBox will pop up to show if there is a update available. And then if I click the buttons on the QMessageBox, the app will just quit. But also I've noticed, if I have an active window, when I click the buttons on the QMessageBox, the app will not quit. So I just want to stop my app from quitting after clicking buttons on the QMessageBox when there are no other active windows. Hope I explain myself clearly.

Mohammad Kanan
  • 4,452
  • 10
  • 23
  • 47
Theodore Tang
  • 831
  • 2
  • 21
  • 42

0 Answers0