4. 复选框与单选按钮

任务目标

创建一个Qt窗口,放置两个QCheckBox(“启用高级模式”、“显示提示信息”)和两个QRadioButton(“模式A”、“模式B”),当用户切换任一控件状态时,在窗口底部的QLabel中实时显示当前所有控件的组合状态。

环境依赖

Qt 5或Qt 6(Widgets模块),支持C++11或更高版本。无需额外第三方库。

关键完整代码

#include <QApplication>
#include <QWidget>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QCheckBox>
#include <QRadioButton>
#include <QLabel>
#include <QGroupBox>

class DemoWidget : public QWidget {
    Q_OBJECT
public:
    DemoWidget(QWidget *parent = nullptr) : QWidget(parent) {
        // 复选框组
        auto *checkGroup = new QGroupBox("复选框", this);
        auto *checkLayout = new QVBoxLayout(checkGroup);
        chkAdvanced = new QCheckBox("启用高级模式", checkGroup);
        chkTips = new QCheckBox("显示提示信息", checkGroup);
        checkLayout->addWidget(chkAdvanced);
        checkLayout->addWidget(chkTips);

        // 单选按钮组(同一父对象自动互斥)
        auto *radioGroup = new QGroupBox("单选按钮", this);
        auto *radioLayout = new QVBoxLayout(radioGroup);
        radioA = new QRadioButton("模式A", radioGroup);
        radioB = new QRadioButton("模式B", radioGroup);
        radioA->setChecked(true);  // 默认选中模式A
        radioLayout->addWidget(radioA);
        radioLayout->addWidget(radioB);

        // 状态显示标签
        statusLabel = new QLabel(this);
        statusLabel->setWordWrap(true);

        // 主布局
        auto *mainLayout = new QVBoxLayout(this);
        mainLayout->addWidget(checkGroup);
        mainLayout->addWidget(radioGroup);
        mainLayout->addWidget(statusLabel);

        // 连接信号:任一控件状态变化时刷新显示
        connect(chkAdvanced, &QCheckBox::toggled, this, &DemoWidget::updateStatus);
        connect(chkTips, &QCheckBox::toggled, this, &DemoWidget::updateStatus);
        connect(radioA, &QRadioButton::toggled, this, &DemoWidget::updateStatus);
        connect(radioB, &QRadioButton::toggled, this, &DemoWidget::updateStatus);

        updateStatus();  // 初始显示
    }

private slots:
    void updateStatus() {
        QString mode = radioA->isChecked() ? "模式A" : "模式B";
        QString advanced = chkAdvanced->isChecked() ? "开启" : "关闭";
        QString tips = chkTips->isChecked() ? "显示" : "隐藏";
        statusLabel->setText(
            QString("当前状态:%1 | 高级模式:%2 | 提示信息:%3")
                .arg(mode, advanced, tips));
    }

private:
    QCheckBox *chkAdvanced;
    QCheckBox *chkTips;
    QRadioButton *radioA;
    QRadioButton *radioB;
    QLabel *statusLabel;
};

int main(int argc, char *argv[]) {
    QApplication app(argc, argv);
    DemoWidget w;
    w.setWindowTitle("复选框与单选按钮");
    w.resize(320, 240);
    w.show();
    return app.exec();
}

#include "main.moc"

运行方式

  1. 将上述代码保存为main.cpp。
  2. 在项目文件(.pro)中添加:QT += widgets。
  3. 编译运行:qmake && make(Linux/macOS)或使用Qt Creator直接构建运行。

验证方法

  • 程序启动后,底部标签应显示:当前状态:模式A | 高级模式:关闭 | 提示信息:隐藏。
  • 勾选“启用高级模式”,标签中“高级模式”变为“开启”。
  • 勾选“显示提示信息”,标签中“提示信息”变为“显示”。
  • 点击“模式B”,标签中“当前状态”变为“模式B”,且“模式A”自动取消选中——验证单选按钮互斥性。
  • 任意组合切换,标签内容始终与四个控件的选中状态一致,即判定通过。

关键说明

两个QRadioButton具有相同的父对象radioGroup,因此自动构成互斥组,无需额外代码。所有控件通过toggled(bool)信号连接到同一个槽函数,实现状态实时刷新。#include "main.moc"是单文件编译时必需的,若使用多文件工程结构则无需此行。