1.QSqlQuery的增删改查
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QSqlQueryModel>
#include <QTableView>
#include <QLineEdit>
#include <QSpinBox>
#include <QPushButton>
#include <QVBoxLayout>
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void on_addButton_clicked();
void on_deleteButton_clicked();
void on_updateButton_clicked();
void on_loadButton_clicked();
private:
QLineEdit *nameLineEdit;
QLineEdit *genderLineEdit;
QSpinBox *ageSpinBox;
QSpinBox *scoreSpinBox;
QPushButton *addButton;
QPushButton *deleteButton;
QPushButton *updateButton;
QTableView *tableView;
QSqlQueryModel *model;
void loadData();
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "MainWindow.h"
#include <QSqlQuery>
#include <QSqlError>
#include <QMessageBox>
#include <QSqlError>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent) {
// 创建控件
nameLineEdit = new QLineEdit(this);
genderLineEdit = new QLineEdit(this);
ageSpinBox = new QSpinBox(this);
scoreSpinBox = new QSpinBox(this);
addButton = new QPushButton("添加", this);
deleteButton = new QPushButton("删除", this);
updateButton = new QPushButton("更新", this);
tableView = new QTableView(this);
// 设置 SpinBox 范围
ageSpinBox->setRange(0, 150);
scoreSpinBox->setRange(0, 100);
// 布局
QVBoxLayout *layout = new QVBoxLayout;
layout->addWidget(nameLineEdit);
layout->addWidget(genderLineEdit);
layout->addWidget(ageSpinBox);
layout->addWidget(scoreSpinBox);
layout->addWidget(addButton);
layout->addWidget(deleteButton);
layout->addWidget(updateButton);
layout->addWidget(tableView);
QWidget *widget = new QWidget(this);
widget->setLayout(layout);
setCentralWidget(widget);
// 连接按钮信号与槽
connect(addButton, &QPushButton::clicked, this, &MainWindow::on_addButton_clicked);
connect(deleteButton, &QPushButton::clicked, this, &MainWindow::on_deleteButton_clicked);
connect(updateButton, &QPushButton::clicked, this, &MainWindow::on_updateButton_clicked);
// 初始化模型和视图
model = new QSqlQueryModel(this);
loadData();
}
MainWindow::~MainWindow() {
}
void MainWindow::loadData() {
model->setQuery("SELECT * FROM students");
tableView->setModel(model);
}
void MainWindow::on_addButton_clicked() {
QString name = nameLineEdit->text();
QString gender = genderLineEdit->text();
int age = ageSpinBox->value();
double score = scoreSpinBox->value();
QSqlQuery query;
query.prepare("INSERT INTO students (name, gender, age, score) VALUES (?, ?, ?, ?)");
query.addBindValue(name);
query.addBindValue(gender);
query.addBindValue(age);
query.addBindValue(score);
if (!query.exec()) {
QMessageBox::critical(this, "添加失败", query.lastError().text());
} else {
loadData();
}
}
void MainWindow::on_deleteButton_clicked() {
QString name = nameLineEdit->text();
QSqlQuery query;
query.prepare("DELETE FROM students WHERE name = ?");
query.addBindValue(name);
if (!query.exec()) {
QMessageBox::critical(this, "删除失败", query.lastError().text());
} else {
loadData();
}
}
void MainWindow::on_updateButton_clicked() {
QString name = nameLineEdit->text();
QString gender = genderLineEdit->text();
int age = ageSpinBox->value();
double score = scoreSpinBox->value();
QSqlQuery query;
query.prepare("UPDATE students SET gender = ?, age = ?, score = ? WHERE name = ?");
query.addBindValue(gender);
query.addBindValue(age);
query.addBindValue(score);
query.addBindValue(name);
if (!query.exec()) {
QMessageBox::critical(this, "更新失败", query.lastError().text());
} else {
loadData();
}
}
void MainWindow::on_loadButton_clicked() {
loadData();
}
main.cpp
#include <QApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QMessageBox>
#include <QSqlError>
#include "MainWindow.h"
int main(int argc, char *argv[]) {
QApplication app(argc, argv);
// 设置数据库连接
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("students.db");
if (!db.open()) {
QMessageBox::critical(nullptr, "Database Error", db.lastError().text());
return -1;
}
// 创建表
QSqlQuery query;
query.exec("CREATE TABLE IF NOT EXISTS students (name TEXT, gender TEXT, age INTEGER, score REAL)");
MainWindow window;
window.show();
return app.exec();
}
人脸识别
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include <QTimer>
#include <opencv2/opencv.hpp>
using namespace cv;
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void processFrameAndUpdateGUI();
private:
Ui::MainWindow *ui;
VideoCapture cap; // 摄像头捕获
QTimer *timer; // 定时器用于更新图像
CascadeClassifier faceCascade; // 用于人脸检测的分类器
void detectAndDraw(Mat &frame);
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include <QMessageBox>
#include <QImage>
#include <QPixmap>
#include <QDir>
#include <qDebug>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, timer(new QTimer(this))
{
ui->setupUi(this);
// 加载 OpenCV 的人脸检测分类器
if (!faceCascade.load("haarcascade_frontalface_default.xml")) {
QMessageBox::critical(this, "Error", "Error loading haarcascade_frontalface_default.xml");
return;
}
// 打开摄像头(0代表默认摄像头)
if (!cap.open(0)) {
QMessageBox::critical(this, "Error", "Error opening video camera");
return;
}
// 设置定时器,每33毫秒(约30帧每秒)触发一次
connect(timer, &QTimer::timeout, this, &MainWindow::processFrameAndUpdateGUI);
timer->start(33);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::processFrameAndUpdateGUI()
{
Mat frame;
cap >> frame; // 从摄像头捕获当前帧
if (frame.empty()) {
return;
}
flip(frame, frame, 1); // 镜像翻转
detectAndDraw(frame); // 检测人脸并绘制红色矩形框
// 将 OpenCV 的 Mat 转换为 Qt 的 QImage
QImage qimg(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_BGR888);
// 在界面上显示视频流
ui->label->setPixmap(QPixmap::fromImage(qimg));
ui->label->resize(qimg.size());
}
void MainWindow::detectAndDraw(Mat &frame)
{
std::vector<Rect> faces;
Mat gray;
// 将彩色图像转换为灰度图像
cvtColor(frame, gray, COLOR_BGR2GRAY);
equalizeHist(gray, gray);
// 检测人脸
faceCascade.detectMultiScale(gray, faces, 1.1, 3, 0, Size(30, 30));
// 遍历检测到的所有人脸,并在每张人脸上绘制红色矩形框
for (size_t i = 0; i < faces.size(); ++i) {
rectangle(frame, faces[i], Scalar(0, 0, 255), 2); // 绘制红色矩形
}
}
3,mian.cpp
#include "MainWindow.h"
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}