您的位置:首页 > 健康 > 养生 > logo头像设计_北京冬奥会火炬设计制作_如何自己编写网站_什么是网络营销?

logo头像设计_北京冬奥会火炬设计制作_如何自己编写网站_什么是网络营销?

2024/12/23 17:06:12 来源:https://blog.csdn.net/m0_46380369/article/details/142864011  浏览:    关键词:logo头像设计_北京冬奥会火炬设计制作_如何自己编写网站_什么是网络营销?
logo头像设计_北京冬奥会火炬设计制作_如何自己编写网站_什么是网络营销?

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_NAMESPACEclass MainWindow : public QMainWindow {Q_OBJECTpublic: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_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic: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 的 QImageQImage 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();
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com