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

思维导图

相关推荐
诗书画唱9 分钟前
【前端面试题】JavaScript 核心知识点解析(第二十二题到第六十一题)
开发语言·前端·javascript
冬天vs不冷9 分钟前
Java基础(九):Object核心类深度剖析
java·开发语言·python
TS的美梦10 分钟前
【1:1复刻R版】python版火山图函数一键出图
开发语言·python·r语言·scanpy·火山图
眠りたいです44 分钟前
Qt音频播放器项目实践:文件过滤、元数据提取与动态歌词显示实现
c++·qt·ui·音视频·媒体·qt5·mime
陈天伟教授1 小时前
(二)Python + 地球信息科学与技术 (GeoICT)=?
开发语言·python
七七&55610 小时前
2024年08月13日 Go生态洞察:Go 1.23 发布与全面深度解读
开发语言·网络·golang
java坤坤10 小时前
GoLand 项目从 0 到 1:第八天 ——GORM 命名策略陷阱与 Go 项目启动慢问题攻坚
开发语言·后端·golang
元清加油10 小时前
【Golang】:函数和包
服务器·开发语言·网络·后端·网络协议·golang
健康平安的活着10 小时前
java之 junit4单元测试Mockito的使用
java·开发语言·单元测试
DjangoJason12 小时前
C++ 仿RabbitMQ实现消息队列项目
开发语言·c++·rabbitmq