Qt常用控件之单选按钮QRadioButton

单选按钮QRadioButton

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

1. RadioButton属性

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

2. 用RadioButton设置单选按钮

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

cpp 复制代码
//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("你的性别为:沃尔玛购物袋");
}

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


3. 用RadioButton设置不可选按钮

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

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

4. RadioButton按下的不同策略

RadioButton 有四个槽函数:

槽函数 说明
clicked() 表示一次点击(即鼠标按下和释放)。
press() 表示鼠标按下(按下的时候就会触发事件)。
released() 表示鼠标释放
toggled() 表示按钮状态切换(只有在按钮从选中和未选中之间切换时才会触发事件)
cpp 复制代码
//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()<<"取消选中武装直升机";
}

窗口效果为:

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

5. QButtonGroup

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

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

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

示例:

cpp 复制代码
#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("学历为:本科");

}
相关推荐
lsx202406几秒前
PHP MySQL Order By
开发语言
笑春风oO1 分钟前
使用国内镜像源加速Qt“更新/安装”的方法【Ubuntu篇】
开发语言·qt
asdzx671 分钟前
Python: 从 PPT 提取图片和文本
开发语言·python·powerpoint
枫叶丹42 分钟前
【HarmonyOS 6.0】AVCodec Kit 同步模式视频编解码深度解析:从API演进到高性能实战
开发语言·华为·harmonyos·视频编解码
jjjava2.05 分钟前
计算机体系与进程管理全解析
java·开发语言
AI人工智能+电脑小能手6 分钟前
【大白话说Java面试题】【Java基础篇】第5题:HashMap的底层原理是什么
java·开发语言·数据结构·后端·面试·hash-index·hash
小成202303202657 分钟前
数据结构(整理常见结构总结到树层级)
java·c语言·数据结构·c++·链表
ximu_polaris8 分钟前
设计模式(C++)-结构型模式-外观模式
c++·设计模式·外观模式
谢谢 啊sir8 分钟前
L1-120 智慧文本编辑器 - java
java·开发语言
sycmancia12 分钟前
Qt——缓冲区操作与目录操作
开发语言·qt