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. Return a QPushButton from a QButtonGroup by id?
QtWS25 Last Chance

Return a QPushButton from a QButtonGroup by id?

Scheduled Pinned Locked Moved Solved General and Desktop
qpushbuttonqbuttongroup
7 Posts 3 Posters 1.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.
  • L Offline
    L Offline
    lansing
    wrote on last edited by
    #1

    I have a group of QPushButton and I added them to a QButtonGroup with id. I want to programatically setcheck one of the button. In order to do that, I need to get that button first, but how do I get it by id?

    1 Reply Last reply
    0
    • B Offline
      B Offline
      Bonnie
      wrote on last edited by Bonnie
      #2

      How about reading the doc first :)
      QAbstractButton *QButtonGroup::button(int id) const
      Or do you just not know how to cast a QAbstractButton * to QPushButton *?

      L 1 Reply Last reply
      1
      • B Bonnie

        How about reading the doc first :)
        QAbstractButton *QButtonGroup::button(int id) const
        Or do you just not know how to cast a QAbstractButton * to QPushButton *?

        L Offline
        L Offline
        lansing
        wrote on last edited by
        #3

        @Bonnie

        I did look over the doc for 10 minutes, I just missed it

        qwasder85Q 1 Reply Last reply
        0
        • L lansing

          @Bonnie

          I did look over the doc for 10 minutes, I just missed it

          qwasder85Q Offline
          qwasder85Q Offline
          qwasder85
          wrote on last edited by
          #4

          @lansing Set the topic as solved, please.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lansing
            wrote on last edited by
            #5

            For some reason I couldn't check the button. I have four buttons in the group with value -2, 0, 1, 2. The target button was returned but setCheck(true) didn't do anything. I checked the checkedId of the buttonGroup, it always returns -1 when this event was triggered.

                qDebug() << "checked id is: " << ui->buttonGroup->checkedId();
            
                QAbstractButton * targetBtn= ui->buttonGroup->button(2);
            
                if (targetBtn!= nullptr) {       
                    blockSignals(true);
                    targetBtn->setChecked(true);
                    blockSignals(false);
                }
            
            
            B 1 Reply Last reply
            0
            • L lansing

              For some reason I couldn't check the button. I have four buttons in the group with value -2, 0, 1, 2. The target button was returned but setCheck(true) didn't do anything. I checked the checkedId of the buttonGroup, it always returns -1 when this event was triggered.

                  qDebug() << "checked id is: " << ui->buttonGroup->checkedId();
              
                  QAbstractButton * targetBtn= ui->buttonGroup->button(2);
              
                  if (targetBtn!= nullptr) {       
                      blockSignals(true);
                      targetBtn->setChecked(true);
                      blockSignals(false);
                  }
              
              
              B Offline
              B Offline
              Bonnie
              wrote on last edited by Bonnie
              #6

              @lansing Are they checkable? Have you called setCheckable(true)?

              L 1 Reply Last reply
              1
              • B Bonnie

                @lansing Are they checkable? Have you called setCheckable(true)?

                L Offline
                L Offline
                lansing
                wrote on last edited by
                #7

                @Bonnie

                Thank you that fixed it. I also have all the buttons set to "autoDefault" which compound the problem. Now I turned off all autoDefault and make them all checkable and they're working properly.

                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