You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

109 lines
4.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include "class_table.h"
#include "ui_class_table.h"
#include <QDebug>
#include <QHeaderView>
#include <QSqlError>
#include <QApplication>
#include <QSqlRecord>
#include <QDateTime>
class_table::class_table(QWidget *parent) :
QWidget(parent),
ui(new Ui::class_table)
{
ui->setupUi(this);
sqlQueryModel = new QSqlQueryModel(this);
/* 数据库初始化 */
sqldata_init();
model = new QSqlTableModel(this, sqlDatabase);
/* 模型设置表的名字,需要与数据库的表的名字相同 */
model->setTable("employee");
/* 数据修改后立马保存到数据库 */
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->select();//执行上面语句
if (model->lastError().isValid())
{
qDebug()<<"选择数据失败!"<<endl;
}
model->setHeaderData(0, Qt::Horizontal, "星期一");
model->setHeaderData(1, Qt::Horizontal, "星期二");
model->setHeaderData(2, Qt::Horizontal, "星期三");
model->setHeaderData(3, Qt::Horizontal, "星期四");
model->setHeaderData(4, Qt::Horizontal, "星期五");
model->setHeaderData(5, Qt::Horizontal, "星期六");
model->setHeaderData(6, Qt::Horizontal, "星期日");
ui->tableView->setModel(model);//加载模型显示
/*tableView布局*/
// ui->tableView->setColumnHidden(0,125);
//ui->tableView->setColumnWidth(1, 125);
//ui->tableView->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);//自动调整列宽
//ui->tableView->setWordWrap(true);
ui->tableView->verticalHeader()->setDefaultSectionSize(100);
ui->tableView->horizontalHeader()->setDefaultSectionSize(103);
/*显示时间*/
QDateTime dateTime;
dateTime = QDateTime::currentDateTime();
//qDebug() << dateTime.toString("yyyy-MM-dd hh:mm:ss dddd");
ui->label_7->setText(dateTime.toString("yyyy-MM-dd dddd"));
floatingbutton= new FloatingButton (this);
// 将悬浮球的pressed()信号连接到槽函数onButtonPressed()上
connect(floatingbutton, &FloatingButton::pressed, this, &class_table::onButtonPressed);
}
class_table::~class_table()
{
sqlDatabase.close();
delete ui;
delete model;
delete dataWidgetMapper;
delete itemSelectionModel;
delete floatingbutton;
}
void class_table::onButtonPressed()
{
emit class_close(false);
qDebug() << "悬浮球被按下了!";
}
void class_table::sqldata_init()
{
/* 查看本机可用的数据库驱动 */
QStringList drivers = QSqlDatabase::drivers();
foreach(QString driver, drivers) {
qDebug()<<driver;
}
/* 以QSQLITE驱动方式打开或者创建数据库 */
sqlDatabase = QSqlDatabase::addDatabase("QSQLITE");
sqlDatabase.setDatabaseName("employee.db");
/* 以open的方式打开class.db数据库则会创建一个class.db */
if (!sqlDatabase.open())
qDebug()<<"连接数据库错误"<<sqlDatabase.lastError()<<endl;
else
qDebug()<<"连接数据库成功"<<endl;
QSqlQuery query(sqlDatabase);
/* 使用指令式创建表 */
query.exec("create table employee (Monday vchar(10), Tuesday vchar(10), Wednesday vchar(10),Thursday vchar(10),"
"Friday vchar(10),Saturday vchar(10),Sunday vchar(10))");
/* 以指令的方式插入数据,如果数据已经存在则不会成功不能插入 */
// query.exec(tr("insert into employee values('没课','自动控制系统-电子楼501','电气设备及plc技术-旭日楼519','没课','没课','没课','没课')"));
// query.exec(tr("insert into employee values('电气设备及plc技术-电子楼308','无课','机器视觉工业应用-电子楼501','自动控制系统-电子楼405','没课','机器视觉工业应用-电子楼405','没课')"));
// query.exec(tr("insert into employee values('没课','没课','没课','没课','没课','没课','没课')"));
// query.exec(tr("insert into employee values('没课','没课','没课','没课','没课','没课','没课')"));
// query.exec(tr("insert into employee values('没课','没课','没课','没课','没课','没课','没课')"));
// query.exec(tr("insert into employee values('没课','没课','没课','没课','没课','没课','没课')"));
}
void class_table::sqldata_delate(int row)
{
model->removeRow(row);
model->submit();//提交当前行的修改到数据库
model->submitAll();
model->select();
}