QT 复选框实现互斥功能,单选框与复选框之间转换

场景需求如下:当选择单关节单自由度,右边只能单选;当选择单关节多自由度,右边可以复选。但是只能选择相同关节的不同自由度,也就是只能选择1,2或3,4或5,6;当选择多关节多自由度时,右边就是正常的复选按钮。

QT 复选框实现互斥功能,单选框与复选框之间转换_第1张图片

实现: 

左边3个为QRadioButton单选框,右边6个为QCheckBox复选框。

negativemode.h头文件中加入:

private slots:
void switchFXorDX(bool flag);//左边单选框的槽函数
void gj_select(bool flag);//右边复选框的槽函数

private:
static int a;

negativemode.cpp源文件中加入:

groupButton2=new QButtonGroup(this);

//将单选框/复选框与槽函数关联
connect(singlesingle,SIGNAL(toggled(bool)),this,SLOT(switchFXorDX(bool)));
connect(singlemulti,SIGNAL(toggled(bool)),this,SLOT(switchFXorDX(bool)));
connect(multimulti,SIGNAL(toggled(bool)),this,SLOT(switchFXorDX(bool)));

connect(jnw,SIGNAL(toggled(bool)),this,SLOT(gj_select(bool)));
connect(jqs,SIGNAL(toggled(bool)),this,SLOT(gj_select(bool)));
connect(zqs,SIGNAL(toggled(bool)),this,SLOT(gj_select(bool)));
connect(bqh,SIGNAL(toggled(bool)),this,SLOT(gj_select(bool)));
connect(wrp,SIGNAL(toggled(bool)),this,SLOT(gj_select(bool)));
connect(wnw,SIGNAL(toggled(bool)),this,SLOT(gj_select(bool)));


int negativemode::a=0;
void negativemode::switchFXorDX(bool flag)
{
    QRadioButton *select=qobject_cast(sender()); //得到当前信号来源的对象
    if (flag==true&&select==singlesingle){//选中singlesingle:单关节单自由度
        a=1;
        groupButton2->addButton(jnw,0);
        groupButton2->addButton(jqs,1);
        groupButton2->addButton(zqs,2);
        groupButton2->addButton(bqh,3);
        groupButton2->addButton(wrp,4);
        groupButton2->addButton(wnw,5);
    }
    if (flag==true&&select==singlemulti){//选中singlemulti:单关节多自由度
        a=2;
        groupButton2->removeButton(jnw);
        groupButton2->removeButton(jqs);
        groupButton2->removeButton(zqs);
        groupButton2->removeButton(bqh);
        groupButton2->removeButton(wrp);
        groupButton2->removeButton(wnw);
        jnw->setChecked(false);//分别设置右边六个为未选中状态
        jqs->setChecked(false);
        zqs->setChecked(false);
        bqh->setChecked(false);
        wrp->setChecked(false);
        wnw->setChecked(false);
    }
    if (flag==true&&select==multimulti){//选中multimulti:多关节多自由度
        a=3;
        groupButton2->removeButton(jnw);
        groupButton2->removeButton(jqs);
        groupButton2->removeButton(zqs);
        groupButton2->removeButton(bqh);
        groupButton2->removeButton(wrp);
        groupButton2->removeButton(wnw);
        jnw->setChecked(false);
        jqs->setChecked(false);
        zqs->setChecked(false);
        bqh->setChecked(false);
        wrp->setChecked(false);
        wnw->setChecked(false);
    }
}

void negativemode::gj_select(bool flag)
{
//若左边选择的是单关节多自由度,则右边只可选同一关节的不同自由度
    if (a==2){
        QCheckBox *select=qobject_cast(sender());
//若选中1或2,设置3,4,5,6为未选中状态
        if(flag==true&&(select==jnw||select==jqs)){
            zqs->setChecked(false);
            zqs->setAutoExclusive(false);
            bqh->setChecked(false);
            bqh->setAutoExclusive(false);
            wrp->setChecked(false);
            wrp->setAutoExclusive(false);
            wnw->setChecked(false);
            wnw->setAutoExclusive(false);
        }
//若选中3或4,设置1,2,5,6为未选中状态
        if(flag==true&&(select==zqs||select==bqh)){
            jnw->setChecked(false);
            jnw->setAutoExclusive(false);
            jqs->setChecked(false);
            jqs->setAutoExclusive(false);
            wrp->setChecked(false);
            wrp->setAutoExclusive(false);
            wnw->setChecked(false);
            wnw->setAutoExclusive(false);
        }
//若选中5或6,设置1,2,3,4为未选中状态
        if(flag==true&&(select==wrp||select==wnw)){//若选中1,设置3,4,5,6为未选中状态
            jnw->setChecked(false);
            jnw->setAutoExclusive(false);
            jqs->setChecked(false);
            jqs->setAutoExclusive(false);
            zqs->setChecked(false);
            zqs->setAutoExclusive(false);
            bqh->setChecked(false);
            bqh->setAutoExclusive(false);
        }
    }
}

 

你可能感兴趣的:(QT 复选框实现互斥功能,单选框与复选框之间转换)