主界面:
添加页面dialog:
dialog头文件:
#ifndef DIALOG_H
#define DIALOG_H
#include"student.h"
#include <QDialog>
#include<QCloseEvent>
namespace Ui {
class Dialog;
}
class Dialog : public QDialog
{
Q_OBJECT
public:
void closeEvent(QCloseEvent *event);
explicit Dialog(QWidget *parent = nullptr);
~Dialog();
public slots:
void on_pushButtonti_clicked();
void on_pushButtonqv_clicked();
signals:
void MyUpdate(Student* pStu);
private:
Ui::Dialog *ui;
};
#endif // DIALOG_H
dialog.cpp
#include "dialog.h"
#include "ui_dialog.h"
#include<QString>
#include"student.h"
#include<QMessageBox>
#include<QCloseEvent>
Dialog::Dialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dialog)
{
ui->setupUi(this);
}
Dialog::~Dialog()
{
delete ui;
}
void Dialog::on_pushButtonti_clicked()
{
//获取单号输入框的内容
QString name=ui->QlineEditxingming->text();
QString id=ui->lineEditxuehao->text();
QString sex=ui->lineEditnianling->text();
QString age=ui->lineEditxingbie->text();
QString chinese=ui->lineEdityuwen->text();
QString math=ui->lineEditshuxue->text();
QString english=ui->lineEdityingyu->text();
//判断数据是否完整
if(id!=""&&name!=""&&sex!=""&&age!=""&&chinese!=""&&math!=""&&english!=""){
//创建学生对象
Student* pStu=new Student;
pStu->SetId(id);
pStu->SetName(name);
pStu->SetSex(sex);
pStu->SetAge(age);
pStu->SetChinese(chinese);
pStu->SetMath(math);
pStu->SetEnglish(english);
//要将数据发给widget
emit MyUpdate(pStu);
}else{
QMessageBox::warning(this,"提示","请将数据录入完整");
}
}
void Dialog::on_pushButtonqv_clicked()
{
this->close();
}
void Dialog::closeEvent(QCloseEvent * event){
QMessageBox msgBox;
msgBox.setText("提示");
msgBox.setInformativeText("确认退出?");
msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Ok);
int ret = msgBox.exec();
if(ret == QMessageBox::Ok){
//若用户点击确认,则接收这个事件,当前窗口会关闭
event->accept();
}else{
//若用户点击取消,则忽略这个事件,当前窗口不会关闭
event->ignore();
}
};
ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include "dialog.h"
#include"student.h"
#include"control.h"
#include<QCloseEvent>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
public:
void ShowItem(Student* pStu);
void ShowList();
void closeEvent(QCloseEvent *event);
private slots:
void on_pushButtonadd_clicked();
void on_MyUpdate(Student* pStu);
void on_pushButtonsave_clicked();
private:
Ui::Widget *ui;
Dialog* pDialog;
control* pcontrol;
};
#endif // WIDGET_H
widget.cpp
#include "widget.h"
#include "ui_widget.h"
#include "dialog.h"
#include<QTableWidget>
#include<QMessageBox>
#include"control.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
setWindowTitle("学生管理系统");
pDialog=nullptr;
pcontrol=control::createctrl();
pcontrol->FileToList();
ShowList();
}
Widget::~Widget()
{
delete ui;
if(pDialog){
delete pDialog;
pDialog=nullptr;
}
}
void Widget::on_pushButtonadd_clicked()
{
if(pDialog){
delete pDialog;
pDialog=nullptr;
}
pDialog=new Dialog(this);
pDialog->setWindowTitle("添加学生信息");
QObject::connect(pDialog,&Dialog::MyUpdate,this,&Widget::on_MyUpdate);
pDialog->exec();
}
void Widget::on_MyUpdate(Student* pStu){
//将数据存入管理类的链条
pcontrol->additem(pStu);
//将数据显示到表各
ShowItem(pStu);
};
void Widget::ShowItem(Student* pStu){
//获得总行数+1
int rows=ui->tableWidget->rowCount();
//设置行数
ui->tableWidget->setRowCount(rows+1);
//将信息显示到表格上
ui->tableWidget->setItem(rows,0,new QTableWidgetItem(pStu->GetId()));
ui->tableWidget->setItem(rows,1,new QTableWidgetItem(pStu->GetSex()));
ui->tableWidget->setItem(rows,2,new QTableWidgetItem(pStu->GetAge()));
ui->tableWidget->setItem(rows,3,new QTableWidgetItem(pStu->GetName()));
ui->tableWidget->setItem(rows,4,new QTableWidgetItem(pStu->GetChinese()));
ui->tableWidget->setItem(rows,5,new QTableWidgetItem(pStu->GetMath()));
ui->tableWidget->setItem(rows,6,new QTableWidgetItem(pStu->GetEnglish()));
};
void Widget::on_pushButtonsave_clicked()
{
pcontrol->ListToFile();
}
void Widget::ShowList(){
auto ite=pcontrol->getlist().begin();
while(ite!=pcontrol->getlist().end()){
ShowItem(*ite);
ite++;
}
};
void Widget::closeEvent(QCloseEvent * event){
QMessageBox::StandardButton but=QMessageBox::question(this,"提示","是否保存数据");
if(but==QMessageBox::Yes){
pcontrol->ListToFile();
}
};
student.h
#ifndef STUDENT_H
#define STUDENT_H
#include<QString>
class Student
{
public:
Student();
private:
QString strId;
QString strName;
QString strAge;
QString strSex;
QString strChinese;
QString strMath;
QString strEnglish;
public:
QString GetId() const;
QString GetName() const;
QString GetAge() const;
QString GetSex() const;
QString GetChinese() const;
QString GetMath() const;
QString GetEnglish() const;
void SetId(const QString& id);
void SetName(const QString& name);
void SetAge(const QString& age);
void SetSex(const QString& sex);
void SetChinese(const QString& chinese);
void SetMath(const QString& math);
void SetEnglish(const QString& english);
};
#endif // STUDENT_H
student.cpp
#include "student.h"
Student::Student()
{
strId="";
strName="";
strAge="";
strSex="";
strChinese="";
strMath="";
strEnglish="";
}
QString Student::GetId() const
{
return strId;
};
QString Student::GetName() const{
return strName;
};
QString Student::GetAge() const{
return strAge;
};
QString Student::GetSex() const{
return strSex;
};
QString Student::GetChinese() const{
return strChinese;
};
QString Student::GetMath() const{
return strMath;
};
QString Student::GetEnglish() const{
return strEnglish;
};
void Student::SetId(const QString& id){
strId=id;
};
void Student::SetName(const QString& name){
strName=name;
};
void Student::SetAge(const QString& age){
strAge=age;
};
void Student::SetSex(const QString& sex){
strSex=sex;
};
void Student::SetChinese(const QString& chinese){
strChinese=chinese;
};
void Student::SetMath(const QString& math){
strMath=math;
};
void Student::SetEnglish(const QString& english){
strEnglish=english;
};
control.h文章来源:https://www.toymoban.com/news/detail-585952.html
#ifndef CONTROL_H
#define CONTROL_H
#include<list>
#include"student.h"
#include<fstream>
using namespace std;
#define PATH "..\\res\\Student.txt"
class control
{
private:
static control *pcontrol;
private:
control();
control(control &c);
list<Student*> lst;
public:
~control();
public:
static control *createctrl();
list<Student*> & getlist();
void additem(Student* pStu);
void deleteitem(QString id);
void ListToFile();
void FileToList();
};
#endif // CONTROL_H
control.cpp文章来源地址https://www.toymoban.com/news/detail-585952.html
#include "control.h"
#include<QString>
control::control()
{
} ;
control::control(control &c){
};
control::~control(){
auto ite=lst.begin();
while (ite!=lst.end()) {
delete *ite;
ite++;
}
};
control* control::pcontrol=nullptr;
control* control::createctrl(){
if(pcontrol==nullptr){
pcontrol=new control;
}
return pcontrol;
};
list<Student*>& control::getlist(){
return lst;
}
void control::additem(Student *pStu){
lst.push_back(pStu);
}
void control::deleteitem(QString id){
auto ite=lst.begin();
while(ite!=lst.end()){
if((*ite)->GetId()==id){
delete *ite;
lst.erase(ite);
break;
}
ite++;
}
}
void control::ListToFile(){
ofstream os;
os.open(PATH);
auto ite=lst.begin();
while(ite!=lst.end()){
string id=(*ite)->GetId().toStdString();
string name=(*ite)->GetName().toStdString();
string age=(*ite)->GetAge().toStdString();
string sex=(*ite)->GetSex().toStdString();
string chinese=(*ite)->GetChinese().toStdString();
string math=(*ite)->GetMath().toStdString();
string english=(*ite)->GetEnglish().toStdString();
os<<id<<""<<name<<""<<age<<""<<sex<<""<<chinese<<""
<<math<<""<<english<<endl;
ite++;
};
os.close();
}
void control::FileToList(){
ifstream is;
is.open(PATH);
string id,name,age,sex,chinese,math,english;
while(is>>id>>name>>age>>sex>>chinese>>math>>english){
Student* pStu=new Student;
pStu->SetId(QString::fromStdString(id));
pStu->SetName(QString::fromStdString(name));
pStu->SetAge(QString::fromStdString(age));
pStu->SetSex(QString::fromStdString(sex));
pStu->SetChinese(QString::fromStdString(chinese));
pStu->SetMath(QString::fromStdString(math));
pStu->SetEnglish(QString::fromStdString(english));
additem(pStu);
};
is.close();
}
到了这里,关于qt学生管理系统(一)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!