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

思维导图

相关推荐
wangjing_052244 分钟前
C语言练习.if.else语句.strstr
c语言·开发语言
Tony_long74831 小时前
Python学习——字符串操作方法
开发语言·c#
SoraLuna1 小时前
「Mac玩转仓颉内测版26」基础篇6 - 字符类型详解
开发语言·算法·macos·cangjie
出逃日志2 小时前
JS的DOM操作和事件监听综合练习 (具备三种功能的轮播图案例)
开发语言·前端·javascript
前端青山2 小时前
React事件处理机制详解
开发语言·前端·javascript·react.js
black0moonlight3 小时前
ISAAC Gym 7. 使用箭头进行数据可视化
开发语言·python
时光の尘4 小时前
C语言菜鸟入门·关键字·int的用法
c语言·开发语言·数据结构·c++·单片机·链表·c
坊钰4 小时前
【Java 数据结构】时间和空间复杂度
java·开发语言·数据结构·学习·算法
Edward-tan4 小时前
c语言数据结构与算法--简单实现线性表(顺序表+链表)的插入与删除
c语言·开发语言·链表
武昌库里写JAVA4 小时前
一文读懂Redis6的--bigkeys选项源码以及redis-bigkey-online项目介绍
c语言·开发语言·数据结构·算法·二维数组