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;
}
相关推荐
界面开发小八哥6 分钟前
更高效的Java 23开发,IntelliJ IDEA助力全面升级
java·开发语言·ide·intellij-idea·开发工具
qystca35 分钟前
洛谷 B3637 最长上升子序列 C语言 记忆化搜索->‘正序‘dp
c语言·开发语言·算法
薯条不要番茄酱35 分钟前
数据结构-8.Java. 七大排序算法(中篇)
java·开发语言·数据结构·后端·算法·排序算法·intellij-idea
今天吃饺子40 分钟前
2024年SCI一区最新改进优化算法——四参数自适应生长优化器,MATLAB代码免费获取...
开发语言·算法·matlab
努力进修1 小时前
“探索Java List的无限可能:从基础到高级应用“
java·开发语言·list
Ajiang28247353043 小时前
对于C++中stack和queue的认识以及priority_queue的模拟实现
开发语言·c++
幽兰的天空3 小时前
Python 中的模式匹配:深入了解 match 语句
开发语言·python
Theodore_10226 小时前
4 设计模式原则之接口隔离原则
java·开发语言·设计模式·java-ee·接口隔离原则·javaee
----云烟----8 小时前
QT中QString类的各种使用
开发语言·qt
lsx2024068 小时前
SQL SELECT 语句:基础与进阶应用
开发语言