当前位置: 首页 > news >正文

Qt常用控件——QRadioButton和QCheckBox

文章目录

    • QRadioButton
    • QAbstractButton信号
    • 实现简单的点餐页面
    • QCheckBox

QRadioButton

QRadioButton是单选按钮,可以让我们在多个选项当中选择一个

作为QAbstractButtonQWidget的子类,它们的属性和语法,对于QRadioButton同样适用

QAbstractButton中和QRadioButton关联较大的属性

属性说明
checkable是否能被使用
checked是否已经被选择(checkable是check的前提条件)
autoExclusive是否排他
选中一个按钮之后,是否会取消其他按钮的选中
对于QRadioButton来说,默认排他的(单选)
#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//添加默认选项ui->radioButton_other->setChecked(true);ui->label->setText("您的性别为: ?");
}Widget::~Widget()
{delete ui;
}void Widget::on_radioButton_male_clicked()
{ui->label->setText("您的性别为: 男");
}void Widget::on_radioButton_female_clicked()
{ui->label->setText("您的性别为: 女");
}void Widget::on_radioButton_other_clicked()
{ui->label->setText("您的性别为: 其他");}

这里可以看到QRadioButton默认是排他的,单选

image-20240909200830421

禁用选项:

想要禁用某个按钮,可以用setCheckable(false)

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//添加默认选项ui->radioButton_other->setChecked(true);ui->label->setText("您的性别为: ?");//禁用ui->radioButton_disable->setCheckable(false);
}Widget::~Widget()
{delete ui;
}void Widget::on_radioButton_male_clicked()
{ui->label->setText("您的性别为: 男");
}void Widget::on_radioButton_female_clicked()
{ui->label->setText("您的性别为: 女");
}void Widget::on_radioButton_other_clicked()
{ui->label->setText("您的性别为: 其他");}void Widget::on_radioButton_disable_clicked()
{ui->label->setText("error");
}

这里虽然不可选中,但是还是触发了点击事件。

这是因为checkable只能够让按钮不被选中,但还是可以响应点击事件

GIF 2024-9-9 20-18-00

可以采用setEnabled或者setDisabled

ui->radioButton_disable->setDisabled(true);
//ui->radioButton_disable->setEnabled(false);

image-20240909202201705

QAbstractButton信号

image-20240909202612422

  • clicked:之前大多数用的都是cicked()信号,表示一次点击事件(等于一次pressed + released
  • clicked(bool):参数的bool,表示是否被选中
  • pressed():鼠标按下
  • released():鼠标释放
  • toggled(bool):切换的时候会触发
void Widget::on_radioButton_clicked(bool checked)
{//checked表示当前radioButton的选中状态qDebug() << "clicked: " << checked;
}void Widget::on_radioButton_4_pressed()
{qDebug() << "pressd";
}void Widget::on_radioButton_2_released()
{qDebug() << "released";
}void Widget::on_radioButton_3_toggled(bool checked)
{//checked状态发生改变,就会触发这个信号qDebug() << "toggled: " << checked;
}

GIF 2024-9-9 20-37-33

实现简单的点餐页面

image-20240909211700084

这里分为三组,需要选择三个,但是radioButton默认是排他的,所有我们可以分组,采用QButtonGroup针对单选按钮进行分组

#include "widget.h"
#include "ui_widget.h"
#include<QButtonGroup>
Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);//进行分组QButtonGroup *group1 = new QButtonGroup(this);QButtonGroup *group2 = new QButtonGroup(this);QButtonGroup *group3 = new QButtonGroup(this);//将按钮放入不同组里group1->addButton(ui->radioButton);group1->addButton(ui->radioButton_2);group1->addButton(ui->radioButton_3);group1->addButton(ui->radioButton_4);group2->addButton(ui->radioButton_5);group2->addButton(ui->radioButton_6);group2->addButton(ui->radioButton_7);group2->addButton(ui->radioButton_8);group3->addButton(ui->radioButton_9);group3->addButton(ui->radioButton_10);group3->addButton(ui->radioButton_11);group3->addButton(ui->radioButton_12);}Widget::~Widget()
{delete ui;
}

image-20240909212340649

QCheckBox

QRadioButton只能选一个,而QCheckBox可以选中多个。

QCheckBox最相关的属性也是checkablechecked,都是继承自QAbstractButton

QCheckBox独有的属性tristate,用来实现“三态复选框”

示例:

image-20240909215152983

#include "widget.h"
#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);
}Widget::~Widget()
{delete ui;
}void Widget::on_pushButton_clicked()
{QString ret = "今日:";if(ui->checkBox_work->isChecked()){ret += ui->checkBox_work->text() + " ";}if(ui->checkBox_eat->isChecked()){ret += ui->checkBox_eat->text() + " ";}if(ui->checkBox_sleep->isChecked()){ret += ui->checkBox_sleep->text() + " ";}if(ui->checkBox_drink->isChecked()){ret += ui->checkBox_drink->text() + " ";}ui->label->setText(ret);
}

GIF 2024-9-9 21-50-44

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • 【VSCode v1.93.0】手动配置远程remote-ssh
  • 开源可视化大屏superset Docker环境部署
  • 计算机网络练级第一级————认识网络
  • VSTO常见的异常
  • 【自然语言处理】实验一:基于NLP工具的中文分词
  • 7.1图像平移
  • 安科瑞Acrel-1000DP分布式光伏监控系统平台的设计与应用-安科瑞 蒋静
  • 哈希表、算法
  • jupyter notebook详细介绍-学习篇
  • 网络编程(UDP)
  • 云原生周刊:OpenTofu Registry 获得用户界面和 API|2024.9.9
  • 【C语言】揭开计数制的面纱:深入浅出二进制及二进制计算
  • JavaEE 第23节 TCP的流量控制与阻塞控制详解
  • 芝法酱学习笔记(0.1)——Ubuntu下,Java开发环境的基本搭建
  • 解锁Python中的人脸识别:Face Recognition库详解与应用
  • [iOS]Core Data浅析一 -- 启用Core Data
  • 【vuex入门系列02】mutation接收单个参数和多个参数
  • 【刷算法】从上往下打印二叉树
  • classpath对获取配置文件的影响
  • HTTP--网络协议分层,http历史(二)
  • maya建模与骨骼动画快速实现人工鱼
  • MySQL Access denied for user 'root'@'localhost' 解决方法
  • underscore源码剖析之整体架构
  • 阿里云前端周刊 - 第 26 期
  • 不上全站https的网站你们就等着被恶心死吧
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 前端设计模式
  • 如何合理的规划jvm性能调优
  • 什么是Javascript函数节流?
  • 使用docker-compose进行多节点部署
  • 学习使用ExpressJS 4.0中的新Router
  • shell使用lftp连接ftp和sftp,并可以指定私钥
  • ​HTTP与HTTPS:网络通信的安全卫士
  • (2)(2.10) LTM telemetry
  • (C++17) optional的使用
  • (二)正点原子I.MX6ULL u-boot移植
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (九)One-Wire总线-DS18B20
  • (十六)Flask之蓝图
  • (四)c52学习之旅-流水LED灯
  • (一)appium-desktop定位元素原理
  • (源码版)2024美国大学生数学建模E题财产保险的可持续模型详解思路+具体代码季节性时序预测SARIMA天气预测建模
  • .a文件和.so文件
  • .htaccess 强制https 单独排除某个目录
  • .NET Core 中插件式开发实现
  • .NET Framework与.NET Framework SDK有什么不同?
  • .Net Winform开发笔记(一)
  • .NET简谈互操作(五:基础知识之Dynamic平台调用)
  • .py文件应该怎样打开?
  • @FeignClient注解,fallback和fallbackFactory
  • [Android 13]Input系列--获取触摸窗口
  • [bzoj4010][HNOI2015]菜肴制作_贪心_拓扑排序
  • [C#][opencvsharp]opencvsharp sift和surf特征点匹配
  • [C++]C++入门--引用
  • [CentOs7]iptables防火墙安装与设置