最近刚刚使用 QButtonGroup 来给QRadioButton分组,在此记录一下
头文件
声明button、buttongroup、以及两个slots
private:
QButtonGroup *m_btnGroup1;
QRadioButton *ue1AT,*ue1Power,*ue1Manual;
QButtonGroup *m_btnGroup2;
QRadioButton *ue2AT,*ue2Power,*ue2Manual;
private slots:
void slotClickedbtnGroup1();
void slotClickedbtnGroup2();
cpp文件
实现分组(注意分配ID),连接信号-槽
ue1AT = new QRadioButton(uecontrolWidget);
ue1Power = new QRadioButton(uecontrolWidget);
ue1Manual = new QRadioButton(uecontrolWidget);
m_btnGroup1 = new QButtonGroup;
m_btnGroup1->addButton(ue1AT ,0);
m_btnGroup1->addButton(ue1Power ,1);
m_btnGroup1->addButton(ue1Manual,2);
connect(ue1AT, SIGNAL(clicked()), this,SLOT(slotClickedbtnGroup1()));
connect(ue1Power, SIGNAL(clicked()), this,SLOT(slotClickedbtnGroup1()));
connect(ue1Manual, SIGNAL(clicked()), this,SLOT(slotClickedbtnGroup1()));
ue2AT = new QRadioButton(uecontrolWidget);
ue2Power = new QRadioButton(uecontrolWidget);
ue2Manual = new QRadioButton(uecontrolWidget);
m_btnGroup2 = new QButtonGroup;
m_btnGroup2->addButton(ue2AT ,0);
m_btnGroup2->addButton(ue2Power ,1);
m_btnGroup2->addButton(ue2Manual,2);
connect(ue2AT, SIGNAL(clicked()), this,SLOT(slotClickedbtnGroup2()));
connect(ue2Power, SIGNAL(clicked()), this,SLOT(slotClickedbtnGroup2()));
connect(ue2Manual, SIGNAL(clicked()), this,SLOT(slotClickedbtnGroup2()));
cpp
直接用 QButtonGroup 的checkedId函数就可以获取组内哪个radiobutton是选中状态。
void SetRunPanel::slotClickedbtnGroup1()
{
switch(m_btnGroup1->checkedId())
{
case 0:
qDebug() << QString::fromLocal8Bit("1组 at");
break;
case 1:
qDebug() << QString::fromLocal8Bit("1组 power");
break;
case 2:
qDebug() << QString::fromLocal8Bit("1组 manual");
break;
}
}
void SetRunPanel::slotClickedbtnGroup2()
{
switch(m_btnGroup2->checkedId())
{
case 0:
qDebug() << QString::fromLocal8Bit("2组 at");
break;
case 1:
qDebug() << QString::fromLocal8Bit("2组 power");
break;
case 2:
qDebug() << QString::fromLocal8Bit("2组 manual");
break;
}
}