作业:将数据库和图像处理自己实现一遍
数据库操作
源文件 #include "widget.h"#include "ui_widget.h"#include<QMessageBox>#include<QDebug>#include <QTableWidget>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget){ui->setupUi(this);//想要添加某个数据库if(!db.contains("stu.db")){//如果当前对象中美欧包含所需数据库,则添加一个数据库db = QSqlDatabase::addDatabase("QSQLITE"); //添加一个sqlite3的数据库db.setDatabaseName("stu.db");}//打开数据库if(!db.open()){QMessageBox::information(this,"提示","数据库打开失败");return ;}//程序执行至此,表示数据库创建成功并打开了//实例化一个sql语句的执行者QSqlQuery querry;//准备sql语句QString sql = "create table if not exists STU(id int,name char,sex char,score double);";//执行sql语句if(!querry.exec(sql)){QMessageBox::information(this,"提示","数据表创建失败");return;}//程序执行至此,表示数据表创建成功了}Widget::~Widget(){delete ui;}//添加按钮对应的槽函数void Widget::on_addBtn_clicked(){//1.将ui界面上的相关信息获取下来int ui_numb = ui->idEdit->text().toUInt();QString ui_name = ui->nameEdit->text();QString ui_sex = ui->sexEdit->text();double ui_score = ui->scoreEdit->text().toDouble();//2.判断信息中是否有空的if(ui_numb == 0 || ui_name.isEmpty() || ui_sex.isEmpty() || ui_score == 0){QMessageBox::information(this,"提示","请将信息填写完整");return;}//3.准备sql语句QString sql = QString("insert into STU(id,name,sex,score) values(%1,'%2','%3',%4);").arg(ui_numb).arg(ui_name).arg(ui_sex).arg(ui_score);// qDebug() <<sql;//4.实例化sql语句执行者QSqlQuery querry;//5.执行sql语句if(!querry.exec(sql)){QMessageBox::information(this,"提示","添加信息失败");return;}else {QMessageBox::information(this,"提示","添加成功");}}//展示信息对应的槽函数void Widget::on_dispBtn_clicked(){//实例化一个sql语句执行者QSqlQuery querry;//准备sql语句QString sql = "select * from STU;";//执行sql语句if(!querry.exec(sql)){QMessageBox::information(this,"提示","查询失败");return;}int i = 0; //遍历结果集的行while(querry.next()){//获取当前记录QSqlRecord record = querry.record();//对当前记录进行操作,便利当前记录的所有字段for(int j=0;j<record.count();j++){// qDebug() <<record.value(i).tostring();//该循环中的value(i),表示的就是第i行j列的那个元素// record.value(i).toString();ui->msgtable->setItem(i,j,new QTableWidgetItem(record.value(j).toString()));}i++; //表示行数增加}}// 删除信息按钮对应的槽函数void Widget::on_deleteBtn_clicked(){// 获取要删除的学生IDint ui_numb = ui->idEdit->text().toUInt();// 检查ID是否有效if (ui_numb == 0) {QMessageBox::information(this, "提示", "请输入有效的学生ID");return;}// 准备SQL语句QString sql = QString("DELETE FROM STU WHERE id = %1;").arg(ui_numb);// 实例化SQL语句执行者QSqlQuery query;// 执行SQL语句if (!query.exec(sql)) {QMessageBox::information(this, "提示", "删除信息失败");} else {QMessageBox::information(this, "提示", "删除成功");// 更新UI显示ui->msgtable->clear();QStringList headers;headers<<"学号"<<"姓名"<<"性别"<<"成绩";ui->msgtable->setHorizontalHeaderLabels(headers);}}//void Widget::on_sortBtn_clicked()//{// // 实例化一个SQL语句执行者// QSqlQuery query;// // 准备SQL语句,按分数升序排序// QString sql = "SELECT * FROM STU ORDER BY score ASC;";// // 执行SQL语句// if (!query.exec(sql)) {// QMessageBox::information(this, "提示", "查询失败");// return;// }// // 清空表格// ui->msgtable->clearContents();// ui->msgtable->setRowCount(0);// // 检查是否有结果// if (!query.next()) {// QMessageBox::information(this, "提示", "没有记录可显示");// return; // No records to display// }// // 重新设置表格行数// do {// // 获取当前记录// QSqlRecord record = query.record();// int j;// // 对当前记录进行操作,便利当前记录的所有字段// for (j = 0; j < record.count(); j++) {// ui->msgtable->setItem(j, j, new QTableWidgetItem(record.value(j).toString()));// }// j++; // 表示行数增加// } while (query.next());//}void Widget::on_sortBtn_clicked(){// 实例化一个SQL语句执行者QSqlQuery query;// 准备SQL语句,按分数升序排序QString sql = "SELECT * FROM STU ORDER BY score ASC;";// 执行SQL语句if (!query.exec(sql)) {QMessageBox::information(this, "提示", "查询失败");return;}// 清空表格ui->msgtable->clearContents();ui->msgtable->setRowCount(0);// 统计记录数int rowCount = 0;while (query.next()) {rowCount++; // 统计记录数}// 设置表格行数ui->msgtable->setRowCount(rowCount);query.first(); // 重置查询结果到第一条记录// 遍历结果集并填充表格int i = 0; // 遍历结果集的行do {// 获取当前记录QSqlRecord record = query.record();// 对当前记录进行操作,便利当前记录的所有字段for (int j = 0; j < record.count(); j++) {ui->msgtable->setItem(i, j, new QTableWidgetItem(record.value(j).toString()));}i++; // 表示行数增加} while (query.next()); // 继续处理所有记录}main.cpp#include "widget.h"#include <QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);Widget w;w.show();return a.exec();}头文件#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include<QSqlDatabase> //数据库的管理类#include<QSqlQuery>#include<QSqlRecord>QT_BEGIN_NAMESPACEnamespace Ui { class Widget; }QT_END_NAMESPACEclass Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_addBtn_clicked();void on_dispBtn_clicked();void on_deleteBtn_clicked();void on_sortBtn_clicked();private:Ui::Widget *ui;//实例化一个数据库对象QSqlDatabase db;};#endif // WIDGET_H注意:pro文件中加入sql并保存
ui界面
图像处理
源文件
#include "widget.h"#include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget){ui->setupUi(this);//实例化 一个视频流对象VideoCapture video;if(!video.open("D:/opencv/resource/01.mp4"))//参数为零则打开自己设备 摄像头{QMessageBox::information(this,"提示","视频文件打开失败");return;}//从视频流对象中不断读取出图像Mat src; //用于存储读取出的图像Mat gray; //用于存放灰度 图Mat dest; //用于存储均衡化图像//定义级联分类器对象CascadeClassifier c;//定义存储人脸矩阵的容器vector<Rect> faces;//给级联 分类器装 载分类模型if(!c.load("D:/opencv/resource/haarcascade_frontalface_alt.xml")){QMessageBox::information(this,"提示","级联分类器加载失败");return;}while(video.read(src)){//翻转图像//翻转规则:0表示按x 轴翻转、证书表示 按y轴翻转,负数表示按xy轴都翻转cv::flip(src,src,1);//灰度处理:将三通道的彩色处理转变成单通道的灰白图cv::cvtColor(src,gray,CV_BGR2GRAY);//均衡化图像处理cv::equalizeHist(gray,dest);/* //对像素数据进行读写//atfor(int j=0;j<src.rows;j++){for(int i=0;i<src.cols;i++){//每个像素又是一个长度为3的一维数组src.at<cv::Vec3b>(i,j)[k]for(int k=0;k<3;k++){src.at<cv::Vec3b>(j,i)[k] = 255 - src.at<cv::Vec3b>(j,i)[k];}}}*///使用 级联分类器获取人脸 矩形区域c.detectMultiScale(dest,faces);//将得到的矩形框绘制到图像上for(uint i=0;i<faces.size();i++){// 调用cv提供的函数cv::rectangle(src,faces[i],Scalar(0,0,255),2);cv::rectangle(gray,faces[i],Scalar(0,0,255),2);cv::rectangle(dest,faces[i],Scalar(0,0,255),2);}// 将图像进行展示imshow("src",src);imshow("gray",gray);imshow("dest",dest);//使用 延时函数if(waitKey(50) == 27){break;}}}Widget::~Widget(){delete ui;}main.cpp#include "widget.h"#include <QApplication>int main(int argc, char *argv[]){QApplication a(argc, argv);Widget w;w.show();return a.exec();}头文件#ifndef WIDGET_H#define WIDGET_H#include <QWidget>#include <QWidget>#include <opencv2/opencv.hpp>#include <iostream>#include <math.h>#include<opencv2/face.hpp>#include <vector>#include <map>#include <QMessageBox>#include <QDebug>#include <QFile>#include <QTextStream>#include <QDateTime>#include <QTimerEvent>#include<QtSerialPort/QtSerialPort>#include<QtSerialPort/QSerialPortInfo>using namespace cv;using namespace cv::face;using namespace std;QT_BEGIN_NAMESPACEnamespace Ui { class Widget; }QT_END_NAMESPACEclass Widget : public QWidget{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;};#endif // WIDGET_H注意:PRO文件中加入以下代码并保存
INCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/includeINCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include/opencvINCLUDEPATH += D:/opencv/opencv3.4-qt-intall/install/include/opencv2LIBS += D:/opencv/opencv3.4-qt-intall/install/x86/mingw/lib/libopencv_*.a