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