对单选按钮分组

我们在上节中创建了两个单选按钮,你会发现它们自动进行了分组。

我们现在再创建两个单选按钮控件。

QRadioButton *radioButton1, * radioButton2, ***radioButton3, ***radioButton4;

radioButton3 = new QRadioButton(this);
radioButton3->setText("单选按钮3");
radioButton3->setGeometry(300, 20, 100, 30);

radioButton4 = new QRadioButton(this);
radioButton4->setText("单选按钮4");
radioButton4->setGeometry(300, 50, 100, 30);

我们想让12成为一组34为另一组,但实际上他们4个是一组。

要解决这个问题,我们需要对单选按钮进行分组。

在 Qt 中,对单选按钮(QRadioButton)分组,主要有两种方式,使它们在同组内实现互斥选择

1. 利用父控件(自动分组)

如果多个单选按钮放在同一个父控件(比如同一个 QWidget 或 QGroupBox)内,Qt 会自动把它们归为一组,实现互斥。

我们创建的单选按钮使用的是同一个父控件,所以他们四个会自动分为一组。

示例:

QGroupBox* groupBox = new QGroupBox("选项组", this);
QRadioButton* radio1 = new QRadioButton("选项1", groupBox);
QRadioButton* radio2 = new QRadioButton("选项2", groupBox);

QVBoxLayout* layout = new QVBoxLayout(groupBox);
layout->addWidget(radio1);
layout->addWidget(radio2);
groupBox->setLayout(layout);


2. 使用 QButtonGroup 显式分组

如果单选按钮不在同一个父控件下,或者想更灵活地管理按钮组,可以使用 QButtonGroup。

  • 创建 QButtonGroup 对象
  • 使用 addButton() 把按钮添加进组
  • 按钮组内部自动实现互斥

示例:

QRadioButton* radio1 = new QRadioButton("选项1", this);
QRadioButton* radio2 = new QRadioButton("选项2", this);

QButtonGroup* group = new QButtonGroup(this);
group->addButton(radio1);
group->addButton(radio2);


额外说明

  • 默认情况下,QButtonGroup 的按钮是互斥的(单选)
  • 可以通过 setExclusive(false) 关闭互斥(允许多选)
  • 可以通过 buttonClicked(int id) 信号监听按钮点击事件

由于现在的单选按钮父控件相同,我们可以使用显式分组

为组定义一个变量

QButtonGroup* radioGroup;

新建组并向组内添加控件

radioGroup = new QButtonGroup(this);
radioGroup->addButton(radioButton3, 3);
radioGroup->addButton(radioButton4, 4);

执行程序后你就能实现分组选择

#include "MyFirstQt6.h"
#include <QtWidgets>

MyFirstQt6::MyFirstQt6(QWidget* parent)
: QMainWindow(parent)
{
setWindowTitle("第一个Qt6窗口");
resize(1200, 600);

QLabel* label = new QLabel(this);
label->setText("这是一个标签控件");
label->setGeometry(10, 30, 200, 50);

QPushButton* button = new QPushButton(this);
button->setText("这是一个按钮控件");
button->setGeometry(10, 80, 200, 50);
connect(button, &QPushButton::clicked, this, &MyFirstQt6::onButtonClicked);

lineEdit = new QLineEdit(this);
lineEdit->setGeometry(10, 130, 200, 50);

TextEdit = new QTextEdit(this);
TextEdit->setGeometry(10, 180, 300, 100);

PlainTextEdit = new QPlainTextEdit(this);
PlainTextEdit->setGeometry(10, 280, 300, 100);

radioButton1 = new QRadioButton(this);
radioButton1->setText("单选按钮1");
radioButton1->setGeometry(200, 20, 100, 30);

radioButton2 = new QRadioButton(this);
radioButton2->setText("单选按钮2");
radioButton2->setGeometry(200, 50, 100, 30);

radioButton3 = new QRadioButton(this);
radioButton3->setText("单选按钮3");
radioButton3->setGeometry(300, 20, 100, 30);

radioButton4 = new QRadioButton(this);
radioButton4->setText("单选按钮4");
radioButton4->setGeometry(300, 50, 100, 30);

radioGroup = new QButtonGroup(this);
radioGroup->addButton(radioButton3, 3);
radioGroup->addButton(radioButton4, 4);
}

MyFirstQt6::~MyFirstQt6()
{}

void MyFirstQt6::onButtonClicked()
{
QString text = TextEdit->to PlainText();
QMessageBox::information(this, "信息", "你输入的内容是: " + text);
}

部分内容省略了,课程内容有完整的详细教程。

计算机技术课程https://blog.csdn.net/2301_76542477/article/details/149032632?spm=1011.2415.3001.5331

相关推荐
jiunian_cn2 小时前
【Redis】数据库管理操作
数据库·redis·缓存
Sylvia-girl2 小时前
IO流~~
java·开发语言
打工哪有不疯的2 小时前
使用 MSYS2 为 Qt (MinGW 32/64位) 完美配置 OpenSSL
c++·qt
kali-Myon2 小时前
2025春秋杯网络安全联赛冬季赛-day2
python·安全·web安全·ai·php·pwn·ctf
Re.不晚2 小时前
JAVA进阶之路——无奖问答挑战3
java·开发语言
代码游侠2 小时前
C语言核心概念复习——C语言基础阶段
linux·开发语言·c++·学习
独行soc2 小时前
2026年渗透测试面试题总结-19(题目+回答)
android·网络·安全·web安全·渗透测试·安全狮
_Johnny_2 小时前
ETCD 配额/空间告警模拟方案
网络·数据库·etcd
㓗冽2 小时前
60题之内难题分析
开发语言·c++·算法
dingdingfish2 小时前
Bash学习 - 第3章:Basic Shell Features,第5节:Shell Expansions
开发语言·学习·bash