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. How to make a button the default?
QtWS25 Last Chance

How to make a button the default?

Scheduled Pinned Locked Moved Solved General and Desktop
dialog boxpushbuttonsetfocus
7 Posts 4 Posters 733 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.
  • J Offline
    J Offline
    jdent
    wrote on last edited by
    #1

    I have this code:

        QPushButton* closeButton = new QPushButton(tr("&Close"));
        QPushButton* revertButton = new QPushButton(tr("&Revert"));
        QPushButton* submitButton = new QPushButton(tr("&Submit"));
    
        closeButton->setDefault(true);
    
        connect(closeButton, &QPushButton::clicked, this, &PasswordDetailsDlg::close);
        connect(revertButton, &QPushButton::clicked, this, &PasswordDetailsDlg::revert);
        connect(submitButton, &QPushButton::clicked, this, &PasswordDetailsDlg::submit);
    
        QDialogButtonBox* buttonBox = new QDialogButtonBox;
        buttonBox->addButton(submitButton, QDialogButtonBox::ResetRole);
        buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole);
        buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole);
    
        return buttonBox;
    

    The close button should be the default and have the focus but it isn't!! Why??

    M JonBJ 2 Replies Last reply
    0
    • SGaistS SGaist

      @jdent you don't have any button with the accept role. Why is that ?

      J Offline
      J Offline
      jdent
      wrote on last edited by
      #7

      @SGaist I finally solved the issue like this:

      submitButton->setAutoDefault(false);
      revertButton->setAutoDefault(false);
      closeButton->setDefault(true);
      

      Thank you all!!

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

        Hi,

        What if you set autoDefault as well ?

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

        J 1 Reply Last reply
        1
        • J jdent

          I have this code:

              QPushButton* closeButton = new QPushButton(tr("&Close"));
              QPushButton* revertButton = new QPushButton(tr("&Revert"));
              QPushButton* submitButton = new QPushButton(tr("&Submit"));
          
              closeButton->setDefault(true);
          
              connect(closeButton, &QPushButton::clicked, this, &PasswordDetailsDlg::close);
              connect(revertButton, &QPushButton::clicked, this, &PasswordDetailsDlg::revert);
              connect(submitButton, &QPushButton::clicked, this, &PasswordDetailsDlg::submit);
          
              QDialogButtonBox* buttonBox = new QDialogButtonBox;
              buttonBox->addButton(submitButton, QDialogButtonBox::ResetRole);
              buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole);
              buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole);
          
              return buttonBox;
          

          The close button should be the default and have the focus but it isn't!! Why??

          M Offline
          M Offline
          mpergand
          wrote on last edited by
          #3

          @jdent

          If you want a specific button to be default you need to call QPushButton::setDefault() on it yourself. However, if there is no default button set and to preserve which button is the default button across platforms when using the QPushButton::autoDefault property, the first push button with the accept role is made the default button when the QDialogButtonBox is shown,

          https://doc.qt.io/qt-6/qdialogbuttonbox.html

          1 Reply Last reply
          0
          • J jdent

            I have this code:

                QPushButton* closeButton = new QPushButton(tr("&Close"));
                QPushButton* revertButton = new QPushButton(tr("&Revert"));
                QPushButton* submitButton = new QPushButton(tr("&Submit"));
            
                closeButton->setDefault(true);
            
                connect(closeButton, &QPushButton::clicked, this, &PasswordDetailsDlg::close);
                connect(revertButton, &QPushButton::clicked, this, &PasswordDetailsDlg::revert);
                connect(submitButton, &QPushButton::clicked, this, &PasswordDetailsDlg::submit);
            
                QDialogButtonBox* buttonBox = new QDialogButtonBox;
                buttonBox->addButton(submitButton, QDialogButtonBox::ResetRole);
                buttonBox->addButton(revertButton, QDialogButtonBox::ResetRole);
                buttonBox->addButton(closeButton, QDialogButtonBox::RejectRole);
            
                return buttonBox;
            

            The close button should be the default and have the focus but it isn't!! Why??

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #4

            @jdent
            As @SGaist says and the accepted solution at https://stackoverflow.com/questions/24556831/qbuttonbox-set-default-button. Perhaps especially if you are under Windows?

            1 Reply Last reply
            0
            • SGaistS SGaist

              Hi,

              What if you set autoDefault as well ?

              J Offline
              J Offline
              jdent
              wrote on last edited by
              #5

              @SGaist I also tried it with no change!!

              SGaistS 1 Reply Last reply
              0
              • J jdent

                @SGaist I also tried it with no change!!

                SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #6

                @jdent you don't have any button with the accept role. Why is that ?

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

                J 1 Reply Last reply
                0
                • SGaistS SGaist

                  @jdent you don't have any button with the accept role. Why is that ?

                  J Offline
                  J Offline
                  jdent
                  wrote on last edited by
                  #7

                  @SGaist I finally solved the issue like this:

                  submitButton->setAutoDefault(false);
                  revertButton->setAutoDefault(false);
                  closeButton->setDefault(true);
                  

                  Thank you all!!

                  1 Reply Last reply
                  0
                  • J jdent has marked this topic as solved on

                  • Login

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