QT5 之 RadioButton,在Ui设计师进行设计的分组

文章介绍了如何在QT5中使用QButtonGroup对RadioButton进行分组,通过代码法将radioButton_a/b/c分组,并添加点击事件处理函数show_info,展示了如何创建和连接信号与槽以响应按钮点击。同时提到,部件设计时可能出现错误提示,需构建后才能正确显示。
摘要由CSDN通过智能技术生成

QT5 之 RadioButton,在Ui设计师进行设计的分组(半代码法)

QT5 之 RadioButton组(纯代码法)

QT5 之 RadioButton,在Ui设计师进行设计的分组(半代码法)

//QT5 之 RadioButton,在Ui设计师进行设计的分组(半代码法)
//radioButton_a/b/c 在 Qt 设计师进行。
//分组采用代码法
      
#include <QButtonGroup>

    QButtonGroup  * m_group_1 = new QButtonGroup(this);
    m_group_1->addButton(ui->radioButton_a, 1);
    m_group_1->addButton(ui->radioButton_b, 2);
    m_group_1->addButton(ui->radioButton_c, 3);

RadioButton在设计师中实现;无需采用代码来实现。

分组采用代码法。

注意:有时在设计师中设计的部件,有错误提示。需要进行 build构建之后,才能正确显示。

QT5 之 RadioButton组

前言

虽然之前用过QradioButton,但是那时只有一组选项用到了QradioButton,所以设不设置组都没关系,但是以后肯定会遇到多组值需要用QradioButton的,提前学习下。
开始

这里主要用到了一个类,QButtonGroup,它还可以用到其他的地方,这里暂时只讲解QradioButton,其余的类似。(注意,QButtonGroup没有可视化属性,不是QWidget的子类)

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QRadioButton>
#include <QButtonGroup>
#include <QVBoxLayout>

class Widget : public QWidget
{
    Q_OBJECT
    
public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    
private slots:
    void show_info(QAbstractButton *button);
    
private:
    QRadioButton *m_radio_btn_1;
    QRadioButton *m_radio_btn_2;
    QRadioButton *m_radio_btn_3;
    QButtonGroup *m_group_1;
    QHBoxLayout *m_layout_1;
    
    QRadioButton *m_radio_btn_4;
    QRadioButton *m_radio_btn_5;
    QRadioButton *m_radio_btn_6;
    QButtonGroup *m_group_2;
    QHBoxLayout *m_layout_2;
    
    QVBoxLayout *m_main_layout;
};
#endif // WIDGET_H

#include "widget.h"
#include <QMessageBox>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
{
    m_radio_btn_1 = new QRadioButton(tr("1"), this);
    m_radio_btn_2 = new QRadioButton(tr("2"), this);
    m_radio_btn_3 = new QRadioButton(tr("3"), this);
    
    m_group_1 = new QButtonGroup(this);
    m_group_1->addButton(m_radio_btn_1, 1);
    m_group_1->addButton(m_radio_btn_2, 2);
    m_group_1->addButton(m_radio_btn_3, 3);
    
    m_layout_1 = new QHBoxLayout;
    m_layout_1->addWidget(m_radio_btn_1);
    m_layout_1->addWidget(m_radio_btn_2);
    m_layout_1->addWidget(m_radio_btn_3);
    
    m_radio_btn_4 = new QRadioButton(tr("4"), this);
    m_radio_btn_5 = new QRadioButton(tr("5"), this);
    m_radio_btn_6 = new QRadioButton(tr("6"), this);
    
    m_group_2 = new QButtonGroup(this);
    m_group_2->addButton(m_radio_btn_4, 4);
    m_group_2->addButton(m_radio_btn_5, 5);
    m_group_2->addButton(m_radio_btn_6, 6);
    
    m_layout_2 = new QHBoxLayout;
    m_layout_2->addWidget(m_radio_btn_4);
    m_layout_2->addWidget(m_radio_btn_5);
    m_layout_2->addWidget(m_radio_btn_6);
    
    m_main_layout = new QVBoxLayout(this);
    m_main_layout->addLayout(m_layout_1);
    m_main_layout->addLayout(m_layout_2);
    setLayout(m_main_layout);
    
    connect(m_group_1, SIGNAL(buttonClicked(QAbstractButton *)), this, SLOT(show_info(QAbstractButton *)));
    connect(m_group_2, SIGNAL(buttonClicked(QAbstractButton *)), this, SLOT(show_info(QAbstractButton *)));
}

Widget::~Widget()
{
}

void Widget::show_info(QAbstractButton *button)
{
    QRadioButton *radio_button = (QRadioButton *)button;
    QMessageBox::information(this, tr("info"), tr("message:").append(radio_button->text()), QMessageBox::Yes);
}

演示成果:

这里提一下,connect里面可以用QButtonGroup来发信号,也可以用QRadioButton来进行,看你具体的需求,这样,这6个QRadioButton就被分为了两组,可以分别进行各自的点击事件。
————————————————
版权声明:本文为CSDN博主「~梦里花开~」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_45718152/article/details/105711899

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值