原创文章,欢迎转载。转载请注明:转载自 祥的博客
原文链接:https://blog.csdn.net/humanking7/article/details/85786540
用QButtonGroup
实现QCheckBox
不互斥,而QRadioButton
互斥
Public Functions
QButtonGroup(QObject * parent = 0)
~QButtonGroup()
bool exclusive() const
void setExclusive(bool) //设置button是否互斥,默认互斥
void addButton(QAbstractButton button, int id = -1)
QAbstractButton button(int id) const
QList<QAbstractButton > buttons() const
QAbstractButton checkedButton() const
int checkedId() const
int id(QAbstractButton button) const
void removeButton(QAbstractButton button)
void setId(QAbstractButton * button, int id)
31 public functions inherited from QObject
Signals
void buttonClicked(QAbstractButton * button)
void buttonClicked(int id)
void buttonPressed(QAbstractButton * button)
void buttonPressed(int id)
void buttonReleased(QAbstractButton * button)
void buttonReleased(int id)
void buttonToggled(QAbstractButton * button, bool checked)
void buttonToggled(int id, bool checked)
QButtonGroup* m_btnGp_AE_eff;//针对CheckBox的buttonGroup
QButtonGroup* m_btnGp_AE_faultFlg;//针对radioButton的buttonGroup
//==================
//QCheckBox
//==================
//AE_eff
m_btnGp_AE_eff = new QButtonGroup();
m_btnGp_AE_eff->setExclusive(false);//不互斥-【重点】
//将CheckBox的指针加入到ButtonGroup中
m_btnGp_AE_eff->addButton(ui.cB_AE_eff_0, 0);
m_btnGp_AE_eff->addButton(ui.cB_AE_eff_1, 1);
m_btnGp_AE_eff->addButton(ui.cB_AE_eff_2, 2);
m_btnGp_AE_eff->addButton(ui.cB_AE_eff_3, 3);
m_btnGp_AE_eff->addButton(ui.cB_AE_eff_4, 4);
m_btnGp_AE_eff->addButton(ui.cB_AE_eff_5, 5);
m_btnGp_AE_eff->addButton(ui.cB_AE_eff_6, 6);
m_btnGp_AE_eff->addButton(ui.cB_AE_eff_7, 7);
//用于checkBox显示值
str_AE_eff_V0[0] = “[0]气压高度(0:无效)”;
str_AE_eff_V0[1] = “[1]指示空速(0:无效)”;
str_AE_eff_V0[2] = “[2]真空速(0:无效)”;
str_AE_eff_V0[3] = “[3]大气总温(0:无效)”;
str_AE_eff_V0[4] = “[4]大气静温(0:无效)”;
str_AE_eff_V0[5] = “[5]升降速度(0:无效)”;
str_AE_eff_V0[6] = “[6]备份总温(0:无效)”;
str_AE_eff_V0[7] = “7”;
str_AE_eff_V1[0] = “[0]气压高度(1:有效)”;
str_AE_eff_V1[1] = “[1]指示空速(1:有效)”;
str_AE_eff_V1[2] = “[2]真空速(1:有效)”;
str_AE_eff_V1[3] = “[3]大气总温(1:有效)”;
str_AE_eff_V1[4] = “[4]大气静温(1:有效)”;
str_AE_eff_V1[5] = “[5]升降速度(1:有效)”;
str_AE_eff_V1[6] = “[6]备份总温(1:有效)”;
str_AE_eff_V1[7] = “7”;
//
//QRadioButton
//
//AE_faultFlg
m_btnGp_AE_faultFlg = new QButtonGroup();
m_btnGp_AE_faultFlg->setExclusive(true);//互斥-【重点】
//将radioButton加入到ButtonGroup中
m_btnGp_AE_faultFlg->addButton(ui.rB_AE_fault_0x00, 0);
m_btnGp_AE_faultFlg->addButton(ui.rB_AE_fault_0x80, 1);
槽函数
protected slots:
void slot_btnGp_AE_eff(int id);
void slot_btnGp_AE_faultFlg(int id);
设置Connect
connect(m_btnGp_AE_eff, SIGNAL(buttonClicked(int)), this, SLOT(slot_btnGp_AE_eff(int)));
connect(m_btnGp_AE_faultFlg, SIGNAL(buttonClicked(int)), this, SLOT(slot_btnGp_AE_faultFlg(int)));
void FaultInjection::slot_btnGp_AE_eff(int id) { //qDebug() << "AE_eff" << id; QString strShow; int i; uint8 val = 0x00; uint8 tmpBit = 0x00; QCheckBox *cb_tmp;
//这段代码优势: //因为CheckBox的id和对应的bit位置是一致的,所以可以获取后直接移动得到对应的位置 //最后在将这些bit与起来 for (i = 0; i < 8;i++) { cb_tmp = (QCheckBox *)m_btnGp_AE_eff->button(i); tmpBit = getCheckBoxVal(cb_tmp) << i;//数据移动到对应的位 val = val | tmpBit; } //改变ui文字 cb_tmp = (QCheckBox *)m_btnGp_AE_eff->button(id); if (cb_tmp->isChecked()) {//选中了为1 cb_tmp->setText(str_AE_eff_V1[id]); } else {//没选中为0 cb_tmp->setText(str_AE_eff_V0[id]); } // 改变lab strShow = QString("0x%1 = %2 b").arg(int(val), 2, 16, QLatin1Char('0')).arg(int(val), 8, 2, QLatin1Char('0')); ui.lab_AE_eff_val->setText(strShow); //改变全局变量值 m_Ui_fault.AE_eff = val;
}
void FaultInjection::slot_btnGp_AE_faultFlg(int id)
{
//qDebug() << “AE_faultFlg” << id;
//QRadioButton* rb_tmp;
QString strShow;
uint8 val = 0x00;
//rb_tmp = (QRadioButton *)m_btnGp_AE_faultFlg->button(id);
if (0==id)
{//0x00 大气机故障
val = 0x00;
}
else if (1==id)
{//0x80 大气机正常有效
val = 0x80;
}
// 改变lab
strShow = QString("0x%1 = %2 b").arg(int(val), 2,
二、在UI界面实现互斥
1、放入2个Radio Button控件,然后选择这2个控件,右键在弹出的选项中选择》指定到按钮组,如图所示(我的是5.12.2版本QT,控件里没有ButtonGroup控件,或者大家知道怎么找留言下)
2、在右边的对象属性框里可以选择是否互斥