数据表
#include "widget.h"
#include "ui_widget.h"
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;
}
//程序执行至此,表示数据库创建成功并打开了
//准备创建数据表
//1、实例化一个sql语句的执行者
QSqlQuery querry;
//2、准备sql语句
QString sql = "create table if not exists STU(id int, name char, sex char ,score double);";
//3、执行sql语句
if(!querry.exec(sql))
{
QMessageBox::information(this,"提示","数据表创建失败");
return;
}
//程序执行至此,表示数据表也创建成功了
}
Widget::~Widget()
{
delete ui;
}
//添加信息按钮对应的槽函数
void Widget::on_addBtn_clicked()
{
//1、将ui界面上的相关信息获取下来
int ui_numb = ui->numbEdit->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;
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"提示","添加信息失败");
return;
}
else
{
QMessageBox::information(this,"提示","添加成功");
}
}
void Widget::on_showBtn_clicked()
{
//1、实例化一个sql语句执行者
QSqlQuery querry;
//2、准备sql语句
QString sql = "select * from STU;";
//3、执行sql语句
if(!querry.exec(sql))
{
QMessageBox::information(this,"提示","查询失败");
return;
}
//程序执行至此,表示已经完成数据库的查询,并将结果存放到querry对象中,可以通过next函数遍历每个结果集
//函数原型:bool next();
int i = 0;
while(querry.next())
{
QSqlRecord record = querry.record();
for(int j = 0;j<record.count();j++)
{
//record.value(j).toString();
//ui->msgTable->setItem(i,j,new QTableWidgetItem(record.value(j).toString()));
QTableWidgetItem *item = new QTableWidgetItem(record.value(j).toString());
ui->msgTable->setItem(i,j,item);
}
i++;
}
}
void Widget::on_deleteBtn_clicked()
{
// 1、将ui界面上的相关信息获取下来
QString ui_name = ui->nameEdit->text();
// 2、判断姓名是否为空
if(ui_name.isEmpty())
{
QMessageBox::information(this,"提示","请输入姓名以删除记录");
return;
}
// 3、准备sql语句
QString sql = QString("delete from STU where name = '%1';").arg(ui_name);
// qDebug() << sql;
QSqlQuery querry;
if(!querry.exec(sql))
{
QMessageBox::information(this,"提示","删除信息失败");
return;
}
else
{
QMessageBox::information(this,"提示","删除成功");
}
// 4、更新表格以反映删除后的数据
// 清空表格
ui->msgTable->setRowCount(0);
// 5、重新查询并显示最新数据
QString selectSql = "select * from STU;";
if(!querry.exec(selectSql))
{
QMessageBox::information(this,"提示","查询失败");
return;
}
int i = 0;
while(querry.next())
{
QSqlRecord record = querry.record();
ui->msgTable->insertRow(i); // 插入新行
for(int j = 0; j < record.count(); j++)
{
QTableWidgetItem *item = new QTableWidgetItem(record.value(j).toString());
ui->msgTable->setItem(i, j, item);
}
i++;
}
}
void Widget::on_sortBtn_clicked()
{
// 1、实例化一个sql语句执行者
QSqlQuery querry;
// 2、准备sql语句,按score排序
QString sql = "select * from STU order by score;";
// 3、执行sql语句
if(!querry.exec(sql))
{
QMessageBox::information(this,"提示","查询失败");
return;
}
// 清空表格
ui->msgTable->setRowCount(0);
// 4、遍历查询结果并显示在msgTable中
int i = 0;
while(querry.next())
{
QSqlRecord record = querry.record();
ui->msgTable->insertRow(i); // 插入新行
for(int j = 0; j < record.count(); j++)
{
QTableWidgetItem *item = new QTableWidgetItem(record.value(j).toString());
ui->msgTable->setItem(i, j, item);
}
i++;
}
}
人脸识别
#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\\resourse\\01.mp4"))
// {
// QMessageBox::information(this,"提示","视频文件打开失败");
// return;
// }
//打开本机摄像头
if(!video.open("D:\\opencv\\resourse\\01.mp4"))
{
QMessageBox::information(this,"提示","视频文件打开失败");
return;
}
Mat src;
Mat gray;
Mat dest;
CascadeClassifier c;
vector<Rect> faces;
if(!c.load("D:\\opencv\\resourse\\haarcascade_frontalface_alt.xml"))
{
QMessageBox::information(this,"提示","级联分类器加载失败");
return;
}
while(video.read(src))
{
cv::flip(src,src,1);
cv::cvtColor(src,gray,CV_BGR2GRAY);
cv::equalizeHist(gray,dest);
/*
for(int i = 0;i<src.rows;i++)
{
for(int j = 0;j<src.cols;j++)
{
for(int k = 0;k<3;k++)
{
src.at<cv::Vec3b>(i,j)[k] = 255-src.at<cv::Vec3b>(i,j)[k];
}
}
}*/
c.detectMultiScale(dest,faces);
for(int i = 0;i<faces.size();i++)
{
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(30) ==27)
{
break;
}
}
}
Widget::~Widget()
{
delete ui;
}