数据库查询案例
头文件
#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlRecord>
#include <QSqlError>
#include <QInputDialog>QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private slots:void on_addBtn_clicked();void on_showBtn_clicked();private:Ui::Widget *ui;QSqlDatabase db;
};
#endif // WIDGET_H
源文件
#include "widget.h"
#include "ui_widget.h"
#include <QMessageBox>
#include <QDebug>Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget)
{ui->setupUi(this);if(!db.contains("stu.db")){db = QSqlDatabase::addDatabase("QSQLITE");db.setDatabaseName("stu.db");}if(!db.open()){QMessageBox::information(this,"提示","数据库打开失败");return;}QSqlQuery querry;QString sql = "create table if not exists STU(id int,name char,sex char,score double);";if(!querry.exec(sql)){QMessageBox::information(this,"提示","数据表创建失败");return;}
}Widget::~Widget()
{delete ui;
}void Widget::on_addBtn_clicked()
{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();if(ui_numb==0||ui_name.isEmpty()||ui_sex.isEmpty()||ui_score==0){QMessageBox::information(this,"提示","请将信息填写完整");return;}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);QSqlQuery querry;if(!querry.exec(sql)){QMessageBox::information(this,"提示","添加信息失败");return;}else{QMessageBox::information(this,"提示","添加成功");}ui->numbEdit->clear();ui->nameEdit->clear();ui->sexEdit->clear();ui->scoreEdit->clear();
}void Widget::on_showBtn_clicked()
{QSqlQuery querry;QString sql = "select * from STU;";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++){ui->msgTable->setItem(i,j,new QTableWidgetItem(record.value(j).toString()));}i++;}
}void Widget::on_deleteBtn_clicked()
{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();if (ui_numb == 0 && ui_name.isEmpty()&&ui_sex.isEmpty()&&ui_score==0){QMessageBox::information(this, "提示", "请输入有效的ID或姓名或性别或成绩");return;}if (ui_numb != 0){QString sql = QString("DELETE FROM STU WHERE id = %1;").arg(ui_numb);QSqlQuery query;if (!query.exec(sql)){QMessageBox::information(this, "提示", "删除信息失败");}else{QMessageBox::information(this, "提示", "删除信息成功");}}if (!ui_name.isEmpty()){QString sql1 = QString("DELETE FROM STU WHERE name = '%2';").arg(ui_name);QSqlQuery query1;if (!query1.exec(sql1)){QMessageBox::information(this, "提示", "删除信息失败");}else{QMessageBox::information(this, "提示", "删除信息成功");}}if (ui_score != 0){QString sql2 = QString("DELETE FROM STU WHERE score = %3;").arg(ui_score);QSqlQuery query2;if (!query2.exec(sql2)){QMessageBox::information(this, "提示", "删除信息失败");}else{QMessageBox::information(this, "提示", "删除信息成功");}}if (!ui_sex.isEmpty()){QString sql3 = QString("DELETE FROM STU WHERE sex = '%2';").arg(ui_sex);QSqlQuery query3;if (!query3.exec(sql3)){QMessageBox::information(this, "提示", "删除信息失败");}else{QMessageBox::information(this, "提示", "删除信息成功");}}ui->numbEdit->clear();ui->nameEdit->clear();ui->sexEdit->clear();ui->scoreEdit->clear();}void Widget::on_sortBtn_clicked()
{QStringList sortColumns;sortColumns << "numb" << "name" << "sex" << "score";bool ok;QString sortColumn = QInputDialog::getItem(this, "选择排序列","请选择要排序的列:",sortColumns, 0, false, &ok);if (!ok || sortColumn.isEmpty())return;QStringList sortOrders;sortOrders << "ASC" << "DESC";QString sortOrder = QInputDialog::getItem(this, "选择排序顺序","请选择排序顺序:",sortOrders, 0, false, &ok);if (!ok || sortOrder.isEmpty())return;QString sql = QString("SELECT * FROM STU ORDER BY %1 %2;").arg(sortColumn).arg(sortOrder);QSqlQuery query;if(!query.exec(sql)){QMessageBox::critical(this, "错误", "排序查询失败: " + query.lastError().text());return;}ui->msgTable->setRowCount(0);int row = 0;while(query.next()){ui->msgTable->insertRow(row);for(int col = 0; col < query.record().count(); ++col){QTableWidgetItem *item = new QTableWidgetItem(query.value(col).toString());ui->msgTable->setItem(row, col, item);}++row;}QMessageBox::information(this, "提示", "排序完成");
}
人脸识别系统
头文件
#ifndef WIDGET_H
#define WIDGET_H#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_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACEclass Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();private:Ui::Widget *ui;
};
#endif // WIDGET_H
源文件
#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(0)){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);c.detectMultiScale(dest,faces);//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];//}//}//}for(uint 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;
}