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

qt QRadioButton详解

QRadioButton 是一个可以切换选中(checked)或未选中(unchecked)状态的选项按钮。单选按钮通常呈现给用户一个“多选一”的选择,即在一组单选按钮中,一次只能选中一个按钮。

重要方法

  • QRadioButton(QWidget* parent = nullptr):默认构造函数。
  • QRadioButton(const QString& text, QWidget* parent = nullptr):使用指定的文本创建单选按钮。
  • void setText(const QString& text):设置单选按钮显示的文本。
  • void setChecked(bool checked):设置单选按钮的选中状态,true为选中,false为未选中。
  • bool isChecked() const:返回单选按钮的选中状态,如果选中则返回true,否则返回false。
  • void setIcon(const QIcon& icon):设置单选按钮显示的图标。
  • void setShortcut(const QKeySequence& shortcut):设置单选按钮的快捷键。

重要信号

  • toggled(bool checked):当单选按钮切换选中状态时发射,参数为是否选中。
  • clicked(bool checked):当单选按钮被点击时发射,参数为是否选中。
#include <QApplication>  
#include <QWidget>  
#include <QHBoxLayout>  
#include <QRadioButton>  
#include <QButtonGroup>  int main(int argc, char *argv[]) {  QApplication app(argc, argv);  // 创建主窗口  QWidget window;  window.setWindowTitle("RadioButton 示例");  // 创建水平布局  QHBoxLayout* layout = new QHBoxLayout(&window);  // 创建按钮组  QButtonGroup* buttonGroup = new QButtonGroup(&window);  // 创建三个单选按钮  QRadioButton* radioBtn1 = new QRadioButton("选项1");  QRadioButton* radioBtn2 = new QRadioButton("选项2");  QRadioButton* radioBtn3 = new QRadioButton("选项3");  // 默认设置第一个单选按钮选中  radioBtn1->setChecked(true);  // 将单选按钮添加到按钮组中  buttonGroup->addButton(radioBtn1);  buttonGroup->addButton(radioBtn2);  buttonGroup->addButton(radioBtn3);  // 将按钮组的 exclusive 属性设置为 true, 确保只能选中一个选项  buttonGroup->setExclusive(true);  // 将单选按钮添加到布局中  layout->addWidget(radioBtn1);  layout->addWidget(radioBtn2);  layout->addWidget(radioBtn3);  // 设置主窗口的布局  window.setLayout(layout);  // 显示主窗口  window.show();  return app.exec();  
}

觉得有帮助的话,打赏一下呗。。

           

http://www.lryc.cn/news/474614.html

相关文章:

  • Qt 最小化,最大化,关闭窗口
  • 【vue项目中添加告警音频提示音】
  • 百度SEO分析实用指南 提升网站搜索排名的有效策略
  • 高并发场景下的性能测试方法!
  • 杂项——USB键盘与鼠标流量分析——BUUCTF——流量分析
  • Java如何实现企业微信审批流程
  • GEE app:在地图上构建一个可以查看局部的小窗
  • leetcode71:简化路径
  • nodejs入门教程4:nodejs创建第一个应用
  • 启用 iPhone 原生的五笔输入
  • 这个工具让你轻松开发一个带AI功能的Notion
  • 光耦合器的关键作用和创新---腾恩科技
  • 穿越死锁的迷雾:pthread_mutex_lock的终极挑战与破解策略
  • Dockerfile制作Oracle19c镜像
  • 【时间之外】IT人求职和创业应知【23】
  • 后端:Spring-1
  • OTX系统架构分析
  • ASO如何低预算进行优化
  • 非线性数据结构之数
  • 个人开发三步走
  • qt QAction详解
  • 建立maven项目常见问题解决办法
  • Windows 10 安装使用Docker踩过的坑和解决-31/10/2024
  • 微服务之间的调用关系
  • Chinese Spelling Correction as Rephrasing Language Model(AAAI2024)
  • DirectShow过滤器开发-写MP3音频文件过滤器(再写 写MP3)
  • 文章解读与仿真程序复现思路——电力自动化设备EI\CSCD\北大核心《基于对等架构的虚拟电厂-配电网双层电碳协同调度模型》
  • 大数据-204 数据挖掘 机器学习理论 - 混淆矩阵 sklearn 决策树算法评价
  • Fsm1
  • C. Gorilla and Permutation