10.11 Qt

数据库查询案例

头文件

#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_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    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_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    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;
}
相关推荐
天之涯上上2 分钟前
JAVA开发 在 Spring Boot 中集成 Swagger
java·开发语言·spring boot
2402_857583493 分钟前
“协同过滤技术实战”:网上书城系统的设计与实现
java·开发语言·vue.js·科技·mfc
爱学习的白杨树10 分钟前
MyBatis的一级、二级缓存
java·开发语言·spring
OTWOL15 分钟前
两道数组有关的OJ练习题
c语言·开发语言·数据结构·c++·算法
问道飞鱼19 分钟前
【前端知识】强大的js动画组件anime.js
开发语言·前端·javascript·anime.js
拓端研究室19 分钟前
R基于贝叶斯加法回归树BART、MCMC的DLNM分布滞后非线性模型分析母婴PM2.5暴露与出生体重数据及GAM模型对比、关键窗口识别
android·开发语言·kotlin
Code成立20 分钟前
《Java核心技术I》Swing的网格包布局
java·开发语言·swing
Auc2425 分钟前
使用scrapy框架爬取微博热搜榜
开发语言·python
QQ同步助手31 分钟前
C++ 指针进阶:动态内存与复杂应用
开发语言·c++
凯子坚持 c38 分钟前
仓颉编程语言深入教程:基础概念和数据类型
开发语言·华为