当前位置: 首页 > news >正文

设置网址/伊春seo

设置网址,伊春seo,物流网络结构模式有哪些,有哪些做笔译的网站一、Sql介绍 Qt Sql模块包含多个类,实现数据库的连接,Sql语句的执行,数据获取与界面显示,数据与界面直接使用Model/View结构。1、使用Sql模块 (1)工程加入 QT sql(2)添加头文件 …

一、Sql介绍

Qt Sql模块包含多个类,实现数据库的连接,Sql语句的执行,数据获取与界面显示,数据与界面直接使用Model/View结构。

1、使用Sql模块

(1)工程加入

QT += sql

(2)添加头文件

#include <QtSel>

2、Sql相关类

1、数据库相关类

  • QTableView:常用的数据库内容显示组件
  • QSalQuryModel:通过设置select语句查询获取数据库内容,数据只读。
  • QSqlTableModel:直接设置一个数据表的名称,可以获取苏韩剧表的全部记录,可以编辑。
  • QSqlRelationalTableModel:为单张的数据库表提供了一个编辑的数据模型,支持外键。

二、QSqltableModel

1、实现程序

在这里插入图片描述

(1)创建项目,基于QMainWindow

(2)添加类

在这里插入图片描述

(3)添加组件

在这里插入图片描述

(4)加载数据库

void MainWindow::openTable()
{tabModel = new QSqlTableModel(this, DB);tabModel->setTable("employee"); //设置数据表名称tabModel->setSort(tabModel->fieldIndex("empNo"), Qt::AscendingOrder); //按照员工号升序tabModel->setEditStrategy(QSqlTableModel::OnManualSubmit); //手动提交数据if(!tabModel->select()){QMessageBox::critical(this, "错误", "打开数据表错误,错误信息\n"+ tabModel->lastError().text());return;}// 修改表头tabModel->setHeaderData(tabModel->fieldIndex("empNo"), Qt::Horizontal, "工号");tabModel->setHeaderData(tabModel->fieldIndex("Name"), Qt::Horizontal, "姓名");tabModel->setHeaderData(tabModel->fieldIndex("Gender"), Qt::Horizontal, "性别");tabModel->setHeaderData(tabModel->fieldIndex("Height"), Qt::Horizontal, "身高");tabModel->setHeaderData(tabModel->fieldIndex("Birthday"), Qt::Horizontal, "出生日期");tabModel->setHeaderData(tabModel->fieldIndex("Mobile"), Qt::Horizontal, "手机号");tabModel->setHeaderData(tabModel->fieldIndex("Province"), Qt::Horizontal, "省份");tabModel->setHeaderData(tabModel->fieldIndex("City"), Qt::Horizontal, "城市");tabModel->setHeaderData(tabModel->fieldIndex("Depart"), Qt::Horizontal, "部门");tabModel->setHeaderData(tabModel->fieldIndex("Education"), Qt::Horizontal, "学历");tabModel->setHeaderData(tabModel->fieldIndex("Salary"), Qt::Horizontal, "薪资");tabModel->setHeaderData(tabModel->fieldIndex("Photo"), Qt::Horizontal, "照片");tabModel->setHeaderData(tabModel->fieldIndex("Memo"), Qt::Horizontal, "备注");theSelection = new QItemSelectionModel(tabModel);ui->tableView->setModel(tabModel);ui->tableView->setSelectionModel(theSelection);connect(theSelection, SIGNAL(currentChanged(QModelIndex, QModelIndex)),this, SLOT(on_currentChanged(QModelIndex, QModelIndex)));connect(theSelection, SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),this, SLOT(on_currentRowChanged(QModelIndex, QModelIndex)));// 隐藏列ui->tableView->setColumnHidden(tabModel->fieldIndex("Photo"), true);ui->tableView->setColumnHidden(tabModel->fieldIndex("Memo"), true);dataMapper = new QDataWidgetMapper;dataMapper->setModel(tabModel);dataMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);dataMapper->addMapping(ui->spinBoxNum, tabModel->fieldIndex("empNo"));dataMapper->addMapping(ui->lineEditName, tabModel->fieldIndex("Name"));dataMapper->addMapping(ui->comboBoxSex, tabModel->fieldIndex("Gender"));dataMapper->addMapping(ui->doubleSpinBoxHeight, tabModel->fieldIndex("Height"));dataMapper->addMapping(ui->lineEditBirthday, tabModel->fieldIndex("Birthday"));dataMapper->addMapping(ui->lineEditPhone, tabModel->fieldIndex("Mobile"));dataMapper->addMapping(ui->comboBoxProvince, tabModel->fieldIndex("Province"));dataMapper->addMapping(ui->lineEditCity, tabModel->fieldIndex("City"));dataMapper->addMapping(ui->comboBoxWork, tabModel->fieldIndex("Depart"));dataMapper->addMapping(ui->comboBoxStudy, tabModel->fieldIndex("Education"));dataMapper->addMapping(ui->textEditInfo, tabModel->fieldIndex("Memo"));getFiledNames();ui->actOpen->setEnabled(false);ui->actAppend->setEnabled(true);ui->actDelete->setEnabled(true);ui->actInsert->setEnabled(true);ui->actScan->setEnabled(true);ui->groupBoxSort->setEnabled(true);ui->groupBoxFilter->setEnabled(true);// 使用delegate实现下拉选择QStringList strList;strList << "男" << "女";bool isEditable = false;delegateSex.setItem(strList, isEditable);ui->tableView->setItemDelegateForColumn(tabModel->fieldIndex("Gender"), &delegateSex);
}void MainWindow::getFiledNames()
{QSqlRecord emptyRec = tabModel->record();for (int i = 0; i < emptyRec.count(); ++i){ui->comboBoxFields->addItem(emptyRec.fieldName(i));}
}void MainWindow::on_currentChanged(const QModelIndex &current, const QModelIndex &preivous)
{Q_UNUSED(current)Q_UNUSED(preivous)ui->actSubmit->setEnabled(tabModel->isDirty()); // 是否有数据修改ui->actRevert->setEnabled(tabModel->isDirty()); // 是否有数据修改
}void MainWindow::on_currentRowChanged(const QModelIndex &current, const QModelIndex &preivous)
{ui->actDelete->setEnabled(current.isValid());ui->actAppend->setEnabled(current.isValid());ui->actInsert->setEnabled(current.isValid());if(! current.isValid()){ui->labelPhoto->clear();return;}dataMapper->setCurrentIndex(current.row());QSqlRecord curRec = tabModel->record(current.row());if(curRec.isNull("Photo")){ui->labelPhoto->clear();}else{QByteArray data  = curRec.value("Photo").toByteArray();QPixmap pic;pic.loadFromData(data);ui->labelPhoto->setPixmap(pic.scaledToWidth(ui->labelPhoto->size().width()));}
}void MainWindow::on_actOpen_triggered()
{QString fileName = QFileDialog::getOpenFileName(this, "选择数据库文件","", "Sqlite数据库(*.db *.db3)");if(fileName.isEmpty()){return;}DB = QSqlDatabase::addDatabase("QSQLITE"); //添加数据库驱动DB.setDatabaseName(fileName); // 设置数据库名称if(!DB.open()){QMessageBox::warning(this, "错误", "打开数据库失败");return;}openTable();
}

在这里插入图片描述

(5)实现工具栏按钮功能

void MainWindow::on_actAppend_triggered()
{tabModel->insertRow(tabModel->rowCount(), QModelIndex());QModelIndex curIndex = tabModel->index(tabModel->rowCount() - 1, 1); // 插入后增加一行theSelection->clearSelection();theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);tabModel->setData(tabModel->index(curIndex.row(), 0), 2000 + tabModel->rowCount());tabModel->setData(tabModel->index(curIndex.row(), 2), "男");ui->actSubmit->setEnabled(true);ui->actRevert->setEnabled(true);
}void MainWindow::on_actInsert_triggered()
{QModelIndex curIndex = theSelection->currentIndex();tabModel->insertRow(curIndex.row(), QModelIndex());theSelection->clearSelection();theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);tabModel->setData(tabModel->index(curIndex.row(), 0), 2000 + tabModel->rowCount());tabModel->setData(tabModel->index(curIndex.row(), 2), "男");ui->actSubmit->setEnabled(true);ui->actRevert->setEnabled(true);
}void MainWindow::on_actDelete_triggered()
{QModelIndex curIndex = theSelection->currentIndex();tabModel->removeRow(curIndex.row());ui->actSubmit->setEnabled(true);ui->actRevert->setEnabled(true);
}void MainWindow::on_actSubmit_triggered()
{bool result = tabModel->submitAll();if(!result){QMessageBox::information(this, "信息", "数据提交错误,错误信息\n"+ tabModel->lastError().text());}else{ui->actSubmit->setEnabled(false);ui->actRevert->setEnabled(false);}
}void MainWindow::on_actRevert_triggered()
{tabModel->revertAll();ui->actSubmit->setEnabled(false);ui->actRevert->setEnabled(false);
}void MainWindow::on_actSetPhoto_triggered()
{QString fileName = QFileDialog::getOpenFileName(this, "选择图片", "", "照片(*.jpg *.png)");if(fileName.isEmpty()){return;}QByteArray data;QFile *file = new QFile(fileName);if(file->open(QIODevice::ReadOnly)){data = file->readAll();QModelIndex curIndex = theSelection->currentIndex();QSqlRecord curRec = tabModel->record(curIndex.row());curRec.setValue("Photo", data);tabModel->setRecord(curIndex.row(), curRec);QPixmap pic;pic.load(fileName);ui->labelPhoto->setPixmap(pic.scaledToWidth(ui->labelPhoto->width()));file->close();}delete file;
}void MainWindow::on_actClearPhoto_triggered()
{QModelIndex curIndex = theSelection->currentIndex();QSqlRecord curRec = tabModel->record(curIndex.row());curRec.setNull("Photo");tabModel->setRecord(curIndex.row(), curRec);ui->labelPhoto->clear();
}void MainWindow::on_actScan_triggered()
{if(tabModel->rowCount() != 0){for (int i = 0; i < tabModel->rowCount(); ++i){QSqlRecord aRec = tabModel->record(i);float salary = aRec.value("Salary").toFloat();salary *= 1.1;aRec.setValue("Salary", salary);tabModel->setRecord(i, aRec);}if(tabModel->submitAll()){QMessageBox::information(this, "信息", "涨工资完成");}}
}void MainWindow::on_comboBoxFields_currentIndexChanged(int index)
{if(ui->rbtnAscend->isCheckable()){tabModel->setSort(index, Qt::AscendingOrder);}else{tabModel->setSort(index, Qt::DescendingOrder);}tabModel->select(); // 重新从数据库装载
}void MainWindow::on_rbtnAscend_clicked()
{tabModel->setSort(ui->comboBoxFields->currentIndex(), Qt::AscendingOrder);tabModel->select(); // 重新从数据库装载
}void MainWindow::on_rbtnDescend_clicked()
{tabModel->setSort(ui->comboBoxFields->currentIndex(), Qt::DescendingOrder);tabModel->select(); // 重新从数据库装载
}void MainWindow::on_rbtnMan_clicked()
{tabModel->setFilter("Gender='男'");
}void MainWindow::on_rbtnWoman_clicked()
{tabModel->setFilter("Gender='女'");
}void MainWindow::on_rbtnAll_clicked()
{tabModel->setFilter("");
}

三、QSqlQueryModel

1、相关类

QAbstractTableModelQSqlQueryModel //封装了指向SELECT语句从数据库查询数据的功能QSqlTableModelQSqlRelationalTableModel

2、实现程序

在这里插入图片描述

1、创建项目,基于QMainWindow

2、添加图标资源文件,添加工具按钮

3、添加组件

在这里插入图片描述

4、实现功能

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);qryModel = new QSqlQueryModel(this);theSelection = new QItemSelectionModel(qryModel);dataMapper = new QDataWidgetMapper(this);dataMapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);dataMapper->setModel(qryModel);ui->tableView->setModel(qryModel);ui->tableView->setSelectionModel(theSelection);connect(theSelection, SIGNAL(currentRowChanged(QModelIndex, QModelIndex)),this, SLOT(on_currentRowChanged(QModelIndex, QModelIndex)));
}MainWindow::~MainWindow()
{delete ui;
}#include <QFileDialog>
#include <QMessageBox>
void MainWindow::on_actOpenDB_triggered()
{QString fileName = QFileDialog::getOpenFileName(this, "打开数据库", "","数据库文件(*.db *.db3)");if(fileName.isEmpty()){return;}DB = QSqlDatabase::addDatabase("QSQLITE");DB.setDatabaseName(fileName);if(!DB.open()){QMessageBox::warning(this, "错误", "打开数据库失败");return;}qryModel->setQuery("SELECT EmpNo,Name,Gender,Height,Birthday,Mobile,Province,City,Depart,Education,Salary FROM employee ORDER BY EMpNo", DB);if(qryModel->lastError().isValid()){QMessageBox::critical(this, "错误", "查询失败\n" + qryModel->lastError().text());return;}qryModel->setHeaderData(0, Qt::Horizontal, "工号");qryModel->setHeaderData(2, Qt::Horizontal, "性别");dataMapper->addMapping(ui->spinBoxNum, 0);dataMapper->addMapping(ui->lineEditName, 1);dataMapper->addMapping(ui->comboBoxSex, 2);dataMapper->addMapping(ui->doubleSpinBoxHeight, 3);dataMapper->addMapping(ui->lineEditBirthday, 4);dataMapper->addMapping(ui->lineEditPhone, 5);dataMapper->addMapping(ui->comboBoxProvince, 6);dataMapper->addMapping(ui->lineEditCity, 7);dataMapper->addMapping(ui->comboBoxWork, 8);dataMapper->addMapping(ui->comboBoxStudy, 9);dataMapper->addMapping(ui->textEditInfo, 10);// dataMapper->toFirst();ui->actOpenDB->setEnabled(false);}void MainWindow::on_currentRowChanged(const QModelIndex &current, const QModelIndex &previous)
{if(!current.isValid()){ui->labelPhoto->clear();return;}dataMapper->setCurrentModelIndex(current);bool first = (current.row() == 0);bool last = (current.row() == qryModel->rowCount() - 1);ui->actRecFirst->setEnabled(!first);ui->actRecPrevious->setEnabled(!first);ui->actRecNext->setEnabled(!last);ui->actRecLast->setEnabled(!last);int curRecNo = theSelection->currentIndex().row();QSqlRecord curRec = qryModel->record(curRecNo);int empNo = curRec.value("EmpNo").toInt();QSqlQuery query;query.prepare("select EmpNo,Memo,Photo from employee where EmpNo = :ID");query.bindValue(":ID", empNo); //防注入query.exec();query.first(); // 回到第一条记录if(qryModel->lastError().isValid()){QMessageBox::critical(this, "错误", "查询失败\n" + qryModel->lastError().text());return;}QVariant va = query.value("Photo");if(!va.isValid()){ui->labelPhoto->clear();}else{QPixmap pic;QByteArray data = va.toByteArray();pic.loadFromData(data);ui->labelPhoto->setPixmap(pic.scaledToWidth(ui->labelPhoto->size().width()));}QVariant va2 = query.value("Memo");ui->textEditInfo->setPlainText(va2.toString());
}void MainWindow::on_actRecFirst_triggered()
{dataMapper->toFirst();int index = dataMapper->currentIndex();QModelIndex curIndex = qryModel->index(index, 1);theSelection->clearSelection();theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);
}void MainWindow::on_actRecPrevious_triggered()
{dataMapper->toPrevious();int index = dataMapper->currentIndex();QModelIndex curIndex = qryModel->index(index, 1);theSelection->clearSelection();theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);
}void MainWindow::on_actRecNext_triggered()
{dataMapper->toNext();int index = dataMapper->currentIndex();QModelIndex curIndex = qryModel->index(index, 1);theSelection->clearSelection();theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);
}void MainWindow::on_actRecLast_triggered()
{dataMapper->toLast();int index = dataMapper->currentIndex();QModelIndex curIndex = qryModel->index(index, 1);theSelection->clearSelection();theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select);
}

四、QSqlQuery

QSqlQuery是可以执行任意SQL语句的类,如SELECT、INSERT、UPDATE、DELETE等。

1、实现程序

在这里插入图片描述

(1)创建项目,基于QMainWindow

(2)添加工具栏按钮

(3)添加对话框

在这里插入图片描述
在这里插入图片描述

#include "dialogdata.h"
#include "ui_dialogdata.h"DialogData::DialogData(QWidget *parent) :QDialog(parent),ui(new Ui::DialogData)
{ui->setupUi(this);
}DialogData::~DialogData()
{delete ui;
}void DialogData::setUpdateRecord(QSqlRecord &recData)
{mRecord = recData;ui->spinBoxEmpNo->setEnabled(false);setWindowTitle("更新记录");// 更新界面ui->spinBoxEmpNo->setValue(recData.value("EmpNo").toInt());ui->lineEditName->setText(recData.value("Name").toString());ui->comboBoxSex->setCurrentText(recData.value("Gender").toString());ui->doubleSpinBoxHeight->setValue(recData.value("Height").toFloat());ui->lineEditBirthday->setText(recData.value("Birthday").toString());ui->lineEditPhone->setText(recData.value("Mobile").toString());ui->comboBoxProvince->setCurrentText(recData.value("Province").toString());ui->lineEditCity->setText(recData.value("City").toString());ui->comboBoxDepart->setCurrentText(recData.value("Depart").toString());ui->comboBoxEducation->setCurrentText(recData.value("Education").toString());ui->spinBoxSalary->setValue(recData.value("Salary").toInt());ui->textEditInfo->setText(recData.value("Memo").toString());QVariant va = recData.value("Photo");if(!va.isValid()){ui->labelPhoto->clear();}else{QByteArray data = va.toByteArray();QPixmap pic;pic.loadFromData(data);ui->labelPhoto->setPixmap(pic.scaledToWidth(ui->labelPhoto->size().width()));}
}void DialogData::setInsertRecord(QSqlRecord &recData)
{mRecord = recData;ui->spinBoxEmpNo->setEnabled(true);setWindowTitle("新建记录");ui->spinBoxEmpNo->setValue(recData.value("EmpNo").toInt());
}QSqlRecord DialogData::getRecordData()
{mRecord.setValue("EmpNo", ui->spinBoxEmpNo->value());mRecord.setValue("Name", ui->lineEditName->text());mRecord.setValue("Gender", ui->comboBoxSex->currentText());mRecord.setValue("Height", ui->doubleSpinBoxHeight->value());mRecord.setValue("Birthday", ui->lineEditBirthday->text());mRecord.setValue("Mobile", ui->lineEditPhone->text());mRecord.setValue("Province", ui->comboBoxProvince->currentText());mRecord.setValue("City", ui->lineEditCity->text());mRecord.setValue("Depart", ui->comboBoxDepart->currentText());mRecord.setValue("Education", ui->comboBoxEducation->currentText());mRecord.setValue("Salary", ui->spinBoxSalary->value());mRecord.setValue("Memo", ui->textEditInfo->toPlainText());return mRecord;
}#include <QFileDialog>
void DialogData::on_btnLoadPhoto_clicked()
{QString fileName = QFileDialog::getOpenFileName(this, "选择图片", "","图片(*.png *.jpg)");if(fileName.isEmpty()){return;}QByteArray data;QFile *file = new QFile(fileName);file->open(QIODevice::ReadOnly);data = file->readAll();file->close();delete file;mRecord.setValue("Photo", data);QPixmap pic;pic.loadFromData(data);ui->labelPhoto->setPixmap(pic.scaledToWidth(ui->labelPhoto->size().width()));
}void DialogData::on_btnClearPhoto_clicked()
{}

(4)工具栏按钮功能

#include "mainwindow.h"
#include "ui_mainwindow.h"#include <QMessageBox>#include "dialogdata.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);setCentralWidget(ui->tableView);qryModel = new QSqlQueryModel;theSelection = new QItemSelectionModel(qryModel);ui->tableView->setModel(qryModel);ui->tableView->setSelectionModel(theSelection);connect(ui->tableView, SIGNAL(doubleClicked(QModelIndex)),this, SLOT(on_TableView_doubleClicked(QModelIndex)));
}MainWindow::~MainWindow()
{delete ui;
}void MainWindow::openTable()
{qryModel->setQuery("SELECT EmpNo,Name,Gender,Height,Birthday,Mobile,Province,City,Depart,Education,Salary From employee order by EmpNo");if(qryModel->lastError().isValid()){QMessageBox::warning(this, "错误", "SqlCmdError" + qryModel->lastError().text());return;}qryModel->setHeaderData(0, Qt::Horizontal, "工号");qryModel->setHeaderData(1, Qt::Horizontal, "姓名");qryModel->setHeaderData(2, Qt::Horizontal, "性别");qryModel->setHeaderData(3, Qt::Horizontal, "身高");qryModel->setHeaderData(4, Qt::Horizontal, "出生日期");qryModel->setHeaderData(5, Qt::Horizontal, "手机");qryModel->setHeaderData(6, Qt::Horizontal, "省份");qryModel->setHeaderData(7, Qt::Horizontal, "城市");qryModel->setHeaderData(8, Qt::Horizontal, "部门");qryModel->setHeaderData(9, Qt::Horizontal, "学历");qryModel->setHeaderData(10, Qt::Horizontal, "工资");//    ui->tableView->resizeColumnsToContents(); // 自动调整列宽//    ui->tableView->horizontalHeader()->setStretchLastSection(true); //拉伸最后一列ui->actOpenDB->setEnabled(false);ui->actRecInsert->setEnabled(true);ui->actRecDelete->setEnabled(true);ui->actRecEdit->setEnabled(true);ui->actScan->setEnabled(true);}#include <QFileDialog>void MainWindow::on_actOpenDB_triggered()
{QString fileName = QFileDialog::getOpenFileName(this, "选择数据库","", "SQlite数据库(*.db *.db3)");if(fileName.isEmpty()){return;}DB = QSqlDatabase::addDatabase("QSQLITE");DB.setDatabaseName(fileName);if(!DB.open()){QMessageBox::warning(this, "错误", "打开数据库失败");return;}openTable();}void MainWindow::on_actRecInsert_triggered()
{QSqlQuery query;query.exec("select * from employee where EmpNo = -1");DialogData *dataDlg = new DialogData;Qt::WindowFlags flags = dataDlg->windowFlags();dataDlg->setWindowFlags(flags | Qt::MSWindowsFixedSizeDialogHint); //固定大小QSqlRecord curData = query.record();curData.setValue("EmpNo", qryModel->rowCount() + 1000);dataDlg->setInsertRecord(curData);int ret = dataDlg->exec();if(ret == QDialog::Accepted){QSqlRecord recData =  dataDlg->getRecordData();query.prepare("INSERT INTO employee (EmpNo,Name,Gender,Height,Birthday,Mobile,Province,City,Depart,Education,Salary,Memo,Photo) ""VALUES(:EmpNo,:Name,:Gender,:Height,:Birthday,:Mobile,:Province,:City,:Depart,:Education,:Salary,:Memo,:Photo)");query.bindValue(":EmpNo", recData.value("EmpNo"));query.bindValue(":Name", recData.value("Name"));query.bindValue(":Gender", recData.value("Gender"));query.bindValue(":Height", recData.value("Height"));query.bindValue(":Birthday", recData.value("Birthday"));query.bindValue(":Mobile", recData.value("Mobile"));query.bindValue(":Province", recData.value("Province"));query.bindValue(":City", recData.value("City"));query.bindValue(":Depart", recData.value("Depart"));query.bindValue(":Education", recData.value("Education"));query.bindValue(":Salary", recData.value("Salary"));query.bindValue(":Memo", recData.value("Memo"));query.bindValue(":Photo", recData.value("Photo"));if(!query.exec()){QMessageBox::critical(this, "error", "Information:" + query.lastError().text());}else{qryModel->setQuery("SELECT EmpNo,Name,Gender,Height,Birthday,Mobile,Province,City,Depart,Education,Salary From employee order by EmpNo");if(qryModel->lastError().isValid()){QMessageBox::warning(this, "错误", "SqlCmdError" + qryModel->lastError().text());return;}}}delete dataDlg;
}void MainWindow::on_actRecEdit_triggered()
{int curRecNo = theSelection->currentIndex().row();QSqlRecord curRec = qryModel->record(curRecNo);int empNo = curRec.value("EmpNo").toInt();QSqlQuery query;query.prepare("select * from employee where EmpNo = :ID");query.bindValue(":ID", empNo);query.exec();query.first();if(!query.isValid()){return;}curRec = query.record();DialogData *dataDlg = new DialogData;Qt::WindowFlags flags = dataDlg->windowFlags();dataDlg->setWindowFlags(flags | Qt::MSWindowsFixedSizeDialogHint); //固定大小dataDlg->setUpdateRecord(curRec);int ret = dataDlg->exec();if(ret == QDialog::Accepted){QSqlRecord recData =  dataDlg->getRecordData();query.prepare("update employee set ""Name=:Name,Gender=:Gender,Height=:Height,Birthday=:Birthday,""Mobile=:Mobile,Province=:Province,City=:City,Depart=:Depart,""Education=:Education,Salary=:Salary,Memo=:Memo,Photo=:Photo where EmpNo=:ID");query.bindValue(":Name", recData.value("Name"));query.bindValue(":Gender", recData.value("Gender"));query.bindValue(":Height", recData.value("Height"));query.bindValue(":Birthday", recData.value("Birthday"));query.bindValue(":Mobile", recData.value("Mobile"));query.bindValue(":Province", recData.value("Province"));query.bindValue(":City", recData.value("City"));query.bindValue(":Depart", recData.value("Depart"));query.bindValue(":Education", recData.value("Education"));query.bindValue(":Salary", recData.value("Salary"));query.bindValue(":Memo", recData.value("Memo"));query.bindValue(":Photo", recData.value("Photo"));query.bindValue(":ID", recData.value("EmpNo"));if(!query.exec()){QMessageBox::critical(this, "error", "Information:" + query.lastError().text());}else{qryModel->setQuery("SELECT EmpNo,Name,Gender,Height,Birthday,Mobile,Province,City,Depart,Education,Salary From employee order by EmpNo");if(qryModel->lastError().isValid()){QMessageBox::warning(this, "错误", "SqlCmdError" + qryModel->lastError().text());return;}}}delete dataDlg;
}void MainWindow::on_actRecDelete_triggered()
{int curRecNo = theSelection->currentIndex().row();QSqlRecord curRec = qryModel->record(curRecNo);int empNo = curRec.value("EmpNo").toInt();QSqlQuery queryDelete;queryDelete.prepare("delete from employee where EmpNo=:ID");queryDelete.bindValue(":ID", empNo);if(!queryDelete.exec()){QMessageBox::warning(this, "错误", "SqlCmdError" + queryDelete.lastError().text());}else{qryModel->setQuery("SELECT EmpNo,Name,Gender,Height,Birthday,Mobile,Province,City,Depart,Education,Salary From employee order by EmpNo");if(qryModel->lastError().isValid()){QMessageBox::warning(this, "错误", "SqlCmdError" + qryModel->lastError().text());return;}}}void MainWindow::on_actScan_triggered()
{QSqlQuery queryEmList;queryEmList.exec("select EmpNo,Salary From employee order by EmpNo");queryEmList.first();QSqlQuery queryUpdate;queryUpdate.prepare("update employee set Salary=:Salary where EmpNo=:ID");while (queryEmList.isValid()){int empID = queryEmList.value("EmpNo").toInt();float salary = queryEmList.value("Salary").toFloat() + 1000;queryUpdate.bindValue(":Salary", salary);queryUpdate.bindValue(":ID", empID);queryUpdate.exec();if(queryUpdate.lastError().isValid()){break;}queryEmList.next();}qryModel->setQuery("SELECT EmpNo,Name,Gender,Height,Birthday,Mobile,Province,City,Depart,Education,Salary From employee order by EmpNo");if(qryModel->lastError().isValid()){QMessageBox::warning(this, "错误", "SqlCmdError" + qryModel->lastError().text());return;}
}void MainWindow::on_TableView_doubleClicked(QModelIndex index)
{Q_UNUSED(index)on_actRecEdit_triggered();
}

在这里插入图片描述

五、QSelRelationalTableModel

QSelRelationalTableModel类为单张的数据库提供了一个可以编辑的数据模型,它支持外键。
QAbstractTableModelQSqlQueryModel //封装了指向SELECT语句从数据库查询数据的功能QSqlTableModelQSqlRelationalTableModel

1、实现程序

在这里插入图片描述

(1)创建项目,基于QMainWindow

在这里插入图片描述

(2)添加图标资源

(3)实现工具栏功能

#include "mainwindow.h"
#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) :QMainWindow(parent),ui(new Ui::MainWindow)
{ui->setupUi(this);setCentralWidget(ui->tableView);}MainWindow::~MainWindow()
{delete ui;
}#include <QFileDialog>
#include <QMessageBox>
void MainWindow::on_actOpenDB_triggered()
{QString fileName = QFileDialog::getOpenFileName(this, "打开数据库", "","Sqlite数据库(*.db *.db3)");if(fileName.isEmpty()){return;}DB = QSqlDatabase::addDatabase("QSQLITE");DB.setDatabaseName(fileName);if(!DB.open()){QMessageBox::warning(this, "错误", "打开数据库失败");}tabModel = new QSqlRelationalTableModel(this, DB);tabModel->setTable("studInfo");tabModel->setEditStrategy(QSqlTableModel::OnManualSubmit);tabModel->setSort(0, Qt::AscendingOrder);tabModel->setHeaderData(0, Qt::Horizontal, "学号");tabModel->setHeaderData(1, Qt::Horizontal, "姓名");tabModel->setHeaderData(2, Qt::Horizontal, "性别");tabModel->setHeaderData(3, Qt::Horizontal, "学院");tabModel->setHeaderData(4, Qt::Horizontal, "专业");tabModel->setRelation(3, QSqlRelation("departments", "departID", "department"));tabModel->setRelation(4, QSqlRelation("majors", "majorID", "major"));theSelection = new QItemSelectionModel(tabModel);ui->tableView->setModel(tabModel);ui->tableView->setSelectionModel(theSelection);// 表格添加代理(学院与专业下拉选)ui->tableView->setItemDelegate(new QSqlRelationalDelegate(ui->tableView));tabModel->select();ui->actOpenDB->setEnabled(false);ui->actRecAppend->setEnabled(true);ui->actRecInsert->setEnabled(true);ui->actRecDelete->setEnabled(true);ui->actFields->setEnabled(true);}void MainWindow::on_actFields_triggered()
{QSqlRecord emptyRec = tabModel->record();QString str;for (int var = 0; var < emptyRec.count(); ++var){str = str + emptyRec.fieldName(var) + "\n";}QMessageBox::information(this, "字段名称", str);
}void MainWindow::on_actRecAppend_triggered()
{tabModel->insertRow(tabModel->rowCount(), QModelIndex()); // 在末尾添加一条QModelIndex curIndex = tabModel->index(tabModel->rowCount() - 1, 1); //theSelection->clearSelection(); // 清空选择项theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select); // 设置新的选项ui->actRevert->setEnabled(true);ui->actSubmit->setEnabled(true);
}void MainWindow::on_actRecInsert_triggered()
{QModelIndex curIndex = ui->tableView->currentIndex();tabModel->insertRow(curIndex.row(), QModelIndex()); // 添加一条theSelection->clearSelection(); // 清空选择项theSelection->setCurrentIndex(curIndex, QItemSelectionModel::Select); // 设置新的选项ui->actRevert->setEnabled(true);ui->actSubmit->setEnabled(true);
}void MainWindow::on_actRevert_triggered()
{tabModel->revertAll();ui->actSubmit->setEnabled(false);ui->actRevert->setEnabled(false);
}void MainWindow::on_actSubmit_triggered()
{bool res = tabModel->submitAll();if(!res){QMessageBox::information(this, "信息", "数据保存错误\n" + tabModel->lastError().text(),QMessageBox::Ok, QMessageBox::NoButton);}else{ui->actSubmit->setEnabled(false);ui->actRevert->setEnabled(false);}
}void MainWindow::on_actRecDelete_triggered()
{QModelIndex curIndex = ui->tableView->currentIndex();QModelIndex nextIndex = tabModel->index(curIndex.row() + 1, 0);tabModel->removeRow(curIndex.row()); // 删除theSelection->clearSelection(); // 清空选择项theSelection->setCurrentIndex(nextIndex, QItemSelectionModel::Select); // 设置新的选项ui->actRevert->setEnabled(true);ui->actSubmit->setEnabled(true);
}

在这里插入图片描述

相关文章:

十一、Qt数据库操作

一、Sql介绍 Qt Sql模块包含多个类&#xff0c;实现数据库的连接&#xff0c;Sql语句的执行&#xff0c;数据获取与界面显示&#xff0c;数据与界面直接使用Model/View结构。1、使用Sql模块 &#xff08;1&#xff09;工程加入 QT sql&#xff08;2&#xff09;添加头文件 …...

【Spring】IoC容器 控制反转 与 DI依赖注入 XML实现版本 第二期

文章目录 基于 XML 配置方式组件管理前置 准备项目一、 组件&#xff08;Bean&#xff09;信息声明配置&#xff08;IoC&#xff09;&#xff1a;1.1 基于无参构造1.2 基于静态 工厂方法实例化1.3 基于非静态 工厂方法实例化 二、 组件&#xff08;Bean&#xff09;依赖注入配置…...

神经网络系列---感知机(Neuron)

文章目录 感知机(Neuron)感知机(Neuron)的决策函数可以表示为&#xff1a;感知机(Neuron)的学习算法主要包括以下步骤&#xff1a;感知机可以实现逻辑运算中的AND、OR、NOT和异或(XOR)运算。 感知机(Neuron) 感知机(Neuron)是一种简单而有效的二分类算法&#xff0c;用于将输入…...

k8s(2)

目录 一.二进制部署k8s 常见的K8S安装部署方式&#xff1a; k8s部署 二进制与高可用的区别 二.部署k8s 初始化操作&#xff1a; 每台node安装docker&#xff1a; 在 master01 节点上操作; 准备cfssl证书生成工具:&#xff1a; 执行脚本文件&#xff1a; 拉入etcd压缩包…...

利用nginx内部访问特性实现静态资源授权访问

在nginx中&#xff0c;将静态资源设为internal&#xff1b;然后将前端的静态资源地址改为指向后端&#xff0c;在后端的响应头部中写上静态资源地址。 近期客户对我们项目做安全性测评&#xff0c;暴露出一些安全性问题&#xff0c;其中一个是有些静态页面&#xff08;*.html&…...

fly-barrage 前端弹幕库(1):项目介绍

fly-barrage 是我写的一个前端弹幕库&#xff0c;由于经常在 Bilibili 上看视频&#xff0c;所以对网页的弹幕功能一直蛮感兴趣的&#xff0c;所以做了这个库&#xff0c;可以帮助前端快速的实现弹幕功能。 项目官网地址&#xff1a;https://fly-barrage.netlify.app/&#xff…...

jetcache如果一个主体涉及多个缓存时编辑或者删除时如何同时失效多个缓存

在实际使用过程中&#xff0c;可能会遇到这种情形&#xff1a;一个主体会有多个缓存&#xff0c;比如用户基础信息缓存、用户详情缓存&#xff0c;那么当删除用户信息后就需要同时失效多个缓存中该主体数据&#xff0c;那么jetcache支持这种应用场景么&#xff0c;答案是支持&a…...

uni-app 实现拍照后给照片加水印功能

遇到个需求需要实现&#xff0c;研究了一下后写了个demo 本质上就是把拍完照后的照片放到canvas里&#xff0c;然后加上水印样式然后再重新生成一张图片 代码如下&#xff0c;看注释即可~使用的话记得还是得优化下代码 <template><view class"content"&g…...

【ArcGIS】利用DEM进行水文分析:流向/流量等

利用DEM进行水文分析 ArcGIS实例参考 水文分析通过建立地表水文模型&#xff0c;研究与地表水流相关的各种自然现象&#xff0c;在城市和区域规划、农业及森林、交通道路等许多领域具有广泛的应用。 ArcGIS实例 某流域30m分辨率DEM如下&#xff1a; &#xff08;1&#xff09…...

论文阅读笔记——PathAFL:Path-Coverage Assisted Fuzzing

文章目录 前言PathAFL&#xff1a;Path-Coverage Assisted Fuzzing1、解决的问题和目标2、技术路线2.1、如何识别 h − p a t h h-path h−path&#xff1f;2.2、如何减少 h − p a t h h-path h−path的数量&#xff1f;2.3、哪些h-path将被添加到种子队列&#xff1f;2.4、种…...

C语言中各种运算符用法

C语言中有许多不同的运算符&#xff0c;用于执行各种不同的操作。 以下是C语言中常见的运算符及其用法&#xff1a; 算术运算符&#xff1a; 加法运算符&#xff08;&#xff09;&#xff1a;用于将两个值相加。减法运算符&#xff08;-&#xff09;&#xff1a;用于将一个值减…...

pythonJax小记(五):python: 使用Jax深度图像(正交投影和透视投影之间的转换)(持续更新,评论区可以补充)

python: 使用Jax深度图像&#xff08;正交投影和透视投影之间的转换&#xff09; 前言问题描述1. 透视投影2. 正交投影 直接上代码解释1. compute_projection_parameters 函数a. 参数解释b. 函数计算 2. ortho_to_persp 函数a. 计算投影参数&#xff1a;b. 生成像素坐标网格&am…...

web安全学习笔记【16】——信息打点(6)

信息打点-语言框架&开发组件&FastJson&Shiro&Log4j&SpringBoot等[1] #知识点&#xff1a; 1、业务资产-应用类型分类 2、Web单域名获取-接口查询 3、Web子域名获取-解析枚举 4、Web架构资产-平台指纹识别 ------------------------------------ 1、开源-C…...

145.二叉树的后序遍历

// 定义一个名为Solution的类&#xff0c;用于解决二叉树的后序遍历问题 class Solution { // 定义一个公共方法&#xff0c;输入是一个二叉树的根节点&#xff0c;返回一个包含后序遍历结果的整数列表 public List<Integer> postorderTraversal(TreeNode root) { /…...

ssh远程连接免密码访问

我们在远程登录的时候&#xff0c;经常需要输入密码&#xff0c;密码往往比较复杂&#xff0c;输入比较耗费时间&#xff0c;这种情况下可以使用ssh免密码登录。 一般的教程是需要生成ssh密钥后&#xff0c;然后把密钥复制到server端完成配置&#xff0c;这里提供一个简单的方…...

Vue-Json-Schema-Form: 如何基于模板定制前端页面

本人从事的是工业物联网, 面对工业设备的通讯难题是各大设备都有各自的通讯协议, 如果想要用一款硬件去和所有设备做通讯的话, 就得面对怎么把自己想要采集的配置下发给自己的采集器的问题, 以前都是采用各种模型去尝试构建配置项, 但是因为配置可能会有深层次嵌套, 而且…...

保存Json对象到数据库

文章目录 背景实现方式1. 直接以 Json 对象保存到数据库2. 以 String 类型保存到数据库 背景 项目过程中可能需要保存 Json 对象到数据库中。 实现方式 有两种实现方式&#xff0c;一种是直接保存 Json 对象到数据库&#xff0c;这种方式在创建实体类以及编写 Mapper XML 脚本…...

《Docker 简易速速上手小册》第3章 Dockerfile 与镜像构建(2024 最新版)

文章目录 3.1 编写 Dockerfile3.1.1 重点基础知识3.1.2 重点案例&#xff1a;创建简单 Python 应用的 Docker 镜像3.1.3 拓展案例 1&#xff1a;Dockerfile 优化3.1.4 拓展案例 2&#xff1a;多阶段构建 3.2 构建流程深入解析3.2.1 重点基础知识3.2.2 重点案例&#xff1a;构建…...

【Python笔记-设计模式】适配器模式

一、说明 适配器模式是一种结构型模式&#xff0c;它使接口不兼容的对象能够相互合作 (一) 解决问题 主要解决接口不兼容问题 (二) 使用场景 当系统需要使用现有的类&#xff0c;但类的接口不符合需求时当需要一个统一的输出接口&#xff0c;但输入类型不可预知时当需要创…...

二分算法(c++版)

二分的本质是什么&#xff1f; 很多人会认为单调性是二分的本质&#xff0c;但其实其本质并非单调性&#xff0c;只是说&#xff0c;有单调性的可以进行二分&#xff0c;但是有些题目没有单调性我们也可以进行二分。其本质其实是一个边界问题&#xff0c;给定一个条件&#xf…...

【C#】用于基于 UV DLP 的 3D 打印机的切片软件源码解析(一)DLP原理 GUI

0. 原理 基于 UV DLP 的 3D 打印机的工作原理是这样的&#xff1a; UV DLP 是一种使用数字光处理&#xff08;Digital Light Processing&#xff09;技术的 3D 打印方法&#xff0c;它利用紫外光&#xff08;UV&#xff09;来固化液态树脂&#xff0c;从而形成实体物体。UV DLP…...

Javase补充-Arrays类的常用方法汇总

文章目录 一 . 排序方法二 . 查找方法三 . 判断是否相等的方法四 . 拷贝方法五 . 填充方法 一 . 排序方法 我们第一个要介绍的就是sort方法 这个排序实现的底层逻辑应该是十分复杂的,以我们目前的水平体系应该无法理解,我们今天尝试用我们可以理解的一种排序算法,插入排序来模…...

微信小程序-人脸检测-眨眼驱动ESP32蓝牙设备灯

前面2篇文章已经写了具体的人脸检测和蓝牙 这里直接结合&#xff0c;只列js 代码&#xff0c;剩下的其他代码在另外文章里面 https://blog.csdn.net/walle167/article/details/136261993 https://blog.csdn.net/walle167/article/details/136261919 上代码 import bleBehavior …...

怎么在wifi中实现手机和电脑文件互传

有时我们想手机电脑文件互传&#xff0c;数据线却不在身边&#xff0c;这时我们可以用MiXplorer来实现wifi中手机和电脑互相访问文件。 MiXplorer是一款来自著名安卓开发者论坛XDA的作品&#xff0c;免费且功能强大&#xff0c;被很多人誉为是“全能文件管理器”。 1.在手机上…...

07 STL 简介

目录 什么是STLSTL的版本STL的六大组件STL的重要性如何学习STLSTL的缺陷 1. 什么是STL c标准库的重要组成部分&#xff0c;不仅是一个可复用的组件库&#xff0c;而且是一个包罗数据结构和算法的软件框架 2. STL的版本 原始版本 Alexander Stepanov、Meng Lee在惠普实验室的…...

unity学习(39)——创建(create)角色脚本(panel)——静态(static)

1.发现一个非常实用的功能&#xff0c;点击unity中console的输出项&#xff0c;可以直接跳转到vs的代码页&#xff01; 2.static类&#xff08;变量&#xff09;有三个特点&#xff1a; &#xff08;1&#xff09;独一份&#xff08;2&#xff09;无法实例化。&#xff08;3&…...

MacOS环境下用powerline配置Terminal终端

Powerline 简介及安装配置 Powerline 是一个 stateless 状态栏&#xff0c;也就是一个全局状态/提示栏。你可以将其配置到你的 bash、Terminal、iTerm2 或 VIM 中&#xff0c;效果会如下所示&#xff1a; 你的 Mac 终端提示栏将会呈现如下图所示&#xff1a; 你的 VIM 状态…...

liunx单机项目部署

文章目录 1.liunx简介2.liunx的jdk安装2.liunx的tomcat安装3.liunx的mysql安装4.单机项目部署 1.liunx简介 Linux&#xff0c;一般指GNU/Linux&#xff08;单独的Linux内核并不可直接使用&#xff0c;一般搭配GNU套件&#xff0c;故得此称呼&#xff09;&#xff0c;是一种免费…...

SQL 中如何实现多表关联查询?

阅读本文之前请参阅----MySQL 数据库安装教程详解&#xff08;linux系统和windows系统&#xff09; 在SQL中&#xff0c;多表关联查询是通过使用JOIN操作来实现的&#xff0c;它允许你从两个或多个表中根据相关列的值来检索数据。以下是几种常见的JOIN类型&#xff1a; …...

oracle 设置权限 禁止删除用户

在Oracle中&#xff0c;可以通过修改系统角色来控制用户的操作权限。要禁止删除用户&#xff0c;需要将DROP USER这个特定的系统权限从相应的角色中移除。 下面是一种常见的方法&#xff0c;使用SQL语句进行操作&#xff1a; -- 创建新的角色&#xff0c;并为其分配所有必要的…...