按钮控件之3---QRadioButton 单选按钮/单选框控件

本文详细的介绍了QRadioButton控件的各种操作,例如:QRadioButton分组、默认选中、禁用启用、重置样式等操作。

一、QRadioButton部件提供了一个带有文本标签的单选框(单选按钮)。QRadioButton是一个可以切换选中 (checked)或未选中 (unchecked)状态的选项按钮。单选框呈现给用户一个**"多选一"**的选择。也就是说,在一组单选框中,一次只能选中一个单选框。

1、单选框默认开启自动互斥(autoExclusive)。如果启用了自动互斥,属于同一个父部件的单选框的行为就和属于一个互斥按钮组的一样。如果你需要为属于同一父部件 的单选框设置多个互斥按钮组,把它们加入QButtonGroup中。

2、每当一个按钮切换选中或未选中状态时,会发出的toggled()信号。如果希望每个按钮切换状态时触发一个动作,连接到这个信号。

3、使用isChecked ()来查看特定按钮是否被选中

4、就像QPushButton一样,单选框可以显示文本 ,以及可选的小图标。图标使用setIcon()来设置,文本可以在构造函数或通过setText()来设置。

5、可以指定快捷键 ,通过在文本中的特定字符前指定一个&。

二、

  1. Radio分组

    QButtonGroup *block1=new QButtonGroup(this); //分组
    QButtonGroup *block2=new QButtonGroup(this); //分组
    block1->addButton(ui->radioButton,0); //一个值为0
    block1->addButton(ui->radioButton_2,1); //一个值为1
    block1->addButton(ui->radioButton_3,2); //一个值为2

    block2->addButton(ui->radioButton_4,0);
    block2->addButton(ui->radioButton_5,1);
    block2->addButton(ui->radioButton_6,1);

2.默认选中

复制代码
ui->radioButton_3->setChecked(1); //默认选中

ui->radioButton_6->setChecked(1);

3.禁用启用、设置文本

复制代码
void MainWindow::on_radioButton_4_clicked()
{
        ui->radioButton_4->setEnabled(false);
        ui->radioButton_6->setEnabled(true);
        ui->radioButton_6->setText("文本");
        return;
}

4.判断焦点

复制代码
if(ui->radioButton_6->isChecked()){
     ui->radioButton_4->setEnabled(true);
     ui->radioButton_6->setEnabled(false);
}

5.设置样式

复制代码
//默认显示样式
ui->radioButton_7->setStyleSheet("#radioButton_7{background-color:rgb(134,183,200);border:2px solid #5F92B2;border-radius:5px;color:white;}"
//hover 鼠标停留样式
"#radioButton_7:hover{background-color:rgb(0,130,150);border:2px solid #5F92B2;border-radius:5px;color:white;}"
//pressed 鼠标点击样式
"#radioButton_7:pressed{background-color:rgb(85,170,255);border:2px solid #3C80B1;border-radius:5px;color:white;}"

6、Radio事件

复制代码
//没有被选中时
QRadioButton::indicator::unchecked {
     image: url(:/images/radiobutton_unchecked.png);
}
 
//选中时鼠标在上面悬停状态
QRadioButton::indicator:unchecked:hover {
     image: url(:/images/radiobutton_unchecked_hover.png);
}
 
//未选中时鼠标点击下按时状态
QRadioButton::indicator:unchecked:pressed {
     image: url(:/images/radiobutton_unchecked_pressed.png);
}
 
//被选中时
QRadioButton::indicator::checked {
     image: url(:/images/radiobutton_checked.png);
}
 
//被选中时鼠标在上面悬停状态
QRadioButton::indicator:checked:hover {
     image: url(:/images/radiobutton_checked_hover.png);
}
 
//被选中时鼠标下按
QRadioButton::indicator:checked:pressed {
     image: url(:/images/radiobutton_checked_pressed.png);
}
相关推荐
用户805533698031 天前
现代Qt开发教程(新手篇)1.14——日志
c++·qt
江公望1 天前
Qt QSharedPointer用法,10分钟讲清楚
开发语言·qt
Shadow(⊙o⊙)1 天前
初识Qt+经典方式实现hello world!的交互
开发语言·c++·后端·qt·学习
道剑剑非道1 天前
FFmpeg + Qt 实现摄像头采集与 MP3 背景音乐 RTSP 推流
开发语言·qt·ffmpeg
努力努力再努力wz1 天前
【Qt入门系列】第一个 Qt Widgets 程序:项目创建、UI 文件、Hello World、对象树与 qDebug 日志
java·c语言·开发语言·数据结构·c++·qt·ui
Hua-Jay1 天前
OpenCV联合C++/Qt 学习笔记(十五)----形态学操作及应用
c++·笔记·qt·opencv·学习·计算机视觉
chao1898441 天前
Qt Modbus TCP 通讯源码
qt·tcp/ip·命令模式
Hua-Jay1 天前
OpenCV联合C++/Qt 学习笔记(十六)----图像细化、轮廓检测、轮廓信息统计及轮廓外接多边形
c++·笔记·qt·opencv·学习·计算机视觉
lilong(DLC)1 天前
Qt信号槽在异步连接时需要将参数进行复制吗?
开发语言·qt
渣渣灰95871 天前
VSCode开发环境开发Qt程序
ide·vscode·qt