QT-day6

作业1:数据库增删查改

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"))
    {
        db = QSqlDatabase::addDatabase("QSQLITE");

        db.setDatabaseName("stuInfo");
    }

    if (!db.open())
    {
        QMessageBox::warning(this,"","打开数据库失败");
        return;
    }

    QSqlQuery query;
    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)";

    if (query.exec(sql))
    {
        QMessageBox::information(this,"","创建数据库表成功");
    }
    else
    {
        QMessageBox::information(this,"","创建数据库表失败");
    }
}

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


void Widget::on_btn_add_clicked()
{

    int numb = ui->edit_numb->text().toUInt();
    QString name = ui->edit_name->text();
    QString sex = ui->edit_sex->text();
    int score = ui->edit_score->text().toUInt();

    if (numb == 0 || name.isEmpty() || sex.isEmpty() || score == 0)
    {
        QMessageBox::information(this,"","请填写完整");
        return;
    }

    QSqlQuery query;

    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);

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

void Widget::on_btn_show_clicked()
{
    ui->tableWidget->clear();

    QSqlQuery query;
    QString sql = "select * from stu_info_table";

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

    int i = 0;//行号
    int j = 0;//列号

    while (query.next())
    {
        for (j = 0;j < query.record().count()-1;j++)
        {
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(query.value(j+1).toString()));
        }
        i++;
    }

}

void Widget::on_btn_del_clicked()
{
    QSqlQuery query;

    int numb = ui->edit_numb->text().toUInt();

    QString sql = QString("delete from stu_info_table where numb=%1").arg(numb);

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

void Widget::on_btn_update_clicked()
{
    QSqlQuery query;

    int numb = ui->edit_numb->text().toUInt();
    QString name = ui->edit_name->text();
    QString sex = ui->edit_sex->text();
    int score = ui->edit_score->text().toUInt();

    if (numb == 0 || name.isEmpty() || sex.isEmpty() || score == 0)
    {
        QMessageBox::information(this,"","请填写完整");
        return;
    }

    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,"","修改成功");
    }
    else
    {
        QMessageBox::information(this,"","修改失败");
    }
}

作业2:黑白

cpp 复制代码
#include "widget.h"
#include <QApplication>

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;

    Mat gray;
    while (v.read(src))
    {
        //imshow("test1",src);

        cvtColor(src,gray,CV_BGR2GRAY);

        imshow("test2",gray);

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

    return a.exec();
}

思维导图

相关推荐
杨荧10 分钟前
【JAVA毕业设计】基于Vue和SpringBoot的服装商城系统学科竞赛管理系统
java·开发语言·vue.js·spring boot·spring cloud·java-ee·kafka
白子寰17 分钟前
【C++打怪之路Lv14】- “多态“篇
开发语言·c++
王俊山IT29 分钟前
C++学习笔记----10、模块、头文件及各种主题(一)---- 模块(5)
开发语言·c++·笔记·学习
为将者,自当识天晓地。31 分钟前
c++多线程
java·开发语言
小政爱学习!33 分钟前
封装axios、环境变量、api解耦、解决跨域、全局组件注入
开发语言·前端·javascript
k09331 小时前
sourceTree回滚版本到某次提交
开发语言·前端·javascript
神奇夜光杯1 小时前
Python酷库之旅-第三方库Pandas(202)
开发语言·人工智能·python·excel·pandas·标准库及第三方库·学习与成长
Themberfue1 小时前
Java多线程详解⑤(全程干货!!!)线程安全问题 || 锁 || synchronized
java·开发语言·线程·多线程·synchronized·
plmm烟酒僧1 小时前
Windows下QT调用MinGW编译的OpenCV
开发语言·windows·qt·opencv
测试界的酸菜鱼1 小时前
Python 大数据展示屏实例
大数据·开发语言·python