按钮控件之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);
}
相关推荐
啊森要自信7 小时前
【QT】常⽤控件详解(四)常用显示类控件类 Label && LCDNumber && ProgressBar && Calendar Widget
开发语言·数据库·c++·qt·qt6.3
Simple_core8 小时前
qt贝塞尔曲线演示工具
开发语言·qt
sannianji8 小时前
qt 采用movetothread在应用程序gui关闭时如何正确退出。
开发语言·qt
幽迷狂11 小时前
AFSIM入门教程03.03:更新所有依赖库版本
c++·qt·仿真·osgearth·osg·军事·afsim
Liuqz200916 小时前
VSCode中使用Qt
ide·vscode·qt
赵_|大人19 小时前
Qt 自动无法加载数据库为空
开发语言·数据库·qt
枫叶丹41 天前
【Qt开发】常用控件(一)
开发语言·qt
TechNomad1 天前
QML开发:QML的第一个程序
qt
浮生卍流年1 天前
C++模板知识点3『std::initializer_list初始化时逗号表达式的执行顺序』
开发语言·c++·qt
huxiao_06012 天前
如何手动打包 Linux(麒麟系统)的 Qt 程序
linux·qt