Qt中QRadioButton的使用

QRadioButton 是 Qt 框架中的一个控件,用于创建单选按钮。单选按钮通常用于让用户从一组互斥的选项中选择一个选项。以下是如何在 C++ 中使用 QRadioButton 的基本示例。


1. 包含必要的头文件

首先,确保包含 QRadioButton 和其他必要的 Qt 头文件。

cpp 复制代码
#include <QApplication>
#include <QWidget>
#include <QRadioButton>
#include <QVBoxLayout>
#include <QMessageBox>

2. 创建主窗口类

创建一个继承自 QWidget 的主窗口类,并在其中添加 QRadioButton。

cpp 复制代码
class MyWindow : public QWidget {
    Q_OBJECT

public:
    MyWindow(QWidget *parent = nullptr) : QWidget(parent) {
        // 设置窗口标题
        setWindowTitle("QRadioButton 示例");

        // 创建布局
        QVBoxLayout *layout = new QVBoxLayout(this);

        // 创建单选按钮
        QRadioButton *radioButton1 = new QRadioButton("选项 1", this);
        QRadioButton *radioButton2 = new QRadioButton("选项 2", this);
        QRadioButton *radioButton3 = new QRadioButton("选项 3", this);

        // 将单选按钮添加到布局中
        layout->addWidget(radioButton1);
        layout->addWidget(radioButton2);
        layout->addWidget(radioButton3);

        // 设置默认选中的单选按钮
        radioButton1->setChecked(true);

        // 连接信号和槽,处理单选按钮的选择事件
        connect(radioButton1, &QRadioButton::clicked, this, &MyWindow::onRadioButtonClicked);
        connect(radioButton2, &QRadioButton::clicked, this, &MyWindow::onRadioButtonClicked);
        connect(radioButton3, &QRadioButton::clicked, this, &MyWindow::onRadioButtonClicked);
    }

private slots:
    void onRadioButtonClicked() {
        // 获取选中的单选按钮
        QRadioButton *radioButton = qobject_cast<QRadioButton*>(sender());
        if (radioButton && radioButton->isChecked()) {
            QMessageBox::information(this, "选择结果", "你选择了: " + radioButton->text());
        }
    }
};

3. 编写主函数

在 main 函数中创建应用程序对象和主窗口,并显示窗口。

cpp 复制代码
int main(int argc, char *argv[]) {
    QApplication app(argc, argv);

    // 创建主窗口
    MyWindow window;
    window.show();

    // 运行应用程序
    return app.exec();
}

4. 编译和运行

确保你已经安装了 Qt 开发环境,然后使用 qmake 和 make 编译程序。

bash 复制代码
qmake -project
qmake
make

运行生成的可执行文件,你将看到一个带有单选按钮的窗口。


5. 代码说明

  • QRadioButton:用于创建单选按钮。
  • setChecked(true):设置默认选中的单选按钮。
  • QRadioButton::clicked 信号:当单选按钮被点击时触发。
  • sender():获取触发信号的对象的指针。
  • QMessageBox:用于显示用户选择的结果。

6. 扩展功能

  • 分组单选按钮 :如果需要将单选按钮分组,可以使用 QButtonGroup。
  • 动态创建单选按钮:可以根据需要动态创建单选按钮并添加到布局中。
  • 样式设置 :可以使用 Qt 样式表(setStyleSheet)自定义单选按钮的外观。

例如,使用 QButtonGroup 分组:

cpp 复制代码
QButtonGroup *buttonGroup = new QButtonGroup(this);
buttonGroup->addButton(radioButton1);
buttonGroup->addButton(radioButton2);
buttonGroup->addButton(radioButton3);
相关推荐
重生之小比特18 分钟前
【C++进阶】封装红⿊树实现mymap和myset
开发语言·c++
边境悍匪22 分钟前
蜗牛学苑 Java 智能体学习 Day49|贯穿项目 5 订单下单业务 思维导图复盘
java·开发语言·spring boot·学习·阿里云
“AI国潮设计-小江”26 分钟前
【AIGC实战】Python+SDXL打造潮汕国潮IP:英歌舞麻将的自动化生成与商用思路
开发语言·人工智能·python·prompt·aigc
Roadinforest1 小时前
Spring Boot 4 六步入门实录:从 Hello World 到一个能交付的 Todo API
java·开发语言·spring boot·spring
88号技师1 小时前
2026年9月SCI-天气状态蚂蚁优化算法Weather State Ants Optimizer-附Matlab免费代码
开发语言·算法·matlab·优化算法
孙启超1 小时前
【AI开发之Rust】第 20 课:壳侧 API 封装 —— 把 Rust 能力接进真实 UI
开发语言·后端·rust
CarIise1 小时前
MyBatis缓存与分页插件:从一级二级缓存到底层原理与PageHelper实战
java·开发语言·spring
chenbingjie_c8 小时前
C++ 进阶核心知识点总结:模板、内存分区、new/delete、内存池
开发语言·c++
Escalating_xu10 小时前
【C 语言】深入理解指针(1·下):指针运算、野指针、assert 与传址实战
java·c语言·开发语言