按钮控件之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);
}
相关推荐
水煎包V:YEDIYYDS8881 天前
QT QML 实现的摇杆按钮,类似王者荣耀 左边方向导航键
qt·qml·摇杆按钮·导航键
winfield8211 天前
MCP 协议详解
开发语言·网络·qt
秦jh_1 天前
【Qt】常用控件(上)
服务器·数据库·qt
刃神太酷啦1 天前
C++ list 容器全解析:从构造到模拟实现的深度探索----《Hello C++ Wrold!》(16)--(C/C++)
java·c语言·c++·qt·算法·leetcode·list
水煎包V:YEDIYYDS8881 天前
QT modbus 通信教程,把modbus封装到线程单例中,在线程内完成数据收发,解析。把重要数据以信号方式通知到qml层展示,解决UI卡顿
qt·modbus·线程服务
东哥很忙XH2 天前
python使用PyQt5开发桌面端串口通信
开发语言·驱动开发·python·qt
汪宁宇2 天前
如何在QT5+MinGW环境中编译使用QGIS开发地图应用
c++·qt·qgis·mingw·地图库
刺客xs2 天前
Qt-----QSS样式表
开发语言·javascript·qt
qq_401700412 天前
QProgressBar+QSS 进度条
qt
小灰灰搞电子2 天前
Qt PDF模块详解
数据库·qt·pdf