PDA

View Full Version : Autoexclusive property for QPushButtons



qtstarter121
17th April 2018, 18:59
So I have a bunch of QPushButtons in QGridLayout and they all have autoExclusive = true. These buttons are loaded from a .ui file.
I added two new QPushButtons to the layout programmatically, and set the same property autoExclusive to true for them.

In my code when each button is clicked I have the following



if (!button->isChecked())
{
button->setChecked(true);
}

Now when I click on either of these two new QPushButtons, they don't get unchecked when I click any other button, but the rest of them do, so I'm not sure why the autoexclusive property doesn't work for these buttons that were added programmatically.

qtstarter121
17th April 2018, 21:02
I haven't been able to find a cleaner solution with respect to the auto exclusive property, but the following works for me in the meantime:



QGridLayout* gL = QWidget::findChild<QGridLayout *>("buttonsLayout");
QPushButton* btn = 0;
for(int i = 0; i < gL->count(); ++i)
{
if(btn = dynamic_cast<QPushButton*>(gL->itemAt(i)->widget()))
{
if(btn != button) // where button is the button that was just recently clicked.
{
btn->setChecked(false);
}

}
}

d_stranz
17th April 2018, 22:52
Try using a QButtonGroup to hold both your static and dynamic buttons. You can set the exclusive property of the button group, which will enforce that for all of the checkable buttons in it. QButtonGroup does not have any visual appearance, it is just a container for holding buttons and enforcing policies on them as a group.

qtstarter121
18th April 2018, 00:58
Thanks for your reply. It makes sense to me that using a QButtonGroup would work well.