Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved Return a QPushButton from a QButtonGroup by id?

    General and Desktop
    qpushbutton qbuttongroup
    3
    7
    493
    Loading More Posts
    • 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
      lansing last edited by

      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 Reply Quote 0
      • B
        Bonnie last edited by 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 1 Reply Last reply Reply Quote 1
        • L
          lansing @Bonnie last edited by

          @Bonnie

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

          qwasder85 1 Reply Last reply Reply Quote 0
          • qwasder85
            qwasder85 @lansing last edited by

            @lansing Set the topic as solved, please.

            1 Reply Last reply Reply Quote 0
            • L
              lansing last edited by

              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 Reply Quote 0
              • B
                Bonnie @lansing last edited by Bonnie

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

                L 1 Reply Last reply Reply Quote 1
                • L
                  lansing @Bonnie last edited by

                  @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 Reply Quote 0
                  • First post
                    Last post