Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] QMessageBox Buttons
QtWS25 Last Chance

[SOLVED] QMessageBox Buttons

Scheduled Pinned Locked Moved General and Desktop
qmessegebox
9 Posts 4 Posters 10.7k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Corpse0327
    wrote on last edited by Corpse0327
    #1

    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);
        messageBox.setEscapeButton(QMessageBox::Cancel);
        messageBox.setDefaultButton(QMessageBox::Yes);
    
        QList<QAbstractButton*> messageBoxButtons = messageBox.buttons();
    
        // How do i get buttons properly? 
        connect(messageBoxButtons.at(0), SIGNAL(clicked(bool))
                , this, SLOT(messageBoxYesClicked(bool)));
        connect(messageBoxButtons.at(1), SIGNAL(clicked(bool))
                , this, SLOT(messageBoxNoClicked(bool)));
        connect(messageBoxButtons.at(2), SIGNAL(clicked(bool))
                , this, SLOT(messageBoxCancelClicked(bool)));
    
        if(canSaveContent() == false)
            // Set YesRole button to disabled
    
        messageBox.exec();
    
        if(m_reply == QMessageBox::Yes)
            // Do something
        else if(m_reply == QMessageBox::No)
            // Do something
        else
            // Do something
    

    These functions merely sets m_reply with proper values.

    void DialogEntryNote::messageBoxYesClicked(bool checked)
    {
        Q_UNUSED(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.

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      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)

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      C 1 Reply Last reply
      0
      • M Offline
        M Offline
        mchinand
        wrote on last edited by
        #3

        See the maybeSave() function in this example.

        1 Reply Last reply
        0
        • M mcosta

          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)

          C Offline
          C Offline
          Corpse0327
          wrote on last edited by Corpse0327
          #4

          @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.

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mcosta
            wrote on last edited by
            #5

            Hi,

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

            Once your problem is solved don't forget to:

            • Mark the thread as SOLVED using the Topic Tool menu
            • Vote up the answer(s) that helped you to solve the issue

            You can embed images using (http://imgur.com/) or (http://postimage.org/)

            C 1 Reply Last reply
            0
            • M mcosta

              Hi,

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

              C Offline
              C Offline
              Corpse0327
              wrote on last edited by Corpse0327
              #6

              @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 :(

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                Then why not just:

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

                ?

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                C 1 Reply Last reply
                0
                • SGaistS SGaist

                  Hi,

                  Then why not just:

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

                  ?

                  C Offline
                  C Offline
                  Corpse0327
                  wrote on last edited by
                  #8

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

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

                  1 Reply Last reply
                  0
                  • SGaistS Offline
                    SGaistS Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    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

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved