• QtC++与QRadioButton详解


    介绍

    QRadioButton 是 Qt 中的一个重要部件,用于创建单选按钮,它有以下几个主要作用和特点:

    1. 单选功能: QRadioButton 用于创建单选按钮,用户可以从一组互斥的选项中选择一个。这在用户界面设计中常用于需要用户从多个选项中选择一个的情况。

    2. 单选按钮组: 您可以将多个 QRadioButton 放入同一个单选按钮组(QButtonGroup),以确保它们是互斥的,即只能选择其中一个。这使得用户界面设计更加清晰和易于理解。

    3. 三态单选按钮: QRadioButton 可以具有三种状态:选中、未选中和未确定。这在一些情况下很有用,例如当用户需要选择一个选项,但不确定应该选择哪个选项时。

    4. 样式和自定义: 您可以自定义 QRadioButton 的样式,包括文本、图标、背景颜色、字体等,以适应特定的应用程序设计。

    5. 信号与槽: QRadioButton 可以发出信号,用于响应用户的选择,您可以使用信号与槽机制连接 QRadioButton 的信号来执行相应的操作。

    6. 表单和表格中的使用: QRadioButton 通常用于表单和表格中,以便用户可以在这些界面中进行单选选择。

    7. 国际化和本地化: QRadioButton 可以包含本地化的文本,使应用程序能够以不同语言显示选项。

    8. 交互性和用户反馈: QRadioButton 通常伴随着用户界面元素,如标签(QLabel)和组框(QGroupBox),以提供一致的用户反馈。

    9. 跨平台: Qt 是一个跨平台的框架,因此 QRadioButton 可以在不同操作系统上提供一致的外观和行为。

    总之,QRadioButton 是 Qt 中用于创建单选按钮的重要工具,可用于创建具有单选功能的用户界面元素,以及用于实现用户输入选择的部分。它适用于各种应用程序,从简单的单选选项到复杂的表单和表格,使用户能够方便地做出选择。

    示例

    #include 
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        // 示例1: 创建单个单选按钮
        QWidget widget1;
        widget1.setWindowTitle("示例1: 创建单个单选按钮");
        QRadioButton radioButton1("选项1", &widget1);
        radioButton1.setChecked(true); // 默认选中
        widget1.show();
    
        // 示例2: 创建单选按钮组
        QWidget widget2;
        widget2.setWindowTitle("示例2: 创建单选按钮组");
        QRadioButton radioButton2("选项1", &widget2);
        QRadioButton radioButton3("选项2", &widget2);
        QRadioButton radioButton4("选项3", &widget2);
        
        QVBoxLayout layout2;
        layout2.addWidget(&radioButton2);
        layout2.addWidget(&radioButton3);
        layout2.addWidget(&radioButton4);
        widget2.setLayout(&layout2);
    
        // 示例3: 使用按钮组
        QWidget widget3;
        widget3.setWindowTitle("示例3: 使用按钮组");
        QRadioButton radioButton5("选项1", &widget3);
        QRadioButton radioButton6("选项2", &widget3);
        QRadioButton radioButton7("选项3", &widget3);
    
        QButtonGroup buttonGroup(&widget3);
        buttonGroup.addButton(&radioButton5);
        buttonGroup.addButton(&radioButton6);
        buttonGroup.addButton(&radioButton7);
        widget3.show();
    
        // 示例4: 获取选中的按钮
        QWidget widget4;
        widget4.setWindowTitle("示例4: 获取选中的按钮");
        QRadioButton radioButton8("选项1", &widget4);
        QRadioButton radioButton9("选项2", &widget4);
        QRadioButton radioButton10("选项3", &widget4);
    
        QButtonGroup buttonGroup2(&widget4);
        buttonGroup2.addButton(&radioButton8);
        buttonGroup2.addButton(&radioButton9);
        buttonGroup2.addButton(&radioButton10);
    
        // 示例5: 禁用单选按钮
        QWidget widget5;
        widget5.setWindowTitle("示例5: 禁用单选按钮");
        QRadioButton radioButton11("选项1", &widget5);
        QRadioButton radioButton12("选项2", &widget5);
        QRadioButton radioButton13("选项3", &widget5);
    
        radioButton12.setEnabled(false); // 禁用选项2
        widget5.show();
    
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        // 示例6: 使用按钮组管理单选按钮
        QWidget widget6;
        widget6.setWindowTitle("示例6: 使用按钮组管理单选按钮");
        QRadioButton radioButton14("选项1", &widget6);
        QRadioButton radioButton15("选项2", &widget6);
        QRadioButton radioButton16("选项3", &widget6);
    
        QButtonGroup buttonGroup3(&widget6);
        buttonGroup3.addButton(&radioButton14);
        buttonGroup3.addButton(&radioButton15);
        buttonGroup3.addButton(&radioButton16);
    
        // 示例7: 自定义样式
        QWidget widget7;
        widget7.setWindowTitle("示例7: 自定义样式");
        QRadioButton radioButton17("选项1", &widget7);
        QRadioButton radioButton18("选项2", &widget7);
    
        radioButton17.setStyleSheet("color: blue; font-weight: bold;");
        radioButton18.setStyleSheet("color: red; font-style: italic;");
        widget7.show();
    
        // 示例8: 使用信号与槽
        QWidget widget8;
        widget8.setWindowTitle("示例8: 使用信号与槽");
        QRadioButton radioButton19("选项1", &widget8);
        QRadioButton radioButton20("选项2", &widget8);
    
        // 连接信号与槽
        QObject::connect(&radioButton19, &QRadioButton::toggled, [](bool checked) {
            if (checked) {
                qDebug() << "选项1 被选中";
            }
        });
    
        QObject::connect(&radioButton20, &QRadioButton::toggled, [](bool checked) {
            if (checked) {
                qDebug() << "选项2 被选中";
            }
        });
    
        widget8.show();
    
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    #include 
    #include 
    #include 
    #include 
    
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
    
        // 示例9: 使用三态单选按钮
        QWidget widget9;
        widget9.setWindowTitle("示例9: 使用三态单选按钮");
        QRadioButton radioButton21("选项1", &widget9);
        QRadioButton radioButton22("选项2", &widget9);
        QRadioButton radioButton23("未确定选项", &widget9);
    
        radioButton23.setCheckState(Qt::PartiallyChecked); // 设置为未确定状态
        widget9.show();
    
        // 示例10: 单选按钮分组和互斥性
        QWidget widget10;
        widget10.setWindowTitle("示例10: 单选按钮分组和互斥性");
        QRadioButton radioButton24("选项1", &widget10);
        QRadioButton radioButton25("选项2", &widget10);
        QRadioButton radioButton26("选项3", &widget10);
    
        QButtonGroup buttonGroup4(&widget10);
        buttonGroup4.addButton(&radioButton24);
        buttonGroup4.addButton(&radioButton25);
        buttonGroup4.addButton(&radioButton26);
    
        widget10.show();
    
        // 示例11: 在表单中使用单选按钮
        QWidget widget11;
        widget11.setWindowTitle("示例11: 在表单中使用单选按钮");
        QRadioButton radioButton27("男", &widget11);
        QRadioButton radioButton28("女", &widget11);
    
        // 可以在表单布局中添加更多字段
    
        widget11.show();
    
        return a.exec();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
  • 相关阅读:
    H7-TOOL发布V2.18,脱机烧录新增芯海,辉芒微,武汉芯源,领芯等,支持了扫描枪,带来LUA写文件API,可以做日志或者数据记录了
    26-sparkstreaming
    GBase 8c审计日志维护(一)
    探究Kafka原理-1.初识Kafka
    研发效能的逻辑:填补软工鸿沟
    竞赛题-6253. 回环句
    【spring】@DependsOn注解学习
    pandas:时间序列数据的周期转换
    Ubuntu批量新建文件
    Linux学习 - vi/vim编辑器
  • 原文地址:https://blog.csdn.net/LuXiaoXin1999/article/details/134226961