QAbstractButton::setAutoExclusive() 是 Qt 中用于设置按钮自动排他性的方法。

自动排他性(Auto-Exclusive)意味着在同一父组件中的可选中按钮会自动形成互斥组,同一时间只能有一个按钮被选中。
#include <QPushButton>
#include <QButtonGroup>
#include <QVBoxLayout>
// 创建按钮
QPushButton *button1 = new QPushButton("按钮1");
QPushButton *button2 = new QPushButton("按钮2");
QPushButton *button3 = new QPushButton("按钮3");
// 设置按钮为可选中状态
button1->setCheckable(true);
button2->setCheckable(true);
button3->setCheckable(true);
// 启用自动排他性
button1->setAutoExclusive(true);
button2->setAutoExclusive(true);
button3->setAutoExclusive(true);
#include <QApplication>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QLabel>
class Example : public QWidget {
public:
Example(QWidget *parent = nullptr) : QWidget(parent) {
QVBoxLayout *layout = new QVBoxLayout(this);
// 创建标签显示状态
statusLabel = new QLabel("当前选择: 无");
layout->addWidget(statusLabel);
// 创建按钮
for (int i = 0; i < 5; ++i) {
QPushButton *btn = new QPushButton(QString("选项 %1").arg(i + 1));
btn->setCheckable(true);
btn->setAutoExclusive(true);
// 连接信号槽
connect(btn, &QPushButton::toggled, this, &Example::onButtonToggled);
layout->addWidget(btn);
buttons.append(btn);
}
}
private slots:
void onButtonToggled(bool checked) {
QPushButton *btn = qobject_cast<QPushButton*>(sender());
if (checked && btn) {
statusLabel->setText(QString("当前选择: %1").arg(btn->text()));
}
}
private:
QList<QPushButton*> buttons;
QLabel *statusLabel;
};
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
Example example;
example.show();
return app.exec();
}
QButtonGroup// 使用 setAutoExclusive(自动分组) button1->setAutoExclusive(true); button2->setAutoExclusive(true); // 同一父组件下的按钮自动形成互斥组 // 使用 QButtonGroup(手动分组) QButtonGroup *group = new QButtonGroup(this); group->addButton(button1); group->addButton(button2); group->setExclusive(true); // 设置排他性
// 模拟单选按钮行为
QWidget *createOptionGroup() {
QWidget *widget = new QWidget;
QVBoxLayout *layout = new QVBoxLayout(widget);
QStringList options = {"选项A", "选项B", "选项C", "选项D"};
for (const QString &option : options) {
QPushButton *btn = new QPushButton(option);
btn->setCheckable(true);
btn->setAutoExclusive(true);
btn->setStyleSheet(
"QPushButton {"
" text-align: left;"
" padding: 5px;"
"}"
"QPushButton:checked {"
" background-color: #0078d4;"
" color: white;"
"}"
);
layout->addWidget(btn);
}
return widget;
}
setCheckable(true) 才有效果QButtonGroup 可能更高效class ToolSelector : public QWidget {
public:
ToolSelector(QWidget *parent = nullptr) : QWidget(parent) {
QHBoxLayout *layout = new QHBoxLayout(this);
// 创建工具按钮
QStringList tools = {"选择", "画笔", "橡皮", "填充", "文字"};
for (const QString &tool : tools) {
QPushButton *btn = new QPushButton(tool);
btn->setCheckable(true);
btn->setAutoExclusive(true);
btn->setFixedSize(60, 30);
connect(btn, &QPushButton::toggled, [this, tool](bool checked) {
if (checked) {
qDebug() << "选中工具:" << tool;
}
});
layout->addWidget(btn);
}
// 默认选中第一个工具
if (layout->count() > 0) {
QPushButton *firstBtn = qobject_cast<QPushButton*>(layout->itemAt(0)->widget());
if (firstBtn) {
firstBtn->setChecked(true);
}
}
}
};