-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
188 lines (164 loc) · 5.8 KB
/
mainwindow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#define CONNECTION_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QDebug>
#include <QSqlError>
#include <QDebug>
#include <QSqlQuery>
#include <QVector>
#include <QSqlResult>
#include <QSqlTableModel>
#include <QSqlRecord>
#include <QSqlTableModel>
#include <QMessageBox>
//QSqlDatabase connectDB();
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//连接mysql
// 说明:pro文件增加QT += sql
// 复制mysql/lib下的libmysql.dll和libmysql.lib到Qt的bin目录下,由于安装的mysql是64位的,只能用64位的Qt编译器
QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL"); //ORACL的话是QODBC,需要手动编译
db.setHostName("localhost");
db.setUserName("root");
// db.setPassword("admin");
db.setPassword("123456");
db.setDatabaseName("jwxt_gzhu");
bool bRet=db.open();
if(bRet==false)
{
qDebug()<<"error open database"<<db.lastError().text();
QMessageBox::warning(this,"数据库连接失败","数据库无法连接,请联系管理员处理!");
exit(0);
}
qDebug()<<"open database success";
// QSqlQuery query= db.exec("select * from blade_blog");
// int size=query.size();
// bool b=query.first();
//获取身份选择
connect(ui->admin,&QRadioButton::clicked,this,[=](){
indentity=0;
qDebug()<<"身份选择:管理员 "; //最后中文与引号要加一个空格,否则会报错
//测试
ui->id_input->setText("admin");
ui->pwd_input->setText("admin");
});
connect(ui->teacher,&QRadioButton::clicked,this,[=](){
indentity=1;
qDebug()<<"身份选择:教师 ";
//测试
ui->id_input->setText("20002");
ui->pwd_input->setText("123456");
});
connect(ui->student,&QRadioButton::clicked,this,[=](){
indentity=2;
qDebug()<<"身份选择:学生 ";
//测试
ui->id_input->setText("1806100125");
ui->pwd_input->setText("123456");
});
//登录按钮
connect(ui->login,&QPushButton::clicked,this,[=](){
if(indentity==0){
qDebug()<<"管理员登录 ";
//获取输入的账号密码
QString id = ui->id_input->text();
QString pwd=ui->pwd_input->text();
qDebug()<<"获取到的账号:"<<id<<"密码:"<<pwd;
//查询adminb表
QSqlTableModel model;
model.setTable("admin");
model.select();
int rowCount=model.rowCount();
bool loginSuccess=false;
for(int i=0;i<rowCount;++i)
{
QSqlRecord record=model.record(i);
if(record.value(0)==id&&record.value(1)==pwd)
{
qDebug()<<"账号密码与数据库匹配成功!";
adminWin=new AdminWin(id);
// this->setVisible(false);
this->hide();
adminWin->show();
loginSuccess=true;
break;
}
}
if (loginSuccess==false)
QMessageBox::warning(this,"登录失败","账号或密码错误!请重新输入!");
}
else if(indentity==1){
qDebug()<<"教师登录 ";
//获取输入的账号密码
QString id = ui->id_input->text();
QString pwd=ui->pwd_input->text();
qDebug()<<"获取到的账号:"<<id<<"密码:"<<pwd;
//查询teacher表
QSqlTableModel model;
model.setTable("teacher");
model.select();
int rowCount=model.rowCount();
bool loginSuccess=false;
for(int i=0;i<rowCount;++i)
{
QSqlRecord record=model.record(i);
if(record.value(0)==id&&record.value(4)==pwd)
{
qDebug()<<"账号密码与数据库匹配成功!";
teacherWin=new TeacherWin(id);
this->hide();
teacherWin->show();
loginSuccess=true;
break;
}
}
if (loginSuccess==false)
QMessageBox::warning(this,"登录失败","账号或密码错误!请重新输入!");
}
else{
qDebug()<<"学生登录 ";
//获取输入的账号密码
QString id = ui->id_input->text();
QString pwd=ui->pwd_input->text();
qDebug()<<"获取到的账号:"<<id<<"密码:"<<pwd;
//查询teacher表
QSqlTableModel model;
model.setTable("student");
model.select();
int rowCount=model.rowCount();
bool loginSuccess=false;
for(int i=0;i<rowCount;++i)
{
QSqlRecord record=model.record(i);
if(record.value(0)==id&&record.value(5)==pwd)
{
qDebug()<<"账号密码与数据库匹配成功!";
studentWin=new StudentWin(id);
this->hide();
studentWin->show();
loginSuccess=true;
break;
}
}
if (loginSuccess==false)
QMessageBox::warning(this,"登录失败","账号或密码错误!请重新输入!");
}
});
// 找回密码
connect(ui->findPwd,&QPushButton::clicked,this,[=](){
qDebug()<<"找回密码";
findPwdWin=new FindPwdWin();
findPwdWin->show();
});
//设置默认选项
ui->admin->click();
// ui->student->click();
}
MainWindow::~MainWindow()
{
delete ui;
}