-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp
232 lines (195 loc) · 5.9 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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QFile>
#include <QDialog>
#include <QMessageBox>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
model = new QSqlTableModel(this);
model2 = new QSqlTableModel(this);
model3 = new QSqlTableModel(this);
model->setTable("student");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->select(); //选取整个表的所有行
skin = 0;
//不显示name属性列,如果这时添加记录,则该属性的值添加不上
// model->removeColumn(1);
// ui->tableView->setModel(model);
//使其不可编辑
//ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
model->database().transaction(); //开始事务操作
if (model->submitAll()) {
model->database().commit(); //提交
} else {
model->database().rollback(); //回滚
QMessageBox::warning(this, tr("tableModel"),
tr("数据库错误: %1")
.arg(model->lastError().text()));
}
}
void MainWindow::on_pushButton_2_clicked()
{
model->revertAll();
}
void MainWindow::on_pushButton_5_clicked()
{
QString name = ui->lineEdit->text();
//根据姓名进行筛选
model->setFilter(QString("name = '%1'").arg(name));
//显示结果
model->select();
}
void MainWindow::on_pushButton_6_clicked()
{
model->setTable("appointment"); //重新关联表
model->select();
ui->tableView->setModel(model);
//这样才能再次显示整个表的内容
}
// 升序
void MainWindow::on_pushButton_7_clicked()
{
//id属性即第0列,升序排列
model->setSort(0, Qt::AscendingOrder);
model->select();
}
// 降序
void MainWindow::on_pushButton_8_clicked()
{
model->setSort(0, Qt::DescendingOrder);
model->select();
}
void MainWindow::on_pushButton_4_clicked()
{
//获取选中的行
int curRow = ui->tableView->currentIndex().row();
//删除该行
model->removeRow(curRow);
int ok = QMessageBox::warning(this,tr("删除当前行!"),
tr("你确定删除当前行吗?"),
QMessageBox::Yes,QMessageBox::No);
if(ok == QMessageBox::No)
{
model->revertAll(); //如果不删除,则撤销
}
else model->submitAll(); //否则提交,在数据库中删除该行
}
void MainWindow::on_pushButton_3_clicked()
{
int rowNum = model->rowCount(); //获得表的行数
int id = 10;
model->insertRow(rowNum); //添加一行
model->setData(model->index(rowNum,0),id);
//model->submitAll(); //可以直接提交
}
void MainWindow::on_pushButton_9_clicked()
{
/* model2->setTable("test"); //重新关联表
model2->select();
ui->tableView_2->setModel(model2);
*/
QSqlQuery query;
sname=(ui->comboBox_2->currentIndex());
ui->tableView_2->setEditTriggers(QAbstractItemView::NoEditTriggers);
switch (sname) {
case 0:
model2->setTable("teachers"); //重新关联表
model2->select();
ui->tableView_2->setModel(model2);
break;
case 1:
model2->setTable("courses"); //重新关联表
model2->select();
ui->tableView_2->setModel(model2);
break;
case 2:
model2->setTable("rooms"); //重新关联表
model2->select();
ui->tableView_2->setModel(model2);
break;
case 3:
model2->setTable("plan"); //重新关联表
model2->select();
ui->tableView_2->setModel(model2);
break;
case 4:
model2->setTable("appointment"); //重新关联表
model2->select();
ui->tableView_2->setModel(model2);
break;
default:
break;
}
}
void MainWindow::on_pushButton_12_clicked()
{
QSqlQuery query;
query.exec("select * from view student_apt");
if(query.size())
query.exec("drop view student_apt");
QString csid = ui->lineEdit_2->text();
week=(ui->spinBox->value());
QString s;
s= QString::number(week, 10);
query.exec("create view student_apt(aptday,aptperiod,crsid,rmid) as select appointment.aptday,aptperiod,appointment.crsid,rmid from plan,appointment where aptweek = "+s+" and plan.crsid = appointment.crsid and classid = "+csid);
model3->setTable("student_apt"); //重新关联表
model3->select();
ui->tableView_3->setModel(model3);
qDebug() << "successed" << query.lastError();
}
void MainWindow::on_pushButton_13_clicked()
{
QString qss;
color=(ui->comboBox->currentIndex());
switch (color) {
case 0:
qss = ":/black.qss";
break;
case 1:
qss = ":/style.qss";
break;
case 2:
qss = ":/orange.qss";
break;
default:
break;
}
QFile file(qss);
file.open(QFile::ReadOnly);
if(file.isOpen())
{
QString btnstylesheet = QObject::tr(file.readAll());
qApp->setStyleSheet(btnstylesheet);
file.close();
}
}
void MainWindow::on_pushButton_14_clicked()
{
QSqlQuery query;
query.exec("select * from view teachers_apt");
if(query.size())
query.exec("drop view teachers_apt");
QString tcid = ui->lineEdit_3->text();
week=(ui->spinBox_2->value());
QString s;
s= QString::number(week, 10);
query.exec("create view teachers_apt(aptday,aptperiod,crsid,rmid) as select appointment.aptday,aptperiod,appointment.crsid,rmid from courses,appointment where aptweek = "+s+" and courses.crsid = appointment.crsid and tcid = "+tcid);
model3->setTable("teachers_apt"); //重新关联表
model3->select();
ui->tableView_4->setModel(model3);
qDebug() << "successed" << query.lastError();
}