#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
/* 主窗体设置位置和显示的大小 */
this->setGeometry(0, 0, 800, 480);
/*将主窗口背景色设置为天蓝色(不透明度为 100%)*/
this->setStyleSheet("Widget { background-color: rgba(135, 206, 250, 100%); }");
/* 实例化5个QRadioButton单选按钮对象 */
radioButton1 = new QRadioButton(this);
radioButton2 = new QRadioButton(this);
radioButton3 = new QRadioButton(this);
radioButton4 = new QRadioButton(this);
radioButton5 = new QRadioButton(this);
// 创建一个按钮组 QButtonGroup 对象
button_group = new QButtonGroup(this);
/* 设置第一组两个 QRadioButton 的位置和显示大小 */
radioButton1->setGeometry(280, 140, 100, 50);
radioButton2->setGeometry(420, 140, 100, 50);
/* 设置第二组3个 QRadioButton 的位置和显示大小 */
radioButton3->setGeometry(140, 290, 100, 50);
radioButton4->setGeometry(340, 290, 100, 50);
radioButton5->setGeometry(540, 290, 100, 50);
/* 设置第一组两个 QRadioButton 的显示文本 */
radioButton1->setText("开关一");
radioButton2->setText("开关二");
/* 设置第二组3个 QRadioButton 的显示文本 */
radioButton3->setText("选择一");
radioButton4->setText("选择二");
radioButton5->setText("选择三");
/* 第一组 QRadioButton 使用样式表1设置 */
radioButton1->setObjectName("radiogroup1");
radioButton2->setObjectName("radiogroup1");
/* 第二组 QRadioButton 使用样式表2设置 */
radioButton3->setObjectName("radiogroup2");
radioButton4->setObjectName("radiogroup2");
radioButton5->setObjectName("radiogroup2");
/*第一组的2个 单选控件,使用 Qt 默认的分组功能*/
/*将第二组三个单选按钮对象添加到按钮组 button_group*/
button_group->addButton(radioButton3);
button_group->addButton(radioButton4);
button_group->addButton(radioButton5);
/*设置第二组按钮成员为互斥*/
button_group->setExclusive(true);
/* 第一组设置初始状态,radioButton1 的 Checked 为 true,另一个为 false*/
radioButton1->setChecked(true);
radioButton2->setChecked(false);
/* 第二组设置初始状态,rradioButton3 的 Checked 为 true,另两个为 false*/
radioButton3->setChecked(true);
radioButton4->setChecked(false);
radioButton5->setChecked(false);
}
Widget::~Widget()
{
}
没有合适的资源?快使用搜索试试~ 我知道了~
Qt常用的按钮控件编程(三)- QRadioButton 按钮
需积分: 0 1 下载量 193 浏览量
2023-05-30
12:16:46
上传
评论 2
收藏 11KB GZ 举报
温馨提示
共12个文件
png:4个
cpp:2个
user:1个
本例程为QRadioButton 单选按钮编程。通过这个例子,初步了解QT管理资源机制,以及如何分组管理QRadioButton 单选按钮。项目演示如何使用默认的分组和 QButtonGroup 进行互斥设置,保证多个选项所在的组内只有一个被选中。项目使用样式表文件将两组按钮使用不同的样式表进行外观设置。 文章中使用的例程和内容都是在chatgpt的帮助下完成的,例程经过测试通过。
资源推荐
资源详情
资源评论
收起资源包目录
_radiobutton.tar.gz (12个子文件)
_radiobutton
widget.cpp 2KB
style.qss 717B
_radiobutton.pro.user 30KB
readme 544B
main.cpp 1KB
res.qrc 268B
images
switch_off.png 16KB
switch_on.png 16KB
radiobutton-on.png 474B
radiobutton-off.png 586B
_radiobutton.pro 1KB
widget.h 565B
共 12 条
- 1
资源评论
robin861109
- 粉丝: 19
- 资源: 7
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功