Qt5之QRadioButton

来源:互联网 发布:朝鲜核问题的影响 知乎 编辑:程序博客网 时间:2024/06/15 20:18

本例程介绍QRadioButton的使用,包括QRadioButton的分组、多个QRadioButton控件响应同一个槽函数、QRadioButton的ID设置从而避免繁琐的判断。

一、在UI界面添加如下控件:


二、对QRadioButton控件进行分组

      QRadioButton的分组有多重方法,如采用组合框、QWidge等,下面介绍采用QButtonGroup方法来实现分组,好处是不影响QRadioButton在界面上的显示(组合框分组方式会在界面上出现组合框,要以自己的需要选择),以及方便ID的设置。

     首先添加头文件:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. #include <QButtonGroup>  
     声明QButtonGroup变量

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. QButtonGroup *btnGroupFruits;  
  2. QButtonGroup *btnGroupVegetables;  
    在窗体构造函数中初始化QButtonGroup,以及把相应的QRadioButton添加进来并设置ID

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. btnGroupFruits = new QButtonGroup(this);  
  2. btnGroupFruits->addButton(ui->radioButton11, 0);  
  3. btnGroupFruits->addButton(ui->radioButton12, 1);  
  4. btnGroupFruits->addButton(ui->radioButton13, 2);  
  5. ui->radioButton11->setChecked(true);  
  6.   
  7. btnGroupVegetables = new QButtonGroup(this);  
  8. btnGroupVegetables->addButton(ui->radioButton21, 0);  
  9. btnGroupVegetables->addButton(ui->radioButton22, 1);  
  10. btnGroupVegetables->addButton(ui->radioButton23, 2);  
  11. ui->radioButton21->setChecked(true);  
三、多个QRadioButton控件响应同一个槽函数

     在头文件声明槽函数:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. public slots:  
  2.     void onRadioClickFruits();  
  3.     void onRadioClickVegetables();  
    在窗体构造函数中绑定信号与槽:

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. connect(ui->radioButton11, SIGNAL(clicked()), this, SLOT(onRadioClickFruits()));  
  2. connect(ui->radioButton12, SIGNAL(clicked()), this, SLOT(onRadioClickFruits()));  
  3. connect(ui->radioButton13, SIGNAL(clicked()), this, SLOT(onRadioClickFruits()));  
  4.   
  5. connect(ui->radioButton21, SIGNAL(clicked()), this, SLOT(onRadioClickVegetables()));  
  6. connect(ui->radioButton22, SIGNAL(clicked()), this, SLOT(onRadioClickVegetables()));  
  7. connect(ui->radioButton23, SIGNAL(clicked()), this, SLOT(onRadioClickVegetables()));  
   槽函数的实现:

   QRadioButton的槽函数中,不需要逐个检查QRadioButton控件状态,仅仅通过btnGroupFruits->checkedId()来获知哪一个QRadioButton控件被选中,其返回被选中控件的ID值。

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. void MainWindow::onRadioClickFruits()  
  2. {  
  3.    switch(btnGroupFruits->checkedId())  
  4.    {  
  5.    case 0:  
  6.        qDebug() << QString::fromLocal8Bit("苹果");  
  7.        break;  
  8.    case 1:  
  9.        qDebug() << QString::fromLocal8Bit("西红柿");  
  10.        break;  
  11.    case 2:  
  12.        qDebug() << QString::fromLocal8Bit("芒果");  
  13.        break;  
  14.    }  
  15. }  
  16.   
  17. void MainWindow::onRadioClickVegetables()  
  18. {  
  19.     switch(btnGroupVegetables->checkedId())  
  20.     {  
  21.     case 0:  
  22.         qDebug() << QString::fromLocal8Bit("土豆");  
  23.         break;  
  24.     case 1:  
  25.         qDebug() << QString::fromLocal8Bit("青椒");  
  26.         break;  
  27.     case 2:  
  28.         qDebug() << QString::fromLocal8Bit("菠菜");  
  29.         break;  
  30.     }  
  31. }  
以下是程序运行结果:


0 0