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

Qt常用控件之单选按钮QRadioButton

单选按钮QRadioButton

QRadioButton 是单选按钮,用于在多个选项中选择一个。

1. RadioButton属性

属性说明
checkable是否能被选中。
checked是否被选中(checkable 是 checked 的前提条件)。
autoExclusive是否排他,即选中这个按钮是否会取消其他按钮的选中,对于 QRadioButton 来说默认是排他的

2. 用RadioButton设置单选按钮

如用 RadioButton 设置一个问卷调查的性别选项菜单:

//widget.hpp
#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_radioButton_clicked()
{
    ui->label->setText("你的性别为:男");
}

void Widget::on_radioButton_2_clicked()
{
    ui->label->setText("你的性别为:女");
}

void Widget::on_radioButton_3_clicked()
{
    ui->label->setText("你的性别为:非二元性别");
}

void Widget::on_radioButton_4_clicked()
{
    ui->label->setText("你的性别为:武装直升机");
}

void Widget::on_radioButton_5_clicked()
{
    ui->label->setText("你的性别为:沃尔玛购物袋");
}

QRadioButton1

按钮只能被单选,选择一个另一个会变为未选择的状态。


3. 用RadioButton设置不可选按钮

注意让一个 RadioButton 不可选不可以只修改 checkable 属性为 false ,这个属性只控制当按钮点击时是否有选中反馈,点击按钮依然会触发 clicked 事件。要让一个 RadioButton 设置为不可选,可以使用 setEnable(false) 或者 setDisable(true) 接口将按钮不可选:

ui->radioButton_4->setEnabled(false);//武装直升机
ui->radioButton_5->setDisabled(true);//沃尔玛购物袋

QRadioButton2

4. RadioButton按下的不同策略

RadioButton 有四个槽函数:

槽函数说明
clicked()表示一次点击(即鼠标按下和释放)。
press()表示鼠标按下(按下的时候就会触发事件)。
released()表示鼠标释放
toggled()表示按钮状态切换(只有在按钮从选中和未选中之间切换时才会触发事件)
//widget.hpp
#include "widget.h"
#include "ui_widget.h"
#include <QDebug>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_radioButton_clicked()
{
    ui->label->setText("你的性别为:男");
}

void Widget::on_radioButton_2_pressed()
{
    ui->label->setText("你的性别为:女");
}

void Widget::on_radioButton_3_released()
{
    ui->label->setText("你的性别为:非二元性别");

}

void Widget::on_radioButton_4_toggled(bool checked)
{
    ui->label->setText("你的性别为:武装直升机");
    if(checked)
        qDebug()<<"选中了武装直升机";
    else
        qDebug()<<"取消选中武装直升机";
}

窗口效果为:

“女” 的按钮在按下后未松开就已经触发了,“武装直升机” 在切换按钮状态时会打印日志信息。

QRadioButton3

5. QButtonGroup

QButtonGroup 用于对单选按钮进行分类,注意要在堆上创建对象。

使用成员函数 addButton() 将按钮添加进组,不同组之间的 RadioButtonautoExclusive 属性互不影响,以此来实现对按钮的排他机制进行分组和分类。

使用时注意引 #include <QButtonGroup> 头文件。

示例:

#include "widget.h"
#include "ui_widget.h"
#include <QDebug>
#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);

    group2->addButton(ui->radioButton_3);
    group2->addButton(ui->radioButton_4);

    group3->addButton(ui->radioButton_5);
    group3->addButton(ui->radioButton_6);


}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_radioButton_clicked()
{
    ui->label->setText("性别为:男");
}

void Widget::on_radioButton_2_clicked()
{
    ui->label->setText("性别为:女");
}


void Widget::on_radioButton_3_clicked()
{
    ui->label_2->setText("年龄为:>=18岁");
}

void Widget::on_radioButton_4_clicked()
{
    ui->label_2->setText("年龄为:<18岁");

}

void Widget::on_radioButton_5_clicked()
{
    ui->label_3->setText("学历为:大专");

}

void Widget::on_radioButton_6_clicked()
{
    ui->label_3->setText("学历为:本科");

}

QRadioButton4


http://www.kler.cn/a/552553.html

相关文章:

  • 整合Salesmart/WhatsApp、开源Odoo模块和Deepseek AI能力,实现针对国外客户的智能客服和个性化推荐服务
  • 音频采集(VUE3+JAVA)
  • 对称加密算法——IDEA加密算法
  • 【第12章:深度学习与伦理、隐私—12.2 数据隐私保护与差分隐私技术的实现与应用】
  • 前x-ai首席科学家karpathy的从零构建ChatGPT视频学习笔记--8000字长图文笔记预警(手打纯干货,通俗易懂)
  • Linux 磁盘挂载教程
  • 计算机毕业设计--基于深度学习技术(Yolov11、v8、v7、v5)算法的高效人脸检测模型设计与实现(含Github代码+Web端在线体验界面)
  • 超全Deepseek资料包,deepseek下载安装部署提示词及本地部署指南介绍
  • IO、NIO解读和不同点,以及常用的文件流操作方法
  • 在 Vue 3 中使用 Lottie 动画:实现一个加载动画
  • [数据结构]复杂度详解
  • 人工智能技术-基于长短期记忆(LSTM)网络在交通流量预测中的应用
  • 【kafka系列】broker
  • 【C语言】第三期——判断语句
  • 文件操作(PHP)(小迪网络安全笔记~
  • 【模板】图论 最短路 (Floyd+SPFA+Dijkstra)
  • JAVA的Servlet一些知识(学习自用)
  • 【kafka系列】如何选择消息语义?
  • oracle获取当月1号
  • 正式页面开发-登录注册页面