参考博客:【Qt控件之QButtonGroup】概述及使用_qt中qbuttongroup-CSDN博客
QButtonGroup *buttonGroup= new QButtonGroup();
QPushButton *button1 = new QPushButton("Button 1");
QPushButton *button2 = new QPushButton("Button 2");
QPushButton *button3 = new QPushButton("Button 3");
//添加按钮
buttonGroup.addButton(button1, 1);
buttonGroup.addButton(button2, 2);
buttonGroup.addButton(button3, 3);
//进行信号和槽的绑定
connect(buttonGroup, static_cast<void(QButtonGroup::*)(int)>(&QButtonGroup::buttonClicked), [&](int index) {
qDebug() << "单击编号为 " << index;
});
//清除按钮
int numBtn = buttonGroup->buttons().size();
for(int i = 0; i < numBtn; i++)
{
buttonGroup->removeButton(buttonGroup->button(i));
}