【Qt】设置互斥型按键组(bool QObject::blockSignals(bool block)) 两种方法

第一种
一、相关的函数
bool QObject::blockSignals(bool block)函数的文档说明
百度翻译:
如果block为true,则此对象发出的信号将被阻止(即,发出信号将不会调用与之连接的任何内容)。如果块为false,则不会发生此类块。
返回值是signalsBlocked()的前一个值。
请注意,即使此对象的信号被阻止,也会发出destroyed()信号。
阻塞时发出的信号不会被缓冲。

二、结果:
每个按键都要确保checkable属性勾选了。
在这里插入图片描述
点击任何一个按键的时候另一个按键会退出选中状态。
在这里插入图片描述
在这里插入图片描述

三、代码
.h文件

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private slots:
    void on_Btn1_clicked();

    void on_Btn2_clicked();

    void on_Btn3_clicked();

private:
    Ui::Widget *ui;

    void BtnBlockSignals(bool block);
};

#endif // WIDGET_H

.cpp文件

#include "widget.h"
#include "ui_widget.h"

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

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

void Widget::BtnBlockSignals(bool block)
{
    ui->Btn1->blockSignals(block);
    ui->Btn2->blockSignals(block);
    ui->Btn3->blockSignals(block);
}

void Widget::on_Btn1_clicked()
{
    BtnBlockSignals(true);
    ui->Btn1->setChecked(true);
    ui->Btn2->setChecked(false);
    ui->Btn3->setChecked(false);
    BtnBlockSignals(false);
}

void Widget::on_Btn2_clicked()
{
    BtnBlockSignals(true);
    ui->Btn1->setChecked(false);
    ui->Btn2->setChecked(true);
    ui->Btn3->setChecked(false);
    BtnBlockSignals(false);
}

void Widget::on_Btn3_clicked()
{
    BtnBlockSignals(true);
    ui->Btn1->setChecked(false);
    ui->Btn2->setChecked(false);
    ui->Btn3->setChecked(true);
    BtnBlockSignals(false);
}

第二种
使用按键组来实现
关键函数:setExclusive();
exclusive : bool
This property holds whether the button group is exclusive
If this property is true, then only one button in the group can be checked at any given time. The user can click on any button to check it, and that button will replace the existing one as the checked button in the group.
In an exclusive group, the user cannot uncheck the currently checked button by clicking on it; instead, another button in the group must be clicked to set the new checked button for that group.
By default, this property is true.
Access functions:
bool
exclusive() const
void
setExclusive(bool)
官方文档的百度翻译:
此属性保留按钮组是否是独占的如果此属性为true,则在任何给定时间只能选中组中的一个按钮。用户可以单击任何按钮来选中它,该按钮将替换组中现有的选中按钮。在独占组中,用户不能通过单击当前选中的按钮来取消选中它;相反,必须单击组中的另一个按钮才能为该组设置新的选中按钮。
默认情况下,此属性为true。

QButtonGroup* btnGroup = new QButtonGroup(this);
btnGroup->addButton(ui->Btn4,0);
btnGroup->addButton(ui->Btn5,1);
btnGroup->addButton(ui->Btn6,2);
btnGroup->setExclusive(true);

connect(btnGroup, SIGNAL(buttonClicked(int)), this, SLOT(on_BtnGroup(int)));
//槽函数
void Widget::on_BtnGroup(int index)
{
    qDebug() << "The" << index << "key is clicked";
}

运行结果:
在这里插入图片描述

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值