Important: Please read the Qt Code of Conduct -

[SOLVED] QMessageBox Buttons

  • Hi

    I have a QDialog and when pressed close, i want to open a QMessageBox and ask whether or not to save.

    Normally one would use

    QMessageBox::StandardButton reply;
    reply = QMessageBox::question(this
                                  , "Closing"
                                  , "Do you want to save?"
                                  , QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel);

    But i want some more features. First is to be able to change buttons' texts. Secondly i want to make Yes button unclickable, if some condition is not met.

    So i figured out i need to manually do it

        QMessageBox messageBox;
        messageBox.setWindowTitle("Do you want to save?");
        messageBox.addButton(BUTTON_YES, QMessageBox::YesRole);
        messageBox.addButton(BUTTON_NO, QMessageBox::NoRole);
        messageBox.addButton(BUTTON_CANCEL, QMessageBox::RejectRole);
        QList<QAbstractButton*> messageBoxButtons = messageBox.buttons();
        // How do i get buttons properly? 
        connect(, SIGNAL(clicked(bool))
                , this, SLOT(messageBoxYesClicked(bool)));
        connect(, SIGNAL(clicked(bool))
                , this, SLOT(messageBoxNoClicked(bool)));
        connect(, SIGNAL(clicked(bool))
                , this, SLOT(messageBoxCancelClicked(bool)));
        if(canSaveContent() == false)
            // Set YesRole button to disabled
        if(m_reply == QMessageBox::Yes)
            // Do something
        else if(m_reply == QMessageBox::No)
            // Do something
            // Do something

    These functions merely sets m_reply with proper values.

    void DialogEntryNote::messageBoxYesClicked(bool checked)
        m_reply = QMessageBox::Yes;

    How do i get buttons properly? I want to connect the button' clicked() signal with function slots i prepared.

    I think my current method is lame. I want to do it the right way. What sort of methods should i follow?

    Thank you in advance for any advice.

  • Hi,

    you forget that QMessageBox::exec() returns the code of clicked button; so

    QMessageBox mbox;
    m_reply = mbox.exec();

    is enough to know which button was clicked (no signals, slots)

  • See the maybeSave() function in this example.

  • @mcosta exec() returns int. Can you eleborate a little bit more please?

    Also doc says

    When using QMessageBox with custom buttons, this function returns an opaque value; use clickedButton() to determine which button was clicked.

    Does button roles acts the same as standard buttons?

    I can check the text on the clicked button for that purpose maybe.

  • Hi,

    using msgBox.standardButton(msgBox.clickedButton()) you can get the code

  • @mcosta msgBox.standardButton(msgBox.clickedButton()) always returns QMessageBox::NoButton.

    Returns the standard button enum value corresponding to the given button, or NoButton if the given button isn't a standard button.

    I assume thats because messageBox.addButton(BUTTON_YES, QMessageBox::YesRole); does not create a standard button. I searched for a method to get clicked buttons given role. But no help :(

    I tried using button text, which solved my problems but that just feels lame :(

  • Lifetime Qt Champion


    Then why not just:

    if (BUTTON_YES == messageBox.clickedButton()) {
    // do stuff


  • @SGaist BUTTON_YES == messageBox.clickedButton().text()

    Yeah, i did that after realizing there is no other solution :(

  • Lifetime Qt Champion

    Sorry, I thought that BUTTON_YES was a pointer to a QAbstractButton instance.

    What I was referring to is described in the documentation of clickedButton

Log in to reply