QT-day6

发布时间:2024年01月15日

作业1:数据库增删查改

#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:黑白

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

思维导图

文章来源:https://blog.csdn.net/weixin_51788918/article/details/135612452
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。