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

思维导图

相关推荐
uppp»10 分钟前
深入理解 Java 反射机制:获取类信息与动态操作
java·开发语言
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
ll7788115 小时前
LeetCode每日精进:20.有效的括号
c语言·开发语言·算法·leetcode·职场和发展
Jackson@ML6 小时前
Python数据可视化简介
开发语言·python·数据可视化
赵琳琅7 小时前
Java语言的云计算
开发语言·后端·golang
lly2024067 小时前
jQuery 杂项方法
开发语言
赵琳琅7 小时前
MDX语言的安全开发
开发语言·后端·golang
开开又心心的学嵌入式7 小时前
C语言——指针进阶应用
c语言·开发语言
开开又心心的学嵌入式7 小时前
C语言——指针基础知识
c语言·开发语言
lonelyhiker7 小时前
javascript的原型链
开发语言·javascript·原型模式