Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Timetable/mainwindow.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
167 lines (151 sloc)
5.53 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "mainwindow.h" | |
#include "ui_mainwindow.h" | |
#include <QTextCodec> | |
MainWindow::MainWindow(QWidget *parent) : | |
QMainWindow(parent), | |
ui(new Ui::MainWindow) | |
{ | |
ui->setupUi(this); | |
QSqlDatabase dbtimetable; | |
dbtimetable = QSqlDatabase::addDatabase("QSQLITE"); | |
dbtimetable.setDatabaseName("DBtimetable.db"); | |
dbtimetable.open(); | |
QSqlQuery sql_query; | |
sql_query.exec("create table timetable(name text, lecturer text, day int,lesson int,classroom text)"); | |
dbtimetable.close(); | |
} | |
MainWindow::~MainWindow() | |
{ | |
delete ui; | |
} | |
static courseInfo *tempcourse=new courseInfo; | |
void MainWindow::on_pushButton_clicked() | |
{ | |
addDialog newadd; | |
int res = newadd.exec(); | |
if (res == QDialog::Accepted) | |
{ | |
//delete newadd; | |
//qDebug()<<"You cliclk OK"; | |
//qDebug()<<newadd.addtemp.day; | |
addCourse(newadd.addtemp.name,newadd.addtemp.lecturer,newadd.addtemp.day,newadd.addtemp.lesson,newadd.addtemp.classroom); | |
QMessageBox::about(this,tr("Notice"),tr("Success")); | |
} | |
if(res==QDialog::Rejected) | |
{ | |
//After clicking cancel; | |
} | |
} | |
void MainWindow::on_pushButton_2_clicked() | |
{ | |
delDialog *newdel=new delDialog; | |
int res = newdel->exec(); | |
if (res == QDialog::Accepted) | |
{ | |
//delete newadd; | |
//qDebug()<<"You cliclk OK"; | |
QString name=newdel->delname; | |
delCourse(name); | |
QMessageBox::about(this,tr("Notice"),tr("Success")); | |
} | |
} | |
void MainWindow::on_pushButton_3_clicked() | |
{ | |
selDialog newsel; | |
QSqlDatabase dbtimetable; | |
dbtimetable = QSqlDatabase::addDatabase("QSQLITE"); | |
dbtimetable.setDatabaseName("DBtimetable.db"); | |
dbtimetable.open(); | |
QSqlQuery sql_query; | |
sql_query.exec("select * from timetable"); | |
while(sql_query.next()) | |
{ | |
courseInfo tempcourse; | |
tempcourse.name= sql_query.value(0).toString(); | |
tempcourse.lecturer = sql_query.value(1).toString(); | |
tempcourse.day= sql_query.value(2).toInt(); | |
tempcourse.lesson = sql_query.value(3).toInt(); | |
tempcourse.classroom=sql_query.value(4).toString(); | |
newsel.list_course.push_back(tempcourse); | |
qDebug()<<QString("name:%1 lecturer:%2 day:%3 lesson:%4").arg(tempcourse.name).arg(tempcourse.lecturer).arg(tempcourse.day).arg(tempcourse.lesson); | |
} | |
dbtimetable.close(); | |
newsel.sel_updata(); | |
newsel.exec(); | |
} | |
void MainWindow::on_pushButton_4_clicked() | |
{ | |
changeDialog *newchange=new changeDialog; | |
int res = newchange->exec(); | |
if (res == QDialog::Accepted) | |
{ | |
//delete newadd; | |
//qDebug()<<"You cliclk OK"; | |
changCourse(newchange->oldname,newchange->oldday,newchange->oldlesson,newchange->name,newchange->lecturer,newchange->day,newchange->lesson,newchange->classroom); | |
QMessageBox::about(this,tr("Notice"),tr("Success")); | |
} | |
} | |
void addCourse(QString name,QString lec,int day,int lesson,QString classroom) | |
{ | |
qDebug()<<"Add Course"; | |
QSqlDatabase dbtimetable; | |
dbtimetable = QSqlDatabase::addDatabase("QSQLITE"); | |
dbtimetable.setDatabaseName("DBtimetable.db"); | |
dbtimetable.open(); | |
QSqlQuery sql_query; | |
QString insert_sql = QString("INSERT INTO timetable VALUES(\"%1\", \"%2\", %3,%4,\"%5\")").arg(name).arg(lec).arg(day).arg(lesson).arg(classroom); | |
//sql_query.exec("INSERT INTO timetable VALUES(\"C语言程序设计\", \"刘老师\", 2,3)"); | |
//QString insert_sql="INSERT INTO timetable VALUES(\"C语言程序设计\", \"刘老师\", 2,3)"; | |
if(!sql_query.exec(insert_sql)) | |
{ | |
qDebug() << sql_query.lastError(); | |
} | |
else | |
{ | |
qDebug() << "inserted succeed!"; | |
} | |
dbtimetable.close(); | |
} | |
void delCourse(QString name) | |
{ | |
QSqlDatabase dbtimetable; | |
dbtimetable = QSqlDatabase::addDatabase("QSQLITE"); | |
dbtimetable.setDatabaseName("DBtimetable.db"); | |
dbtimetable.open(); | |
QSqlQuery sql_query; | |
QString del_sql = QString("delete from timetable where name = \"%1\"").arg(name); | |
sql_query.exec(del_sql); | |
dbtimetable.close(); | |
} | |
void selCourse(selDialog newsel) | |
{ | |
QSqlDatabase dbtimetable; | |
dbtimetable = QSqlDatabase::addDatabase("QSQLITE"); | |
dbtimetable.setDatabaseName("DBtimetable.db"); | |
dbtimetable.open(); | |
//newsel.list_course.exec("select * from timetable"); | |
dbtimetable.close(); | |
} | |
void changCourse(QString oldname,int oldday,int oldlesson,QString name,QString lec,int day,int lesson,QString classroom) | |
{ | |
QSqlDatabase dbtimetable; | |
// if (QSqlDatabase::contains("del_sql")) { | |
// dbtimetable = QSqlDatabase::database("del_sql"); | |
// } else { | |
// dbtimetable = QSqlDatabase::addDatabase("QSQLITE", "del_sql"); | |
dbtimetable = QSqlDatabase::addDatabase("QSQLITE"); | |
dbtimetable.setDatabaseName("DBtimetable.db"); | |
dbtimetable.open(); | |
QSqlQuery sql_query; | |
QString change_str = QString("update timetable set name = \"%1\",lecturer=\"%2\",day=%3,lesson=%4,classroom=\"%6\" where name = \"%5\" AND day=%7 AND lesson=%8") | |
.arg(name) | |
.arg(lec) | |
.arg(day) | |
.arg(lesson) | |
.arg(oldname) | |
.arg(classroom) | |
.arg(oldday) | |
.arg(oldlesson); | |
sql_query.exec(change_str); | |
dbtimetable.close(); | |
} |