Solved Return a QPushButton from a QButtonGroup by id?
-
I have a group of
QPushButton
and I added them to aQButtonGroup
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? -
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 *? -
I did look over the doc for 10 minutes, I just missed it
-
@lansing Set the topic as solved, please.
-
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 thecheckedId
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); }
-
@lansing Are they checkable? Have you called
setCheckable(true)
? -
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.