QT之QButtonGroup用法

 

QButtonGroup方法来实现分组:将相同功能的按键,设为一个分组,然后可以进行 单选 或 多选 或 互斥单选

Qt5之QRadioButton

 

QAbstractButton类是按钮部件的抽象基类,提供了按钮所共有的功能。

Qt 之 QAbstractButton

 

Qt 之 QCheckBox

 

==============================================================

 

#ifndef ADDSTU_H
#define ADDSTU_H

#include 
#include 

namespace Ui {
class addStu;
}

class addStu : public QDialog
{
    Q_OBJECT

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

    void clearUserInterface();
    void writeToFile(QString cnt);


private slots:
    void on_btn_ok_clicked();

    void on_btn_cancel_clicked();

private:
    Ui::addStu *ui;

    QButtonGroup *sexGroup;//性别按钮组
    QButtonGroup *insGroup;//兴趣按钮组
};

#endif // ADDSTU_H

 

#include "addstu.h"
#include "ui_addstu.h"
#include
#include
#include //要显示line里面的内容要加的
//#include 
#include //存到文件需要加的
#include 
#include 

addStu::addStu(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::addStu)
{
    ui->setupUi(this);

    ui->le_id->setPlaceholderText("请输入学号");
    ui->le_id->setClearButtonEnabled(true);

    sexGroup = new QButtonGroup(this);
    sexGroup->addButton(ui->rbtn_male,0);
    sexGroup->addButton(ui->rbtn_female,1);
    ui->rbtn_male->setChecked(true);

    insGroup = new QButtonGroup(this);
    insGroup->addButton(ui->checkBox,0);
    insGroup->addButton(ui->checkBox_2,1);
    insGroup->addButton(ui->checkBox_3,2);
    insGroup->addButton(ui->checkBox_4,3);
    // 设置不互斥
    insGroup->setExclusive(false);//这样的话就支持多选了。
}

addStu::~addStu()
{
    delete ui;
}
//https://blog.csdn.net/cxp2205455256/article/details/44956051
//https://blog.csdn.net/liang19890820/article/details/50976944/
void addStu::on_btn_ok_clicked()
{
    QString name = this->ui->le_name->text();
    QString id = this->ui->le_id->text();
    QString sex = this->sexGroup->checkedButton()->text();
    QString age = ui->cbb_age->currentText();
    QString faculty = ui->cbb_faculty->currentText();

    qDebug() << name;
    qDebug() << id;
    qDebug() << sex;

    QList ins_list = insGroup->buttons();
    QString ins;
    for(int i =0 ;iisChecked())
            ins += che->text() + ",";
    }

/*
    QMessageBox msgBox;
    msgBox.setText("The document has been modified.");
    msgBox.setInformativeText("Do you want to save your changes?");
    msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
    msgBox.setDefaultButton(QMessageBox::Save);
    int ret = msgBox.exec();
    */

    QString content, writetofileContent;

    content = name +'\n'+id +'\n'+sex +'\n'+age+'\n'+faculty+'\n'+ins;
    writetofileContent= name +' '+id +' '+sex +' '+age+' '+faculty+' '+ins;
    qDebug() << writetofileContent;
    if(name.length() <1 || id.length() <1)
    {
        QMessageBox::critical(this,"错误","信息填写不完整,请重新检查","确定");
    }else{
        int ret;
        ret = QMessageBox::question(this,"确认添加吗?",content,"确认","取消");
        qDebug() << ret;
        if(ret == 0)
        {
            clearUserInterface();

            writetofileContent += '\n';
            writeToFile(writetofileContent);
        }
    }

}

void addStu::on_btn_cancel_clicked()
{
    this->close();
}

void addStu::clearUserInterface()
{
    ui->le_name->clear();
    ui->le_id->clear();
    ui->rbtn_male->setChecked(true);
    ui->cbb_age->setCurrentIndex(0);
    ui->cbb_faculty->setCurrentIndex(0);

    QList ins_list = insGroup->buttons();
    QString ins;
    for(int i =0 ;isetChecked(false);
    }
}

void addStu::writeToFile(QString cnt)
{
    QFile file("stu.txt");
    if (!file.open(QIODevice::Append | QIODevice::Text))
        return;

    QTextStream out(&file);
    out << cnt;
}


 

你可能感兴趣的:(QT)