QTday6作业

思维导图:

QTday6作业_第1张图片

 widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include //数据库管理类
#include //执行sql语句类
#include //数据库记录类
#include //数据库错误类
#include 

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = nullptr);
    ~Widget();

private slots:
    void on_addBtn_clicked();

    void on_showBtn_clicked();

    void on_delBtn_clicked();

    void on_dataBtn_clicked();

private:
    Ui::Widget *ui;
    QSqlDatabase db;
};

#endif // WIDGET_H

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    return a.exec();
}

widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    //判断是否有该数据库
    if(!db.contains("stuInfo.db"))
    {
        //说明数据库不存在
        db = QSqlDatabase::addDatabase("QSQLITE");

        //给刚刚创建的数据库命名
        db.setDatabaseName("stuInfo");
    }
    //打开数据库
    if(!db.open())
    {
        QMessageBox::information(this,"错误","打开数据库失败");
        return;
    }
    //创建数据库表
    //实例化一个执行sql语句的对象
    QSqlQuery query;

    //准备sql语句
    QString sql="create table if not exists stu_info_table( "
                "id integer primary key autoincrement,"
                "numb integer,"
                "name varchar(20),"
                "sex varchar(4),"
                "score integer)";
    //执行sql语句
    if(query.exec(sql))
    {
        QMessageBox::information(this,"","创建数据库表成功");
    }
    else {
        QMessageBox::information(this,"","创建数据库表失败");
    }

}

Widget::~Widget()
{
    delete ui;
}

//添加按扭对应的槽函数处理
void Widget::on_addBtn_clicked()
{
    //获取ui界面上的成员信息
    int numb = ui->stuEdit->text().toUInt();
    QString name = ui->nameEdit->text();
    QString sex = ui->sexEdit->text();
    int score = ui->socreEdit->text().toUInt();

    //判断用户是否填写完整信息
    if(numb == 0 || name.isEmpty() || sex.isEmpty() || score == 0)
    {
        QMessageBox::information(this,"","请将信息填写完整");
        return;
    }

    //实例化一个执行sql语句的对象
    QSqlQuery query;

    //准备sql语句
    QString sql = QString("insert into stu_info_table(numb,name,sex,score)"
                  "values(%1,'%2','%3',%4)").arg(numb).arg(name).arg(sex).arg(score);

    //执行sql语句
    if(query.exec(sql))
    {
        QMessageBox::information(this,"","添加成功");
    }
    else {
        QMessageBox::information(this,"","添加失败");
    }
}

//显示功能
void Widget::on_showBtn_clicked()
{
    //准备sql语句
    QSqlQuery query;
    //ui->tableWidget->clear();
    QString sql = "select * from stu_info_table";

    if(!query.exec(sql))
    {
        QMessageBox::information(this,"","查询失败");
        return;
    }
    int i=0;//记录行号
    while(query.next())
    {
        for(int j=0;jtableWidget->setItem(i,j,new QTableWidgetItem(query.value(j+1).toString()));

        }
        i++;
    }
}

//删除成员
void Widget::on_delBtn_clicked()
{
    int numb = ui->stuEdit->text().toUInt();
    QString name = ui->nameEdit->text();
    QString sex = ui->sexEdit->text();
    int score = ui->socreEdit->text().toUInt();
    //准备sql语句
    QSqlQuery query;
    //QString sql = QString("delete from stu_info_table where numb=%1 and name='%2'  and sex='%3' and score='%4'").arg(numb).arg(name).arg(sex).arg(score);
    QString sql ;
    sql = QString("delete from stu_info_table where name='%2'").arg(name);

    if(numb != 0)
    {
        sql = QString("delete from stu_info_table where numb=%1").arg(numb);
    }
    else if (!name.isEmpty())
    {
        sql = QString("delete from stu_info_table where name='%2'").arg(name);
    }
    else if (!sex.isEmpty())
    {
        sql = QString("delete from stu_info_table where sex='%3'").arg(sex);
    }
    else if (score != 0)
    {
        sql = QString("delete from stu_info_table where score='%4'").arg(score);
    }

    if(!query.exec(sql))
    {
        QMessageBox::information(this,"","删除失败");
        return;
    }
}

void Widget::on_dataBtn_clicked()
{
    int numb = ui->stuEdit->text().toUInt();
    QString name = ui->nameEdit->text();
    QString sex = ui->sexEdit->text();
    int score = ui->socreEdit->text().toUInt();
    QSqlQuery query;
    QString sql = QString("update stu_info_table set name='%1',sex='%2',score=%3 where numb=%4").arg(name).arg(sex).arg(score).arg(numb);

    if(!query.exec(sql))
    {
        QMessageBox::information(this,"","修改失败");
        return;
    }


}

widget.h

#ifndef WIDGET_H
#define WIDGET_H

#include 
#include 
#include 
#include 
#include
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include
#include
using namespace  cv;
using namespace cv::face;
using namespace std;

namespace Ui {
class Widget;
}

class Widget : public QWidget
{
    Q_OBJECT

public:
    explicit Widget(QWidget *parent = 0);
    ~Widget();

private:
    Ui::Widget *ui;
};

#endif // WIDGET_H

main.cpp

#include "widget.h"
#include 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;
    w.show();

    //将本地的视频将在到程序中来
    VideoCapture v;
    v.open("D:\\opencv\\heads\\01.mp4");

    //定义一个存放从视频里读取到的一帧图像
    Mat src;

    while(v.read(src))
    {
        //显示图片
        imshow("test1",src);

        if( 27 == waitKey(30))
        {
            break;
        }
    }
    Mat gray;
    //灰度处理
    while(v.read(src))
    {
        cvtColor(src,gray,CV_BGR2GRAY);
        //显示图片
        imshow("test2",gray);

        if( 27 == waitKey(30))
        {
            break;
        }
    }
    Mat dst;
    while(v.read(src))
    {
        cvtColor(src,gray,CV_BGR2GRAY);

        equalizeHist(gray,dst);
        //显示图片
        imshow("test3",dst);

        if( 27 == waitKey(30))
        {
            break;
        }
    }


    return a.exec();
}

 widget.cpp

#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

 

效果图:

QTday6作业_第2张图片 

你可能感兴趣的:(c++,qt,sql)