I hope that you're adding 40 buttons programmatically, not manually in Qt Designer, so you should be able to create a list of all buttons: QList<QPushButton*> my_buttons
. You need to create a button group and put all buttons into it. It's quite simple:
QButtonGroup* group = new QButtonGroup(this);
foreach(QPushButton* button, my_buttons) {
group->addButton(button);
}