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() 将按钮添加进组,不同组之间的 RadioButton 的 autoExclusive 属性互不影响,以此来实现对按钮的排他机制进行分组和分类。

使用时注意引 #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("学历为:本科");

}
相关推荐
lunar_coder1 小时前
【C++】如何解决智能指针的析构函数,如何实现shared_ptr的引用计数以及解决shared_ptr的循环引用
c++·1024程序员节
白白白小纯2 小时前
C++进阶.多态
c++
2401_885885042 小时前
国际语音C#接口怎么写?.NET环境下实现国际语音验证码批量下发
开发语言·c#·.net
程序猿乐锅3 小时前
【从0-1学习Python | 第七篇】Python 面向对象基础
开发语言·笔记·后端·python·学习·1024程序员节
Wang's Blog3 小时前
Java 项目实战: 交友项目 - 查询圈子个人动态:查询我的动态需求分析
java·开发语言·交友
凯瑟琳.奥古斯特3 小时前
力扣1016高效解法:剪枝优化技巧
开发语言·c++·算法·leetcode·职场和发展
xy34533 小时前
Axure 9.0 列表框 介绍
ui·axure·原型·产品设计
xy34533 小时前
Axure 9.0 内联框架加载方式
前端·ui·html·axure·原型·产品设计
xiangyun613 小时前
C++ priority_queue 底层:手撕二叉堆与 Top K 问题
c++·算法·面试·stl·1024程序员节
坤坤子吖4 小时前
C++继承:友元、静态成员、多继承与组合
开发语言·c++·笔记·学习·1024程序员节