单选按钮和多选按钮,存放进QButtonGroup中
QButtonGroup方法来实现分组:将相同功能的按键,设为一个分组,然后可以进行 单选 或 多选 或 互斥单选
QAbstractButton类是按钮部件的抽象基类,提供了按钮所共有的功能。
1 //单选按钮放进组,多选按钮放进组 2 sexGroup = new QButtonGroup(this); 3 sexGroup->addButton(this->ui->rb_male,0); 4 sexGroup->addButton(this->ui->rb_female,1); 5 this->ui->rb_male->setChecked(true); //default 6 7 habbitGroup = new QButtonGroup(this); 8 habbitGroup->addButton(this->ui->cb_1,0); 9 habbitGroup->addButton(this->ui->cb_2,1); 10 habbitGroup->addButton(this->ui->cb_3,2); 11 habbitGroup->addButton(this->ui->cb_4,3); 12 // 设置不互斥 13 habbitGroup->setExclusive(false);//这样的话就支持多选了。
1 //获取性别 2 QString sex = this->sexGroup->checkedButton()->text();
1 //获取兴趣,遍历 2 QList<QAbstractButton*> ins_list = habbitGroup->buttons(); 3 QString ins=""; 4 for(int i =0 ;i<ins_list.length();i++) 5 { 6 QAbstractButton *che = ins_list.at(i); 7 if(che->isChecked()) 8 ins += che->text() + ","; 9 }