按钮控件之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);
}
相关推荐
csdndenglu1 小时前
QT 5.9.2+VTK8.0实现等高线绘制
开发语言·qt
@Turbo@1 小时前
【QT】QString& 与QString区别
开发语言·qt
小灰灰搞电子6 小时前
Qt 仪表盘源码分享
开发语言·qt
菜鸟看点7 小时前
QT中子线程触发主线程弹窗并阻塞等待用户响应
qt
byxdaz7 小时前
Qt OpenGL 3D 编程入门
qt·opengl
誰能久伴不乏10 小时前
深度解析 Qt 最顶层类 QObject:继承关系与内存生命周期管理
开发语言·qt
菜鸟看点11 小时前
QT中子线程触发主线程弹窗并阻塞等待用户响应-传统信号槽实现
qt
朱小勇本勇12 小时前
Qt自带示例及官方文档学习
开发语言·qt·学习
Wyn_14 小时前
【QT】自定义QWidget标题栏,可拖拽(拖拽时窗体变为normal大小),可最小/大化、关闭(图文详情)
开发语言·qt
@Turbo@14 小时前
【QT】在Qt6的`QTextEdit`中,同一行更新内容
开发语言·qt