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 10 Aug 2020, 02:45 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 10 Aug 2020, 02:48 last edited by Bonnie 8 Oct 2020, 02:50
      #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 10 Aug 2020, 02:53
      1
      • B Bonnie
        10 Aug 2020, 02:48

        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 10 Aug 2020, 02:53 last edited by
        #3

        @Bonnie

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

        Q 1 Reply Last reply 10 Aug 2020, 07:30
        0
        • L lansing
          10 Aug 2020, 02:53

          @Bonnie

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

          Q Offline
          Q Offline
          qwasder85
          wrote on 10 Aug 2020, 07:30 last edited by
          #4

          @lansing Set the topic as solved, please.

          1 Reply Last reply
          0
          • L Offline
            L Offline
            lansing
            wrote on 10 Aug 2020, 08:11 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 10 Aug 2020, 09:13
            0
            • L lansing
              10 Aug 2020, 08:11

              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 10 Aug 2020, 09:13 last edited by Bonnie 8 Oct 2020, 09:15
              #6

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

              L 1 Reply Last reply 10 Aug 2020, 09:24
              1
              • B Bonnie
                10 Aug 2020, 09:13

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

                L Offline
                L Offline
                lansing
                wrote on 10 Aug 2020, 09:24 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

                1/7

                10 Aug 2020, 02:45

                • Login

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