C++ (Qt)#include <QtGui>#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)#include <QtWidgets/QtWidgets>#endif #include <Windows.h> class Widget : public QWidget{ Q_OBJECT public: Widget(QWidget *parent = 0) : QWidget(parent), box(new QGroupBox("box", this)), b1(new QRadioButton("b1", box)), b2(new QRadioButton("b2", box)) { QVBoxLayout *vbl = new QVBoxLayout(box); vbl->addWidget(b1); vbl->addWidget(b2); bg.addButton(b1); bg.addButton(b2); connect(&bg, SIGNAL(buttonClicked(QAbstractButton *)), SLOT(buttonFromGroupClicked(QAbstractButton *))); } private slots: void buttonFromGroupClicked(QAbstractButton *button) { qDebug() << "selected button with title" << button->text(); } private: QGroupBox *box; QRadioButton *b1, *b2; QButtonGroup bg;}; int main(int argc, char **argv){ QApplication app(argc, argv); Widget w; w.show(); w.adjustSize(); return app.exec();} #include "main.moc"
//*.hprivate: QButtonGroup group;private: void init();private slots: void buttonClicked( QAbstractButton *);//*.cppvoid init(){group->setExclusive(false);QList<QCheckBox*> listCheckBox = ui.groupBox->findChildren<QCheckBox*>();for (int i = 0; i < listCheckBox.size(); i++){group.addButton(listCheckBox[i],i);}connect(&group, SIGNAL(buttonClicked ( QAbstractButton *)), this, SLOT(buttonClicked ( QAbstractButton *));}