QT:数据库,opencv
1.数据库和Qt的UI界面完成表格的增添,删除,查看。
xxx.cpp
#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")) //如果当前对像没包含所需数据库,则添加stu.db{db=QSqlDatabase::addDatabase("QSQLITE");//设置数据库类型:Sqlite3db.setDatabaseName("stu.db");//设置数据库名字}if(!db.open()){QMessageBox::information(this,"提示","数据库打开失败");return;}//在数据库中创建数据表QSqlQuery query; //实例化对象,作为数据库语句的执行者QString sql="create table if not exists STU(id int,name char,sex char,score double);";//数据库执行语句//执行sql语句if(!query.exec(sql)){QMessageBox::information(this,"提示","数据库创建失败");}
}Widget::~Widget()
{delete ui;
}//添加信息功能槽函数
void Widget::on_addButton_clicked()
{int ui_id=ui->idEdit->text().toUInt(); //获取信息,文本转换成int类型QString ui_name=ui->nameEdit->text().toUtf8();QString ui_sex=ui->sexEdit->text().toUtf8();double ui_score=ui->scoreEdit->text().toDouble();//获取信息,文本转换成double类型if(ui_id==0 || ui_name.isEmpty() || ui_sex.isEmpty() || ui_score==0)//输入栏中是否为空{QMessageBox::information(this,"提示","请将信息补充完整");return ;}//实例化执行对象QSqlQuery query;//命令语句,采用匿名对象其中value中占位,QString sql=QString("insert into STU(id,name,sex,score) values(%1,'%2','%3',%4);").arg(ui_id).arg(ui_name).arg(ui_sex).arg(ui_score);if(!query.exec(sql)) //执行添加命令{QMessageBox::information(this,"提示","添加信息失败");return ;}else{QMessageBox::information(this,"提示","添加信息成功");}
}//显示按钮槽函数功能
void Widget::on_showButton_clicked()
{//实例化执行对象QSqlQuery query;QString sql="select * from STU;";//选择STU所有对象if(!query.exec(sql)) //执行命令{QMessageBox::information(this,"提示","查询失败");return;}//将数据库中数据显示在表格int i=0;//next()读取查询的下一条结果,返回bool类型while (query.next()) {//获取当前记录,record;QSqlRecord record=query.record();for(int j=0;j<record.count();j++) //遍历当前记录所有字段{//该循环的value(i),表示i行j列//两种写法,都需要在堆区申请空间//ui->tableWidget->setItem(i,j,new QTableWidgetItem(record.value(j).toString()));QTableWidgetItem *item=new QTableWidgetItem(record.value(j).toString());//读取信息ui->tableWidget->setItem(i,j,item);//显示与table界面}i++;}}void Widget::on_delButton_clicked()
{int ui_id=ui->idEdit->text().toUInt();QString ui_name=ui->nameEdit->text();QString ui_sex=ui->sexEdit->text();double ui_score=ui->scoreEdit->text().toDouble();QSqlQuery query;QString sql=QString("delete from STU where name='%1';").arg(ui_name);//删除命令,根据姓名删除if(!query.exec(sql)){QMessageBox::information(this,"提示","数据删除失败");}else{QMessageBox::information(this,"提示","数据删除成功");}for (int row = ui->tableWidget->rowCount() - 1; row >= 0; --row) {QTableWidgetItem *item = ui->tableWidget->item(row, 1);if (item && item->text() == ui_name) {ui->tableWidget->removeRow(row);}}}void Widget::on_sortButton_clicked()
{QSqlQuery query;QString sql="SELECT * FROM STU ORDER BY score DESC;";//对成绩进行降序排序if(!query.exec(sql)) //执行命令{QMessageBox::information(this,"提示","排序失败");return;}//将数据库中数据显示在表格int i=0;//next()读取查询的下一条结果,返回bool类型while (query.next()) {//获取当前记录,record;QSqlRecord record=query.record();for(int j=0;j<record.count();j++) //遍历当前记录所有字段{//该循环的value(i),表示i行j列//两种写法,都需要在堆区申请空间//ui->tableWidget->setItem(i,j,new QTableWidgetItem(record.value(j).toString()));QTableWidgetItem *item=new QTableWidgetItem(record.value(j).toString());//读取信息ui->tableWidget->setItem(i,j,item);//显示与table界面}i++;}}
2.采用openCV中的级联分类器完成人脸检测
#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(0)){QMessageBox ::information(this,"提示","视频文件打开失败");return;}//实例化对象Mat pic; //BGR图像容器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(pic)) //循环显示图片形成视频{//flip翻转//cv::flip(pic,pic,1);cv::cvtColor(pic,gray,CV_BGR2GRAY);//转灰度图像cv::equalizeHist(gray,dest);//直方图均衡//使用级联分类器获取人脸c.detectMultiScale(dest,faces);//将矩形框绘制到图像for(uint i=0;i<faces.size();i++){cv::rectangle(pic,faces[i],Scalar(0,0,255),2);}//imshow("gray",gray);imshow("src",pic);//开新窗口显示//imshow("dest",dest);if(waitKey(15)==27) //延时,形成视频{break;}}}Widget::~Widget()
{delete ui;
}