实例介绍
【实例简介】
学生管理系统
【实例截图】
1.学生成绩管理系统使用的数据库是MySql,需要调试程序请到main函数里修改数据库信息,否则运行不了。
2.MySql数据库里面应创建一个数据库命名为school,在school里面应创建两张table分别是student和teacher。下面截图是两张表所包含的内容。
【核心代码】
#include "LoginDialog.h"
#include "ui_LoginDialog.h"
#include <QVBoxLayout>
LoginDialog::LoginDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::LoginDialog)
{
ui->setupUi(this);
//创建QSqlTableModel
model=new QSqlTableModel(this);
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
//设置登录对话框的标题
this->setWindowTitle("登录");
//设置登录对话框的图标
this->setWindowIcon(QIcon(":/student/img/book.jpg"));
//设置登录对话框大小固定为399*252
this->setMaximumSize(399,252);
this->setMinimumSize(399,252);
//设置调色板,用于设置titlelabel的颜色
QPalette p;
p.setColor(QPalette::WindowText,Qt::black);
ui->titlelabel->setPalette(p);
//设置调色板,用于设置logindialog的背景
QPalette palette;
palette.setBrush(QPalette::Background,QBrush(QPixmap(":/student/img/loginbk1.jpg").scaled(this->size())));
this->setPalette(palette);
//设置三个按钮的图标
ui->exitbtn->setIcon(QIcon(":/student/img/exit.png"));
ui->registerbtn->setIcon(QIcon(":/student/img/log.png"));
ui->loginbtn->setIcon(QIcon(":/student/img/Enter.png"));
//设置groupbox,放置两个radiobutton
radiogroup=new QButtonGroup(this);
radiogroup->addButton(ui->studentradio,0);
radiogroup->addButton(ui->teacherradio,1);
//设置图片
ui->piclabel->setPixmap(QPixmap(":/student/img/log.png").scaled(ui->piclabel->size()));
//设置登录按钮不可用
ui->loginbtn->setEnabled(false);
//设置lineedit提示语句
ui->userline->setPlaceholderText("请输入用户名");
ui->passwordline->setPlaceholderText("请输入密码");
//设置passlineedit显示为密码模式
ui->passwordline->setEchoMode(QLineEdit::Password);
//连接信号与槽
connect(ui->loginbtn,SIGNAL(clicked()),this,SLOT(loginbtnSlot()));
connect(ui->registerbtn,SIGNAL(clicked()),this,SLOT(registerbtnSlot()));
connect(ui->exitbtn,SIGNAL(clicked()),this,SLOT(exitbtnSlot()));
//设置登录按钮可用
connect(ui->userline,SIGNAL(textChanged(QString)),this,SLOT(loginbtnSetSlot(QString)));
connect(ui->passwordline,SIGNAL(textChanged(QString)),this,SLOT(loginbtnSetSlot(QString)));
}
LoginDialog::~LoginDialog()
{
delete ui;
}
void LoginDialog::loginbtnSlot()
{
if(!this->judgeEmpty())
{
ui->passwordline->clear();
return;
}
//判断是否学生登录
if(radiogroup->checkedId()==0)
{
model->setTable("student");
model->select();
int i;
for(i=0;i<model->rowCount();i )
{
QSqlRecord record=model->record(i);
if(record.value(0)==ui->userline->text()&&
record.value(5)==ui->passwordline->text())
{
QString str1="登录成功";
QString str2=record.value(1).toString();
QString str3="学生";
QMessageBox::information(this,"提示",str3 str2 str1,QMessageBox::Yes);
this->clearAll();
//创建学生成绩管理窗口
student=new StudentManage;
//连接学生成绩管理窗口和登录对话框信号与槽
connect(student,SIGNAL(toLoginDialog()),this,SLOT(showNormal()));
student->show();
connect(this,SIGNAL(toStudentManage(QString,QString)),student,SLOT(comeLoginDialog(QString,QString)));
emit toStudentManage(str2,record.value(0).toString());
this->hide();
return;
}
else if(record.value(0)==ui->userline->text()&&
record.value(5)!=ui->passwordline->text())
{
QMessageBox::information(this,"提示","密码输入有误",QMessageBox::Yes);
this->clearAll();
return;
}
}
QMessageBox::warning(this,"提示","用户不存在,请注册",QMessageBox::Yes);
this->clearAll();
return;
}
else if(radiogroup->checkedId()==1)
{
model->setTable("teacher");
model->select();
int i;
for(i=0;i<model->rowCount();i )
{
QSqlRecord record=model->record(i);
if(record.value(0)==ui->userline->text()&&
record.value(2)==ui->passwordline->text())
{
QString str1="登录成功";
QString str2=record.value(1).toString();
QString str3="教师";
QMessageBox::information(this,"提示",str3 str2 str1,QMessageBox::Yes);
//创建教师成绩管理窗口
teacher=new TeacherManage;
teacher->show();
connect(this,SIGNAL(toTeacherManage(QString,QString)),teacher,SLOT(comeLoginDialog(QString,QString)));
connect(teacher,SIGNAL(toLoginDialog()),this,SLOT(showNormal()));
emit toTeacherManage(str2,record.value(0).toString());
this->clearAll();
this->hide();
return;
}
else if(record.value(0)==ui->userline->text()&&
record.value(2)!=ui->passwordline->text())
{
QMessageBox::information(this,"提示","密码输入有误",QMessageBox::Yes);
this->clearAll();
return;
}
}
QMessageBox::warning(this,"提示","用户不存在,请注册",QMessageBox::Yes);
this->clearAll();
return;
}
else
QMessageBox::warning(this,"警告","请选择登录方式",QMessageBox::Yes);
}
void LoginDialog::registerbtnSlot()
{
RegisterDialog d(this);
this->hide();
if(d.exec()==QDialog::Accepted)
{
this->showNormal();
}
this->clearAll();
}
void LoginDialog::exitbtnSlot()
{
this->close();
}
void LoginDialog::loginbtnSetSlot(QString)
{
ui->loginbtn->setEnabled(true);
}
void LoginDialog::clearAll()
{
ui->userline->clear();
ui->passwordline->clear();
}
bool LoginDialog::judgeEmpty()
{
if(ui->userline->text().isEmpty())
{
QMessageBox::warning(this,"警告","用户名不能为空");
return false;
}
if(ui->passwordline->text().isEmpty())
{
QMessageBox::warning(this,"警告","密码不能为空");
return false;
}
else
return true;
}
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论