传送门:https://www.cnblogs.com/Manual-Linux/p/9596517.html
widget.h
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QTextEdit>
#include <QButtonGroup>
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = nullptr);
~Widget();
public slots:
void slots_fruits();
void slots_vegetables();
private:
QString fruits;
QString vegetables;
QTextEdit *TextEdit;
QButtonGroup *groupButton1;
QButtonGroup *groupButton2;
Ui::Widget *ui;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include <QRadioButton>
#include <QGridLayout>
#include <QTextEdit>
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
QGridLayout *GridLayout = new QGridLayout();
fruits = tr("苹果");
vegetables = tr("青椒");
TextEdit = new QTextEdit();
TextEdit->setText(tr("当前选中的水果:")+fruits+tr("\n当前选中的蔬菜:")+vegetables);
TextEdit->setReadOnly(true);
QRadioButton *apple_radioButton = new QRadioButton();
apple_radioButton->setText(tr("苹果"));
QRadioButton *banan_radioButton = new QRadioButton();
banan_radioButton->setText(tr("香蕉"));
QRadioButton *pear_radioButton = new QRadioButton();
pear_radioButton->setText(tr("梨"));
QRadioButton *potato_radioButton = new QRadioButton();
potato_radioButton->setText(tr("土豆"));
QRadioButton *greenpepper_radioButton = new QRadioButton();
greenpepper_radioButton->setText(tr("青椒"));
QRadioButton *spinach_radioButton = new QRadioButton();
spinach_radioButton->setText(tr("菠菜"));
groupButton1 = new QButtonGroup();
groupButton1->addButton(apple_radioButton, 0);
groupButton1->addButton(banan_radioButton, 1);
groupButton1->addButton(pear_radioButton, 2);
apple_radioButton->setChecked(true);
groupButton2 = new QButtonGroup();
groupButton2->addButton(potato_radioButton, 0);
groupButton2->addButton(greenpepper_radioButton, 1);
groupButton2->addButton(spinach_radioButton, 2);
greenpepper_radioButton->setChecked(true);
GridLayout->addWidget(apple_radioButton, 0, 0);
GridLayout->addWidget(banan_radioButton, 0, 1);
GridLayout->addWidget(pear_radioButton, 0, 2);
GridLayout->addWidget(potato_radioButton, 1, 0);
GridLayout->addWidget(greenpepper_radioButton, 2, 0);
GridLayout->addWidget(spinach_radioButton, 3, 0);
GridLayout->addWidget(TextEdit, 4, 0, 1, 12);
GridLayout->setRowStretch(0,1);
GridLayout->setColumnStretch(0,1);
GridLayout->setColumnStretch(1,1);
GridLayout->setColumnStretch(2,1);
GridLayout->setColumnStretch(3,9);
GridLayout->setRowStretch(1,1);
GridLayout->setRowStretch(2,1);
GridLayout->setRowStretch(3,1);
GridLayout->setRowStretch(4,8);
connect(apple_radioButton, SIGNAL(clicked(bool)), this, SLOT(slots_fruits()));
connect(banan_radioButton, SIGNAL(clicked(bool)), this, SLOT(slots_fruits()));
connect(pear_radioButton, SIGNAL(clicked(bool)), this, SLOT(slots_fruits()));
connect(potato_radioButton, SIGNAL(clicked(bool)), this, SLOT(slots_vegetables()));
connect(greenpepper_radioButton, SIGNAL(clicked(bool)), this, SLOT(slots_vegetables()));
connect(spinach_radioButton, SIGNAL(clicked(bool)), this, SLOT(slots_vegetables()));
setLayout(GridLayout);
ui->setupUi(this);
}
void Widget::slots_fruits()
{
switch (groupButton1->checkedId()) {
case 0:
fruits = tr("苹果");
TextEdit->setText(tr("当前选中的水果:")+fruits+tr("\n当前选中的蔬菜:")+vegetables);
break;
case 1:
fruits = tr("香蕉");
TextEdit->setText(tr("当前选中的水果:")+fruits+tr("\n当前选中的蔬菜:")+vegetables);
break;
case 2:
fruits = tr("梨");
TextEdit->setText(tr("当前选中的水果:")+fruits+tr("\n当前选中的蔬菜:")+vegetables);
break;
}
}
void Widget::slots_vegetables()
{
switch (groupButton2->checkedId()) {
case 0:
vegetables = tr("土豆");
TextEdit->setText(tr("当前选中的水果:")+fruits+tr("\n当前选中的蔬菜:")+vegetables);
break;
case 1:
vegetables = tr("青椒");
TextEdit->setText(tr("当前选中的水果:")+fruits+tr("\n当前选中的蔬菜:")+vegetables);
break;
case 2:
vegetables = tr("菠菜");
TextEdit->setText(tr("当前选中的水果:")+fruits+tr("\n当前选中的蔬菜:")+vegetables);
break;
}
}
Widget::~Widget()
{
if(TextEdit)
{
delete TextEdit;
}
if(groupButton1)
{
delete groupButton1;
}
if(groupButton2)
{
delete groupButton2;
}
delete ui;
}
main.cpp
#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.show();
return a.exec();
}
运行结果: