学生信息存储系统
基于Qt的框架,以及cereal开发一个学习信息存储系统。
完成的功能包括:保存信息,读取信息并显示。(仅仅是示例,没有考虑性能,内存问题)
代码如下:
//学生信息结构
class Student
{
public:
Student(){}
Student(QString,QString,QString,QString);
Student(QStringList);
template <class Archive>
void serialize(Archive & ar)
{
ar(CEREAL_NVP(name), CEREAL_NVP(id), CEREAL_NVP(sex), CEREAL_NVP(age));
}
QStringList getStudent();
private:
std::string name;
std::string id;
std::string sex;
std::string age;
};
//存储学生结构的数据,包括序列化的接口
class CerealModel
{
public:
CerealModel();
QVector <QStringList> getStudents(); //获取所有学生信息列表
void append(QStringList); //添加一个学生信息 void load(); //从文件加载学生信息 void save(); //保存信息到文件private: QString filePath; std :: vector <Student> m_students; }; //.cpp CerealModel :: CerealModel(){ filePath =“./ out.json”; //固定} QVector <QStringList> CerealModel :: getStudents(){ QVector <QStringList> lists; for(auto stu:m_students) { lists << stu.getStudent();
return lists;
}
void CerealModel::append(QStringList stu)
{
m_students.emplace_back(std::move(Student(stu)));
}
void CerealModel::load()
{
std::vector<Student> list;
ifstream in;
{
in.open("./out.json", ios::in);
cereal::JSONInputArchive archive(in);
archive(cereal::make_nvp("Students", list));
}
in.close();
qDebug() << list.size();
m_students = list;
}
void CerealModel::save()
{
qDebug() <<"save begin:"<<m_students.size();
ofstream out;
{
out.open(filePath.toStdString(), ios::trunc); //ios::trunc表示在打开文件前将文件清空,由于是写入,文件不存在则创建
cereal::JSONOutputArchive archive(out);
archive(cereal::make_nvp("Students", m_students));
}
out.close();
qDebug() <<"save end";
}
//qwidget.h
#include <QWidget>
class CerealModel;
namespace Ui {
class Widget;
}
class Widget : public QWidget
{
Q_OBJECT
public:
explicit Widget(QWidget *parent = 0);
~Widget();
private slots:
void on_pushButton_2_clicked();
void on_pushButton_3_clicked();
void on_pushButton_clicked();
private:
Ui::Widget *ui;
CerealModel *m_pModel = nullptr;
};
//qwidget.cpp
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
m_pModel = new CerealModel;
on_pushButton_2_clicked();
}
Widget::~Widget()
{
delete ui;
}
//查询所有信息接口
void Widget::on_pushButton_2_clicked()
{
//search
m_pModel->load();
ui->label_5->setText("读取所有的学生信息");
ui->textEdit->clear();
QVector<QStringList> lists = m_pModel->getStudents();
for(int i = 0; i < lists.size(); ++i)
{
QString str= QString("No: %1, Name: %2, Id: %3, Sex: %4, Age: %5")
.arg(i + 1)
.arg(lists[i][0])
.arg(lists[i][1])
.arg(lists[i][2])
.arg(lists[i][3]);
ui->textEdit->append(str);
}
}
//清空显示区域
void Widget::on_pushButton_3_clicked()
{
ui->textEdit->clear();
}
//
void Widget::on_pushButton_clicked()
{
QString name = ui->lineEdit->text();
QString id = ui->lineEdit_2->text();
QString sex = ui->lineEdit_3->text();
QString age = ui->lineEdit_4->text();
if(name.isNull() || id.isNull() || sex.isNull() || age.isNull())
{
QMessageBox::warning(this, "Warning", "请输入正常的学生信息", QMessageBox::Yes);
return;
}
m_pModel->append(QStringList() << name << id << sex <<age);
m_pModel->save();
ui->label_5->setText("新增学生信息");
ui->lineEdit->setText("");
ui->lineEdit_2->setText("");
ui->lineEdit_3->setText("");
ui->lineEdit_4->setText("");
on_pushButton_2_clicked();
}
示例界面如下:
打开Json文件可以得到如下信息: