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