感受 :
这是本人在继C++之后,自学QT做的一个比较完整的项目,在这之中遇到过好多问题。好在查看了帮助文档,和网上查找都一一解决,经历这次的项目练习,我比之初接触QT更加深入的了解和掌握了更多的知识。所以要多加练习才能勤能补拙。
程序实现概况:
这次的项目是一个基于QT/C++的学生信息管理系统,学生属性有(name,age,sex,phone,colleage,address,hobby)应用程序包括7个界面,分别为(登录界面,注册界面,功能选择界面,添加学生界面,查看学生信息界面,删除学生信息界面,修改学生信息界面)。
实现代码:
Logoininterface.h
#pragma once
#include
#include
#include
#include
#include
#include
#include "ui_Logininterface.h"
#include "menu.h"
#include "newuser.h"
class Logininterface : public QMainWindow
{
Q_OBJECT
public:
Logininterface(QWidget *parent = Q_NULLPTR);
private slots:
int _login();//登录
int _newuser();//注册新用户
private:
Ui::LogininterfaceClass ui;
menu m;
newuser n;
QString inputaccount;
QString inputpassward;
};
Logininterface.cpp
#include "Logininterface.h"
Logininterface::Logininterface(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
//设置背景
QPixmap pixmap("F:\\vs2017\\Logininterface\\Logininterface\\image\\loginbackground.jpg");
QPalette palette;
palette.setBrush(backgroundRole(), QBrush(pixmap));
setPalette(palette);
//this->ui->flagface->setPixmap(QPixmap(":/image/flagdog.png"));
connect(this->ui.login, SIGNAL(clicked(bool)), this, SLOT(_login()));
connect(this->ui.newuser, SIGNAL(clicked(bool)), this, SLOT(_newuser()));
}
//登录
int Logininterface::_login()
{
int value = 0;
QString linedata;
QMessageBox mes;
inputaccount = this->ui.account_number->text();
inputpassward = this->ui.passward->text();
QString userinput_account = inputaccount + "&" + inputpassward;
QFile file("accountinformation.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return 0;
}
QTextStream in(&file);
while (!in.atEnd())
{
linedata = in.readLine();
//qDebug() << linedata;
if (linedata == userinput_account)
{
value++;
this->hide();
m.show();
}
}
if (value == 0)
{
mes.setWindowTitle("提示!!");
mes.setText("账号有误,重新输入!!");
mes.exec();
this->ui.account_number->clear();
this->ui.passward->clear();
}
file.close();
return 0;
}
//注册新用户
int Logininterface::_newuser()
{
this->hide();
n.show();
n.exec();
this->show();
return 0;
}
newuser.h
#pragma once
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif
#include
#include
#include
#include
#include
#include
#include "ui_newuser.h"
class newuser : public QDialog
{
Q_OBJECT
private slots:
int _okinformation();//确认信息
int _noinformation();//取消保存
int _return();//返回
public:
newuser(QWidget *parent = Q_NULLPTR);
~newuser();
private:
Ui::newuser ui;
};
newuser.cpp
#include "newuser.h"
newuser::newuser(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
//设置背景
QPixmap pixmap("F:\\vs2017\\Logininterface\\Logininterface\\image\\newuserbackground.jpg");
QPalette palette;
palette.setBrush(backgroundRole(), QBrush(pixmap));
setPalette(palette);
connect(this->ui.ok, SIGNAL(clicked(bool)), this, SLOT(_okinformation()));
connect(this->ui.no, SIGNAL(clicked(bool)), this, SLOT(_noinformation()));
connect(this->ui.reTurn, SIGNAL(clicked(bool)), this, SLOT(_return()));
}
newuser::~newuser()
{
}
//实现
//取消保存
int newuser::_noinformation()
{
this->ui.newaccount->clear();
this->ui.newpassward->clear();
this->ui.confirmaccount->clear();
this->ui.confirmpassward->clear();
return 0;
}
//确认账号信息
int newuser::_okinformation()
{
int value = 0;
QMessageBox messagebox;
messagebox.setWindowTitle("账号信息确认:");
//有空的时候
if (ui.newaccount->text().isEmpty() || ui.newpassward->text().isEmpty() ||
ui.confirmaccount->text().isEmpty() || ui.confirmpassward->text().isEmpty())
{
messagebox.setText("您设置的账号信息不完整!");
messagebox.exec();
}
QString newacc = ui.newaccount->text();
QString newpas = ui.newpassward->text();
QString conacc = ui.confirmaccount->text();
QString conpas = ui.confirmpassward->text();
QString linedata;
//账号不一致
if (newacc != conacc && newpas == conpas)
{
messagebox.setText("账号不一致请确认!");
messagebox.exec();
}
//密码不一致
if (newacc == conacc && newpas != conpas)
{
messagebox.setText("密码不一致请确认!");
messagebox.exec();
ui.newpassward->clear();
ui.confirmpassward->clear();
}
//账号密码均不对
if (newacc != conacc && newpas != conpas)
{
messagebox.setText("账号密码都不一致!请重新设置。");
messagebox.exec();
this->ui.newaccount->clear();
this->ui.newpassward->clear();
this->ui.confirmaccount->clear();
this->ui.confirmpassward->clear();
}
//账号信息无误
if (newacc == conacc && newpas == conpas)
{
messagebox.setText("是否确认保存账号?");
messagebox.exec();
//判断账号是否已注册
QFile file_2("accountinformation.txt");
if (!file_2.open(QIODevice::ReadOnly | QIODevice::Text))
{
return 0;
}
else
{
QTextStream in(&file_2);
while (!in.atEnd())
{
linedata = in.readLine();
if (linedata.indexOf(newacc) > -1)
{
value++;
}
}
}
//保存
if (value == 0)
{
QFile file("accountinformation.txt");
if (!file.open(QIODevice::Append | QIODevice::Text))
return 0;
QTextStream out(&file);
out << newacc << "&" << newpas << "\n";
messagebox.setText("保存成功。");
messagebox.exec();
this->ui.newaccount->clear();
this->ui.newpassward->clear();
this->ui.confirmaccount->clear();
this->ui.confirmpassward->clear();
}
else
{
messagebox.setText("此账号已被注册。");
messagebox.exec();
this->ui.newaccount->clear();
this->ui.newpassward->clear();
this->ui.confirmaccount->clear();
this->ui.confirmpassward->clear();
}
}
return 0;
}
//返回
int newuser::_return()
{
this->close();
return 0;
}
menu.h
//class addstudent;
#pragma once
#include
#include
#include "ui_menu.h"
#include "addstudent.h"
#include "viewstudent.h"
#include "modifystudent.h"
#include "deletestudent.h"
class menu : public QDialog
{
Q_OBJECT
public:
menu(QWidget *parent = Q_NULLPTR);
~menu();
private slots:
int _addStudent();//添加学生
int _viewStudent();//查看学生
void _modifyStudent();//修改学生信息
void _deleteStudent();//删除学生信息
private:
//Ui::menu ui;
Ui::Dialog ui;
addstudent as;
viewstudent vs;
modifystudent ms;
deletestudent ds;
};
menu.cpp
#include "menu.h"
menu::menu(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
//背景设置
QPixmap pixmap("F:\\vs2017\\Logininterface\\Logininterface\\image\\menubackground.jpg");
QPalette palette;
palette.setBrush(backgroundRole(), QBrush(pixmap));
setPalette(palette);
//信号和曹链接
connect(this->ui.addstudent, SIGNAL(clicked(bool)), this, SLOT(_addStudent()));
connect(this->ui.viewstudent, SIGNAL(clicked(bool)), this, SLOT(_viewStudent()));
connect(this->ui.modifystudent, SIGNAL(clicked(bool)), this, SLOT(_modifyStudent()));
connect(this->ui.deletestudent, SIGNAL(clicked(bool)), this, SLOT(_deleteStudent()));
}
menu::~menu()
{
}
//实现
//添加学生
int menu::_addStudent()
{
this->close();
as.show();
as.exec();
this->show();
return 0;
}
//查看学生信息
int menu::_viewStudent()
{
this->hide();
vs.show();
vs.exec();
this->show();
return 0;
}
//修改学生信息
void menu::_modifyStudent()
{
this->hide();
ms.show();
ms.exec();
this->show();
}
//删除学生信息
void menu::_deleteStudent()
{
this->close();
ds.show();
ds.exec();
this->show();
}
addstudent.h
#pragma once
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif
#include
#include
#include
#include
#include
#include "ui_addstudent.h"
class addstudent : public QDialog
{
Q_OBJECT
public:
addstudent(QWidget *parent = Q_NULLPTR);
~addstudent();
private slots:
int _saveInformation();//保存学生信息
int _return();//返回
private:
Ui::addstudent ui;
//menu mu;
};
addstudent.cpp
#include "addstudent.h"
addstudent::addstudent(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
QPixmap pixmap("F:\\vs2017\\Logininterface\\Logininterface\\image\\addstudentbackground.jpg");
QPalette palette;
palette.setBrush(backgroundRole(), QBrush(pixmap));
setPalette(palette);
//信号和槽链接
connect(this->ui.ok, SIGNAL(clicked(bool)), this, SLOT(_saveInformation()));
connect(this->ui.returnup, SIGNAL(clicked(bool)), this, SLOT(_return()));
}
addstudent::~addstudent()
{
}
//实现
//保存学生信息
int addstudent::_saveInformation()
{
int limited = 0, _limited = 0;
QString name, sex, age, tel, college, address, hobby=NULL;
QMessageBox messagebox;
messagebox.setWindowTitle("确认信息");
//名字
if (!this->ui.name->text().isEmpty())
{
name = this->ui.name->text();
limited++;
}
else if(this->ui.name->text().isEmpty())
{
messagebox.setText("名字为空!!");
messagebox.exec();
}
//性别
if (!this->ui.boy->isChecked() && !this->ui.girl->isChecked())
{
messagebox.setText("没有选择性别!!");
messagebox.exec();
}
else if (this->ui.boy->isChecked() || this->ui.girl->isChecked())
{
if (this->ui.boy->isChecked())
{
limited++;
sex = this->ui.boy->text();
}
else
{
sex = this->ui.girl->text();
limited++;
}
}
//年龄
age = this->ui.age->currentText();
limited++;
//电话
if (!this->ui.tel->text().isEmpty())
{
tel = this->ui.tel->text();
limited++;
}
else
{
messagebox.setText("电话为空!!");
messagebox.exec();
}
//院系
college = this->ui.college->currentText();
limited++;
//籍贯
//QString adddress=this->ui.whe
if (!this->ui.address->text().isEmpty())
{
address = this->ui.address->text();
limited++;
}
else
{
messagebox.setText("籍贯为空!!");
messagebox.exec();
}
//兴趣爱好
if (!this->ui.sing->isChecked() && !this->ui.dance->isChecked() && !this->ui.basketball->isChecked() && !this->ui.rap->isChecked())
{
messagebox.setText("兴趣爱好没有勾选!!");
messagebox.exec();
}
if (this->ui.sing->isChecked())
{
_limited++;
hobby = this->ui.sing->text() + hobby+" ";
}
if (this->ui.dance->isChecked())
{
_limited++;
hobby = this->ui.dance->text() + hobby + " ";
}
if (this->ui.basketball->isChecked())
{
_limited++;
hobby = this->ui.basketball->text() + hobby + " ";
}
if (this->ui.rap->isChecked())
{
_limited++;
hobby = this->ui.rap->text() + hobby + " ";
}
QString information;
if (limited == 6&&_limited > 0)
{
information = name + "\n" + sex + "\n" + age + "\n" + tel + "\n" + college + "\n" + address + "\n" + hobby + "\n";
messagebox.setText(information);
messagebox.exec();
QFile file("studentinformation.text");
if (!file.open(QIODevice::Append | QIODevice::Text))
{
return 0;
}
QTextStream out(&file);
out << name << "+" << sex << "+" << age << "+" << tel << "+" << college << "+" << address << "+" << hobby << "\n";
messagebox.setText("保存成功!!");
messagebox.exec();
}
return 0;
}
//返回
int addstudent::_return()
{
this->ui.name->clear();
this->ui.address->clear();
this->ui.tel->clear();
this->close();
//mu.show();
return 0;
}
viewstudent.h
#pragma once
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif
#include
#include
#include
#include
#include
#include
#include "ui_viewstudent.h"
class viewstudent : public QDialog
{
Q_OBJECT
public:
viewstudent(QWidget *parent = Q_NULLPTR);
~viewstudent();
private slots:
void _return();//返回
void _viewStudent();//查看学生信息
private:
Ui::viewstudent ui;
};
viewstudent.cpp
#include "viewstudent.h"
viewstudent::viewstudent(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
//背景设置
QPixmap pixmap("F:\\vs2017\\Logininterface\\Logininterface\\image\\viewbackground.jpg");
QPalette palette;
palette.setBrush(backgroundRole(), QBrush(pixmap));
setPalette(palette);
connect(this->ui.returnup, SIGNAL(clicked(bool)), this, SLOT(_return()));
connect(this->ui.ok, SIGNAL(clicked(bool)), this, SLOT(_viewStudent()));
}
viewstudent::~viewstudent()
{
}
//实现
//返回
void viewstudent::_return()
{
this->ui.inputname->clear();
this->close();
}
//查看学生信息
void viewstudent::_viewStudent()
{
int value = 0;
QString data = NULL;
QString name = this->ui.inputname->text();
QMessageBox mes;
mes.setWindowTitle("提示?");
if (!this->ui.onestudent->isChecked() && !this->ui.allstudent->isChecked())
{
mes.setText("请先选择查询模式。");
mes.exec();
}
else if (!this->ui.inputname->text().isEmpty() && this->ui.allstudent->isChecked())
{
mes.setText("请确认查询模式是否正确。");
mes.exec();
}
else if (this->ui.onestudent->isChecked() && this->ui.inputname->text().isEmpty())
{
mes.setText("姓名不能为空。");
mes.exec();
}
else if (this->ui.onestudent->isChecked() && !this->ui.inputname->text().isEmpty())
{
//one
QFile file("studentinformation.text");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
QTextStream in(&file);
while (!in.atEnd())
{
data = in.readLine();
if (data.indexOf(name) > -1)
{
value++;
mes.setText(data);
mes.exec();
}
}
if (value == 0)
{
mes.setText("没有该学生的信息!");
mes.exec();
}
}
else
{
//全部
QFile file("studentinformation.text");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
QTextStream in(&file);
while (!in.atEnd())
{
data = data + in.readLine() + "\n";
}
if (data == NULL)
{
mes.setText("学生信息系统没有数据.");
mes.exec();
}
else
{
mes.setText(data);
mes.exec();
}
}
}
deletestudent.h
#pragma once
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif
#include
#include
#include
#include
#include
#include
#include
#include "ui_deletestudent.h"
class deletestudent : public QDialog
{
Q_OBJECT
public:
deletestudent(QWidget *parent = Q_NULLPTR);
~deletestudent();
private slots:
void _return();//返回
void _getDeleteStudent();//获取删除学生信息
void _deleteStudent();//删除学生信息
private:
Ui::deletestudent ui;
};
deletestudent.cpp
#include "deletestudent.h"
deletestudent::deletestudent(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
//背景设置
QPixmap pixmap("F:\\vs2017\\Logininterface\\Logininterface\\image\\deletebackground.jpg");
QPalette palette;
palette.setBrush(backgroundRole(), QBrush(pixmap));
setPalette(palette);
connect(this->ui.ok, SIGNAL(clicked(bool)), this, SLOT(_getDeleteStudent()));
connect(this->ui.returnup, SIGNAL(clicked(bool)), this, SLOT(_return()));
connect(this->ui.okdelete, SIGNAL(clicked(bool)), this, SLOT(_deleteStudent()));
}
deletestudent::~deletestudent()
{
}
//返回
void deletestudent::_return()
{
this->close();
}
//获取删除学生信息
void deletestudent::_getDeleteStudent()
{
int value = 0;
QMessageBox mes;
mes.setWindowTitle("提示?");
QString name = this->ui.inputname->text(), linedata;
QFile file("studentinformation.text");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
else
{
QTextStream in(&file);
while (!in.atEnd())
{
linedata = in.readLine();
if (name != NULL)
{
if (linedata.indexOf(name) > -1)
{
value++;
QFont font;
font.setPointSize(14);
font.setBold(true);
this->ui.deletestudentinformation->setFont(font);
this->ui.deletestudentinformation->setText(linedata);
//connect(this->ui.okdelete, SIGNAL(clicked(bool)), this, SLOT(_deleteStudent()));
}
}
}
}
if (value == 0)
{
mes.setText("没有要删除学生的信息。");
mes.exec();
this->ui.inputname->clear();
}
}
//删除学生信息
void deletestudent::_deleteStudent()
{
QMessageBox mes;
QString linedata, alldata = NULL, name = this->ui.inputname->text();
QFile file("studentinformation.text");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
else
{
QTextStream in(&file);
while (!in.atEnd())
{
linedata = in.readLine();
if (linedata.indexOf(name) == -1)
{
alldata = alldata + linedata + "\n";
}
}
//mes.setText(alldata);
//mes.exec();
}
QFile file_2("studentinformation.text");
if (!file_2.open(QIODevice::WriteOnly | QIODevice::Text))
{
return;
}
else
{
QTextStream out(&file_2);
out << alldata;
mes.setText(name + "的信息删除成功!");
mes.exec();
this->ui.inputname->clear();
this->ui.deletestudentinformation->clear();
}
file.close();
file_2.close();
}
modifystudent.h
#pragma once
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif
#include
#include
#include
#include
#include "ui_modifystudent.h"
class modifystudent : public QDialog
{
Q_OBJECT
public:
modifystudent(QWidget *parent = Q_NULLPTR);
~modifystudent();
private slots:
void _getdata();//根据输入获取学生信息
void _clear();//清除信息
void _save();//保存信息并替换
void _return();//返回
private:
Ui::modifystudent ui;
};
modifystudent.cpp
#include "modifystudent.h"
modifystudent::modifystudent(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
//设置背景
QPixmap pixmap("F:\\vs2017\\Logininterface\\Logininterface\\image\\modifybackground.jpg");
QPalette palette;
palette.setBrush(backgroundRole(), QBrush(pixmap));
setPalette(palette);
//获取学生信息
connect(this->ui.ok, SIGNAL(clicked(bool)), this, SLOT(_getdata()));
//清除信息
connect(this->ui.clear, SIGNAL(clicked(bool)), this, SLOT(_clear()));
//保存并替换文件中的数据
connect(this->ui.save, SIGNAL(clicked(bool)), this, SLOT(_save()));
//返回上一层
connect(this->ui.returnup, SIGNAL(clicked(bool)), this, SLOT(_return()));
}
modifystudent::~modifystudent()
{
}
//实现
//根据输入获取学生信息
void modifystudent::_getdata()
{
QMessageBox mes;
mes.setWindowTitle("提示?");
QString name = this->ui.inputname->text();
QString linedata;
int value = 0;
if (name == NULL)
{
mes.setText("请输入要修改学生姓名。");
mes.exec();
}
else if (name != NULL)
{
QFile file("studentinformation.text");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
return;
}
QTextStream in(&file);
while (!in.atEnd())
{
linedata = in.readLine();
if (linedata.indexOf(name) > -1)
{
value++;
//获取学生属性
//姓名
this->ui.name->setText(linedata.section("+", 0, 0));
//性别
if (this->ui.boy->text() == linedata.section("+", 1, 1))
{
this->ui.boy->setChecked(true);
}
else
{
this->ui.girl->setChecked(true);
}
//年龄
this->ui.getage->setText(linedata.section("+", 2, 2));
//电话
this->ui.tel->setText(linedata.section("+", 3, 3));
//院系
this->ui.getcollege->setText(linedata.section("+", 4, 4));
//地址
this->ui.address->setText(linedata.section("+", 5, 5));
//兴趣爱好
if (linedata.indexOf(this->ui.sing->text()) > -1)
{
this->ui.sing->setChecked(true);
}
if (linedata.indexOf(this->ui.dance->text()) > -1)
{
this->ui.dance->setChecked(true);
}
if (linedata.indexOf(this->ui.basketball->text()) > -1)
{
this->ui.basketball->setChecked(true);
}
if (linedata.indexOf(this->ui.rap->text()) > -1)
{
this->ui.rap->setChecked(true);
}
}
}
}
if (value == 0 && name != NULL)
{
mes.setText("没有要修改的学生信息!!");
mes.exec();
}
}
//清除信息
void modifystudent::_clear()
{
this->ui.name->clear();
this->ui.tel->clear();
this->ui.address->clear();
}
//保存信息并替换
void modifystudent::_save()
{
int value = 0, flag = 0;
QMessageBox mes;
QString linedata, alldata = NULL, hobby = NULL;
QFile file("studentinformation.text");
mes.setWindowTitle("提示?");
QString name = this->ui.inputname->text(), newdata = NULL;
if (name == NULL)
{
mes.setText("请先添加要修改的学生姓名。");
mes.exec();
}
else
{
//获取不用修改的学生信息
if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QTextStream in(&file);
while (!in.atEnd())
{
linedata = in.readLine();
if (linedata.indexOf(name) == -1)
{
alldata = alldata + linedata + "\n";
}
}
//mes.setText(alldata);
//mes.exec();
}
//修改信息并且保存
//name
if (!this->ui.name->text().isEmpty())
{
newdata = newdata + this->ui.name->text() + "+";
flag++;
}
else
{
mes.setText("没有填写姓名。");
}
//sex
if (!this->ui.boy->isChecked() && !this->ui.girl->isChecked())
{
mes.setText("没有勾选性别。");
mes.exec();
}
else if (this->ui.boy->isChecked())
{
flag++;
newdata = newdata + this->ui.boy->text() + "+";
}
else
{
flag++;
newdata = newdata + this->ui.girl->text() + "+";
}
//age
newdata = newdata + this->ui.age->currentText() + "+";
//tel
if (!this->ui.tel->text().isEmpty())
{
flag++;
newdata = newdata + this->ui.tel->text() + "+";
}
else
{
mes.setText("没有填写电话。");
mes.exec();
}
//college
newdata = newdata + this->ui.college->currentText() + "+";
//address
newdata = newdata + this->ui.address->text() + "+";
//hobby
if (this->ui.sing->isChecked())
{
value++;
hobby = hobby + this->ui.sing->text();
}
if (this->ui.dance->isChecked())
{
value++;
hobby = hobby + this->ui.dance->text();
}
if (this->ui.basketball->isChecked())
{
value++;
hobby = hobby + this->ui.basketball->text();
}
if (this->ui.rap->isChecked())
{
value++;
hobby = hobby + this->ui.rap->text();
}
if (value == 0)
{
mes.setText("没有选择兴趣爱好。");
mes.exec();
}
//修改后的学生信息
newdata = newdata + hobby;
//修改后的全部学生信息
alldata = alldata + newdata + "\n";
if (value > 0 && flag ==3)
{
//保存
//mes.setText(alldata);
//mes.exec();
QFile file("studentinformation.text");
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QTextStream out(&file);
out << alldata;
mes.setText("修改成功。");
mes.exec();
}
}
else
{
mes.setText("信息不完整。");
mes.exec();
}
}
file.close();
}
//返回上一层
void modifystudent::_return()
{
this->close();
}
main.cpp
#include "Logininterface.h"
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Logininterface w;
w.show();
return a.exec();
}