Qt的Radio Button(单选按钮)
1 在UI界面中加入控件

2 对QRadioButton控件进行分组
QRadioButton的分组有多重方法,如采用组合框、QWidge等,下面介绍采用QButtonGroup方法来实现分组,好处是不影响QRadioButton在界面上的显示(组合框分组方式会在界面上出现组合框,要以自己的需要选择),以及方便ID的设置。
添加头文件
#include <QButtonGroup>
#include <QDebug>
声明QButtonGroup变量
private:
QButtonGroup *groupButton1;
QButtonGroup *groupButton2;
在窗体构造函数中初始化QButtonGroup,把相应的QRadioButton添加进来并设置ID
groupButton1=new QButtonGroup(this);
groupButton1->addButton(ui->apple_radioButton,0);
groupButton1->addButton(ui->banan_radioButton,1);
groupButton1->addButton(ui->pear_radioButton,2);
ui->apple_radioButton->setChecked(true); //默认选中apple_radioButton
groupButton2=new QButtonGroup(this);
groupButton2->addButton(ui->potato_radioButton,0);
groupButton2->addButton(ui->greenpepper_radioButton,1);
groupButton2->addButton(ui->spinach_radioButton,2);
ui->greenpepper_radioButton->setChecked(true);
3 多个QRadioButton控件响应同一个槽函数
在头文件中声明槽函数
public slots:
void slots_fruits();
void slots_vegetables();
在窗体构造函数中绑定信号和槽函数
//绑定信号和槽函数
connect(ui->apple_radioButton,SIGNAL(clicked(bool)),
this,SLOT(slots_fruits()));
connect(ui->banan_radioButton,SIGNAL(clicked(bool)),
this,SLOT(slots_fruits()));
connect(ui->pear_radioButton,SIGNAL(clicked(bool)),
this,SLOT(slots_fruits()));
//绑定信号和槽函数
connect(ui->potato_radioButton,SIGNAL(clicked(bool)),
this,SLOT(slots_vegetables()));
connect(ui->greenpepper_radioButton,SIGNAL(clicked(bool)),
this,SLOT(slots_vegetables()));
connect(ui->spinach_radioButton,SIGNAL(clicked(bool)),
this,SLOT(slots_vegetables()));
槽函数的实现
QRadioButton的槽函数中,不需要逐个检查QRadioButton控件状态,仅仅通过groupButton1->checkedId()来获知哪一个QRadioButton控件被选中,其返回被选中控件的ID值。
void MainWindow::slots_fruits()
{
qDebug()<<"fruits"<<endl;
switch(groupButton1->checkedId())
{
case 0:
qDebug() <<"apple"<<endl;
break;
case 1:
qDebug() <<"banan_radioButton"<<endl;
break;
case 2:
qDebug() <<"pear_radioButton"<<endl;
break;
}
}
void MainWindow::slots_vegetables()
{
qDebug()<<"vegetables"<<endl;
switch(groupButton2->checkedId())
{
case 0:
qDebug() <<"potato_radioButton"<<endl;
break;
case 1:
qDebug() <<"greenpepper_radioButton"<<endl;
break;
case 2:
qDebug() <<"spinach_radioButton"<<endl;
break;
}
}
运行结果


参考资料
https://blog.csdn.net/CXP2205455256/article/details/44956051
Qt的Radio Button(单选按钮)的更多相关文章
- radio button(单选按钮)
单选按钮只是input输入框的一种类型. 每一个单选按钮都应该嵌套在它自己的label(标签)元素中. 注意:所有关联的单选按钮应该使用相同的name属性. 下面是一个单选按钮的例子: <lab ...
- VC单选按钮控件(Radio Button)用法(转)
先为对话框加上2个radio button,分别是Radio1和Radio2. 问题1:如何让Radio1或者Radio2默认选上?如何知道哪个被选上了? 关键是选上,“默认”只要放在OnInitDi ...
- MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box)等.命令按钮就是我们前面多次提到的侠义的 ...
- Check Box、Radio Button、Combo Box控件使用
Check Box.Radio Button.Combo Box控件使用 使用控件的方法 1.拖动控件到对话框 2. 定义控件对应的变量(值变量或者控件变量) 3.响应控件各种消息 Check Box ...
- VS2010/MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
言归正传,鸡啄米上一节中讲了编辑框的用法,本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box ...
- MFC中Radio Button使用方法
先为对话框加上2个radio button,分别是Radio1和Radio2. 问题1:如何让Radio1或者Radio2默认选上?如何知道哪个被选上了? 关键是选上,“默认”只要放在OnInitDi ...
- VS2010-MFC(常用控件:按钮控件Button、Radio Button和Check Box)
转自:http://www.jizhuomi.com/software/182.html 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check ...
- JQuery radio(单选按钮)操作方法汇总
这篇文章主要介绍了JQuery radio(单选按钮)操作方法汇总,本文讲解了获取选中值.设置选中值.根据Value值设置选中.删除Radio.遍历等内容,需要的朋友可以参考下 随着Jquery的 ...
- MFC radio button 绑定变量用法
我们在对话框中拖动一个radio button控件,然后点击类向导,结果却发现在Member Variables里看不到Radio控件的ID.这是为什么? 2.1 三个Radio Button,ID分 ...
随机推荐
- spring使用@Autowired为抽象父类注入依赖
有时候为了管理或者避免不一致性,希望具体服务统一继承抽象父类,同时使用@Autowired为抽象父类注入依赖.搜了了网上,有些解决方法实现实在不敢恭维,靠子类去注入依赖,那还有什么意义,如下: 父类: ...
- linux homebrew skill 技巧
$ cat /usr/bin/gitdiffH0#!/bin/bashgit diff HEAD $(git ls-files| grep '\.py$\|\.py\.in$') | osflake8 ...
- 寻找复杂背景下物体的轮廓(OpenCV / C++ - Filling holes)
一.问题提出 这是一个来自"answerOpenCV"(http://answers.opencv.org/question/200422/opencv-c-filling-hol ...
- linux物理网卡检测命令mii-tool
mii-tool #是用于查看.管理物理的网络接口的状态,还可以配置网卡需要的协商方式. 一 ,查看网卡工作状态 mill-tool [网卡名] #查看物理网卡连接状态 -V 显示版本信息 ...
- STM32之独立看门狗(IWDG)与窗口看门狗(WWDG)总结
一.独立看门狗 STM32 的独立看门狗由内部专门的 40Khz 低速时钟驱动,即使主时钟发生故障,它也仍然有效. 看门狗的原理:单片机系统在外界的干扰下会出现程序跑飞的现象导致出现死循环,看门狗电路 ...
- 1. 元信息:Meta类 2. 基于对象查询的sql优化 3. 自定义:Group_Concat() 4. ajax前后台交互
一.元信息 ''' 1. 元信息 1. Model类可以通过元信息类设置索引和排序信息 2. 元信息是在Model类中定义一个Meta子类 class Meta: # 自定义表名 db_table = ...
- 牛客网数据库SQL实战(1-5)
1.查找最晚入职员工的所有信息 CREATE TABLE `employees` ( `emp_no` int(11) NOT NULL, `birth_date` date NOT NULL, `f ...
- fatal: [db01]: FAILED! => {"changed": false, "msg": "The PyMySQL (Python 2.7 and Python 3.X) or MySQL-python (Python 2.X) module is required."}
centos7.5 使用ansible中的role安装mariadb创建用户报错 错误: [root@m01 roles]# ansible-playbook site.yml TASK [mysql ...
- FFmpeg:视频转码、剪切、合并、播放速调整
原文:https://fzheng.me/2016/01/08/ffmpeg/ FFmpeg:视频转码.剪切.合并.播放速调整 2016-01-08 前阵子帮导师处理项目 ppt,因为插入视频的格式问 ...
- 【做题】Codeforces Round #429 (Div. 2) E. On the Bench——组合问题+dp
题目大意是给你n个数,求相邻两数相乘不是完全平方数的排列数. 一开始看到这题的时候,本人便想给相乘为完全平方数的数对建边,然后就写萎了... 后来通过集体智慧发现这个重要性质:对于自然数a,b,c,若 ...