【无标题】

本文介绍了如何在C++中,利用Qt库操作SQLite数据库进行数据增删改查,以及如何使用OpenCV对视频帧进行灰度转换。展示了`MyWindow`类中涉及的数据库连接、表创建、数据插入、查询和更新,以及视频处理的基本步骤。
摘要由CSDN通过智能技术生成

1. 删除和更新功能添加

代码

#include "mywindow.h"
#include "ui_mywindow.h"

MyWindow::MyWindow(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::MyWindow)
{
    ui->setupUi(this);


    if(! db.contains("stuInfo.db"))
    {
        //database doesn't exist, create database
        //QSQLITE: database driver for sqlite3
        db = QSqlDatabase::addDatabase("QSQLITE");

        db.setDatabaseName("stuInfo");

        //open database
        if(!db.open())
        {
            QMessageBox::information(this,
                                     "info",
                                     "failed to database");
            return;
        }

        //create table
        //
        QSqlQuery query;

        QString sql = " CREATE TABLE IF NOT EXISTS stu_info ( "
                      " id INTEGER PRIMARY KEY AUTOINCREMENT, "
                      " num INTEGER, "
                      " name VARCHAR(20), "
                      " gender VARCHAR(4), "
                      " score INTEGER "
                      " ) ";
        if(query.exec(sql))
        {
            QMessageBox::information(this,"Info","Table created!");
        }
        else{
            QMessageBox::information(this,"Info","Failed to create table!");
        }

    }
}

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


void MyWindow::on_btnAdd_clicked()
{
    //
    int num = ui->lineEditNo->text().toUInt();
    QString name = ui->lineEditName->text();
    QString gender = ui->lineEditGender->text();
    int score = ui->lineEditScore->text().toUInt();
    if(num == 0 || name.isEmpty() || gender.isEmpty() || score == 0)
    {
        QMessageBox::information(this, "Info", "Please fill info completed!");
    }

    QSqlQuery query;

    QString sql = QString (" INSERT INTO stu_info ("
                           "num, name, gender, score)"
                           " VALUES(%1, '%2', '%3', %4) ")
            .arg(num).arg(name).arg(gender).arg(score);
    if(query.exec(sql))
    {
        QMessageBox::information(this, "Info", "record added");
    }
    else
    {
        QMessageBox::information(this, "Info", "Failed to add record");
    }

}

void MyWindow::on_btnDisplay_clicked()
{
    QSqlQuery query;
    QString sql = "SELECT * FROM stu_info";

    if(! query.exec(sql))
    {
        QMessageBox::information(this, "Info", "failed to query!");

        return;
    }

    ui->tableWidget->clear();

    //query result is in query object
    int i = 0; //line number
    while(query.next())
    {
        for(int j=0; j < query.record().count(); j++)
        {
            ui->tableWidget->setItem(i, j, new QTableWidgetItem(query.value(j).toString()));

        }
        i++;
    }
}

void MyWindow::on_btnUpdate_clicked()
{
    int row = ui->tableWidget->currentRow();

    int id = ui->tableWidget->item(row, 0)->text().toUInt();
    int num = ui->tableWidget->item(row, 1)->text().toUInt();
    QString name = ui->tableWidget->item(row, 2)->text();
    QString gender = ui->tableWidget->item(row, 3)->text();
    int score = ui->tableWidget->item(row, 4)->text().toUInt();

    QSqlQuery query;

    QString sql = QString("UPDATE stu_info SET "
                          "num = %1, "
                          "name = '%2' , "
                          "gender = '%3', "
                          "score = %4 "
                          "WHERE id = %5 ;").arg(num).arg(name).arg(gender).arg(score).arg(id);

    qDebug()<< sql;

    if(! query.exec(sql))
    {

        QMessageBox::information(this, "Info", "failed to update!");

        return;
    }



}

void MyWindow::on_btnDelete_clicked()
{
    int row = ui->tableWidget->currentRow();
    int id = ui->tableWidget->item(row, 0)->text().toUInt();

    QSqlQuery query;

    QString sql = QString("DELETE FROM stu_info WHERE id=%1 ;").arg(id);
    if(! query.exec(sql))

    qDebug()<< sql;

    if(! query.exec(sql))
    {

        QMessageBox::information(this, "Info", "failed to delete!");

        return;
    }

    ui->tableWidget->clear();
    on_btnDisplay_clicked();



}

2. 将视频从彩色变换成灰度

代码

#include "mywidget.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MyWidget w;
    w.show();

    VideoCapture v;
    v.open("C:\\opencv\\heads\\01.mp4");

    //define a containter for image
    Mat src;


    while(v.read(src))
    {
        //show
        //imshow("test", src);
        //Grey
        Mat grey;
        //彩色图转换成灰度图
        cvtColor(src, grey, CV_BGR2GRAY );

        namedWindow("winGrey");

        imshow("winGrey", grey);

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


    }

    return a.exec();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值