【QT开发笔记-基础篇】

2023-10-27

本节对应的视频讲解:B_站_视_频

https://www.bilibili.com/video/BV11G411x7hN


Qt 中的单选按钮类是 QRadioButton

它是一个可以切换选中(checked)或未选中(unchecked)状态的单选按钮

单选按钮常用在 “多选一” 的场景,也就是说,在一组单选按钮中,一次只能选中一个单选按钮

比如性别中的 “男女” 二选一,学历中的 “博士/硕士/本科/其他” 四选一,等等。
单选按钮-案例


1. 属性和方法


QRadioButton 有很多属性,完整的可查看帮助文档。这里列出常用的属性和方法:


1.1 文本

这两个是其父类 QAbstractButton 中的属性和方法,因此 QPushButtonQRadioButtonQCheckBox 都具有该属性

// 获取和设置显示的文本
QString text() const
void setText(const QString &text)

1.2 选中状态

// 获取和设置单选按钮的选中状态
bool isChecked() const
void setChecked(bool)

可见,切换单选按钮的选中状态,有两种方式:

  • 通过鼠标点击实现
  • 在代码中使用 setChecked(bool) 来实现

1.3 自动排他

我们前面说过,单选按钮实现的是 “多选一”,因此单选按钮的该属性默认是使能的

// 获取和设置自动排他
bool autoExclusive() const
void setAutoExclusive(bool)

而对于多选按钮,也叫复选按钮-QCheckBox,通常的场景是用户选择一组按钮中的多个,因此该属性默认是禁能的。

综合以上,“多选一” 要满足以下两个条件:

  • 把同一组的单选按钮,放在同一个布局中。不同的组的单选按钮,放在不同的布局中

  • 单选按钮的 autoExclusive 属性设置为 true,单选按钮的该属性默认是使能的。可在右侧的属性按钮中看到,如下:
    自动排他


1.4 信号槽

按钮在按下和抬起的过程中,会发射多个信号。

// 单选按钮 QRadioButton 被点击时,会发出该信号
void clicked();

// 当单选按钮的选中状态发生改变时,会发射该信号
// 所谓状态改变,是指选中变为非选中,和非选中变为选中
void toggled(bool checked)

2. 案例


该案例演示,单选按钮的属性以及信号槽
案例


2.1 布局

UI 设计师界面,拖拽对应的控件,修改显示的文字、控件的 name,然后完成布局


2.2 代码实现

完整的项目,在本节视频的置顶评论下载即可

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 设置第2组中的默认显示
    ui->rbMale2->setChecked(true);
    ui->rbMaster2->setChecked(true);

    // 设置第3组中的默认显示
    ui->rbFemale3->setChecked(true);
    ui->rbDoctor3->setChecked(true);

    // 使用 QButtonGroup 对单选按钮进行分组
    mBtnGroupGender = new QButtonGroup(this);
    mBtnGroupGender->addButton(ui->rbMale4,0);
    mBtnGroupGender->addButton(ui->rbFemale4,1);

    mBtnGroupEdu = new QButtonGroup(this);
    mBtnGroupEdu->addButton(ui->rbDoctor4,10);
    mBtnGroupEdu->addButton(ui->rbMaster4,11);
    mBtnGroupEdu->addButton(ui->rbBachelor4,12);
    mBtnGroupEdu->addButton(ui->rbOther4,13);

    connect(ui->rbMale4, &QRadioButton::clicked, this, &Widget::onRadioButtonClicked);
    connect(ui->rbFemale4, &QRadioButton::clicked, this, &Widget::onRadioButtonClicked);
    connect(ui->rbDoctor4, &QRadioButton::clicked, this, &Widget::onRadioButtonClicked);
    connect(ui->rbMaster4, &QRadioButton::clicked, this, &Widget::onRadioButtonClicked);
    connect(ui->rbBachelor4, &QRadioButton::clicked, this, &Widget::onRadioButtonClicked);
    connect(ui->rbOther4, &QRadioButton::clicked, this, &Widget::onRadioButtonClicked);

    // 设置第4组中的默认显示
    ui->rbMale4->setChecked(true);
    ui->rbBachelor4->setChecked(true);
    onRadioButtonClicked();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_btnGetSelection_clicked()
{
    QString s;

    if(ui->rbMale3->isChecked()) {
        s += ui->rbMale3->text();
    } else if(ui->rbFemale3->isChecked()) {
        s += ui->rbFemale3->text();
    }

    if(ui->rbDoctor3->isChecked()) {
        s += ui->rbDoctor3->text();
    } else if(ui->rbMaster3->isChecked()) {
        s += ui->rbMaster3->text();
    } else if(ui->rbBachelor3->isChecked()) {
        s += ui->rbBachelor3->text();
    } else if(ui->rbOther3->isChecked()) {
        s += ui->rbOther3->text();
    }

    ui->leResult3->setText(s);
}

void Widget::onRadioButtonClicked()
{
    QString s;

    int checkedGenderId = mBtnGroupGender->checkedId();
    if(checkedGenderId == 0) {
        s += "男";
    } else if(checkedGenderId == 1) {
        s += "女";
    }

    int checkedEduId = mBtnGroupEdu->checkedId();
    if(checkedEduId == 10) {
        s += "博士";
    } else if(checkedEduId == 11) {
        s += "硕士";
    } else if(checkedEduId == 12) {
        s += "本科";
    } else if(checkedEduId == 13) {
        s += "其他";
    }

    ui->leResult4->setText(s);
}

void Widget::on_rbMale4_toggled(bool checked)
{
    qDebug()<< "rbMale4 state changed: " << checked;
}

本节对应的视频讲解:B_站_视_频

https://www.bilibili.com/video/BV11G411x7hN

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【QT开发笔记-基础篇】 的相关文章

随机推荐