buzzer.h
#ifndef BUZZER_H #define BUZZER_H // 引入必要的头文件 #include <stdio.h> #include <unistd.h> #include <stdlib.h> #include <fcntl.h> #include <sys/ioctl.h> // 定义常量 #define PWM_IOCTL_SET_FREQ 1 #define PWM_IOCTL_STOP 0 #define BUZZER_FREQ 1000 // 固定频率 1000 Hz // 函数声明 void open_buzzer(void); void close_buzzer(void); #endif // BUZZER_H
buzzer.cpp
#include "buzzer.h" static int fd = -1; // 将 fd 变量定义为静态,避免重定义 // 打开蜂鸣器并设置频率为 1000 Hz void open_buzzer(void) { fd = open("/dev/pwm", O_RDWR); if (fd < 0) { perror("打开 pwm_buzzer 设备"); exit(1); } // 设置蜂鸣器频率为 1000 Hz int ret = ioctl(fd, PWM_IOCTL_SET_FREQ, BUZZER_FREQ); if (ret < 0) { perror("设置蜂鸣器频率"); close(fd); exit(1); } // 发声 500 ms 后关闭蜂鸣器 usleep(500 * 1000); // 500毫秒 if (ioctl(fd, PWM_IOCTL_STOP) < 0) { perror("关闭蜂鸣器"); } close(fd); fd = -1; // 确保 fd 被重置 } // 关闭蜂鸣器 void close_buzzer(void) { if (fd >= 0) { ioctl(fd, PWM_IOCTL_STOP); close(fd); fd = -1; } }
main.cpp
#include <QApplication> #include "mainwindow.h" int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w(0); // 显式传递0 w.show(); return a.exec(); }
mainwindow.h
#ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QSqlDatabase> #include <QSqlTableModel> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); private slots: void on_addButton_clicked(); void on_editButton_clicked(); void on_deleteButton_clicked(); private: Ui::MainWindow *ui; QSqlDatabase db; QSqlTableModel *model; void setupDatabase(); void setupModel(); void stopBuzzer(); }; #endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h" #include "ui_mainwindow.h" #include <QSqlQuery> #include <QSqlError> #include <QInputDialog> #include <QMessageBox> #include <QSqlTableModel> #include "buzzer.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); setupDatabase(); setupModel(); } MainWindow::~MainWindow() { delete ui; } void MainWindow::setupDatabase() { db = QSqlDatabase::addDatabase("QSQLITE"); db.setDatabaseName("accountbook.db"); if (!db.open()) { QMessageBox::critical(this, "数据库错误", db.lastError().text()); } QSqlQuery query; query.exec("CREATE TABLE IF NOT EXISTS accounts (id INTEGER PRIMARY KEY AUTOINCREMENT, description TEXT, amount REAL)"); } void MainWindow::setupModel() { model = new QSqlTableModel(this); model->setTable("accounts"); model->select(); ui->tableView->setModel(model); // 设置description字段为选择框 ui->comboBox->addItem("美食"); ui->comboBox->addItem("游戏"); ui->comboBox->addItem("娱乐"); ui->comboBox->addItem("学习"); ui->comboBox->addItem("工作"); ui->comboBox->addItem("其他"); } void MainWindow::on_addButton_clicked() { QString description = ui->comboBox->currentText(); double amount = QInputDialog::getDouble(this, "添加条目", "金额:"); QSqlQuery query; query.prepare("INSERT INTO accounts (description, amount) VALUES (:description, :amount)"); query.bindValue(":description", description); query.bindValue(":amount", amount); if(query.exec()) { open_buzzer(); // 调用蜂鸣器 } else { QMessageBox::warning(this, "添加条目", "添加条目失败。"); } model->select(); } void MainWindow::on_editButton_clicked() { QModelIndexList selected = ui->tableView->selectionModel()->selectedRows(); if (selected.isEmpty()) { QMessageBox::warning(this, "编辑条目", "请选择要编辑的条目。"); return; } int row = selected.first().row(); QString description = model->data(model->index(row, 1)).toString(); double amount = model->data(model->index(row, 2)).toDouble(); description = ui->comboBox->currentText(); amount = QInputDialog::getDouble(this, "编辑条目", "金额:", amount); QSqlQuery query; query.prepare("UPDATE accounts SET description = :description, amount = :amount WHERE id = :id"); query.bindValue(":description", description); query.bindValue(":amount", amount); query.bindValue(":id", model->data(model->index(row, 0)).toInt()); if(query.exec()) { open_buzzer(); // 调用蜂鸣器 } else { QMessageBox::warning(this, "编辑条目", "编辑条目失败。"); } model->select(); } void MainWindow::on_deleteButton_clicked() { QModelIndexList selected = ui->tableView->selectionModel()->selectedRows(); if (selected.isEmpty()) { QMessageBox::warning(this, "删除条目", "请选择要删除的条目。"); return; } int row = selected.first().row(); int id = model->data(model->index(row, 0)).toInt(); QSqlQuery query; query.prepare("DELETE FROM accounts WHERE id = :id"); query.bindValue(":id", id); if(query.exec()) { open_buzzer(); // 调用蜂鸣器 } else { QMessageBox::warning(this, "删除条目", "删除条目失败。"); } model->select(); }
标签:随笔,void,最新,ui,query,model,include,MainWindow From: https://www.cnblogs.com/yangjialong/p/18298432