#include "class_table.h" #include "ui_class_table.h" #include #include #include #include #include #include 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()<<"选择数据失败!"<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()<removeRow(row); model->submit();//提交当前行的修改到数据库 model->submitAll(); model->select(); }