首页 > 其他分享 >最新随笔

最新随笔

时间:2024-07-12 15:10:18浏览次数:11  
标签:随笔 void 最新 ui query model include MainWindow

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

相关文章

  • 最新AI一站式系统源码-ChatGPT商业版系统源码,支持自定义AI智能体应用、AI绘画、AI视频
     一、前言人工智能语言模型和AI绘画在多个领域都有广泛的应用.....SparkAi创作系统是一款基于ChatGPT和Midjourney开发的智能问答和绘画系统,提供一站式AIB/C端解决方案,涵盖AI大模型提问、AI绘画、AI视频、文档分析、图像识别和理解、TTS&语音识别、AI换脸等多项功能。......
  • 2024最新【内网隐蔽扫描,Nmap高级用法】(非常详细)零基础入门到精通,收藏这一篇就够了
    前言Nmap(NetworkMapper)是一款开源免费的网络发现和安全审计工具,主要用于扫描目标主机的开放端口、操作系统类型、启用的服务等信息。以下是Nmap的一些常见使用介绍Nmap的常见使用介绍「主机发现」:Nmap可以通过发送不同类型的探测包(如ICMPecho请求、TCPSYN包等)来检测......
  • Java 算法和数据结构 答案整理,最新面试题
    Java中如何使用动态规划求解背包问题?1、定义子问题:首先确定动态规划状态,通常以物品数量和背包容量为变量定义子问题,例如dp[i][j]表示前i件物品放入容量为j的背包所能获得的最大价值。2、确定状态转移方程:基于是否选择当前物品,将问题分为两个子问题,即dp[i][j]=......
  • 最新等保测评要求与企业应对策略
    等保(等级保护)是中国网络安全的基本制度之一,旨在根据信息系统的安全级别,实施相应的保护措施。等保2.0在2019年底正式实施,它对信息系统进行了更细致的分类,并提出了更全面的安全要求。最新的等保测评要求通常包括以下几个方面:•安全技术要求:-物理和环境安全:包括物理访问控制、......
  • MathType7.4.6.8最新免费下载,数学表达神器来袭!
    大家好啊,我是爱分享的小能手!今天要给大家安利一款神奇的工具——MathType7.4免费版本。这不仅仅是一个简单的数学公式编辑器,而是你学术写作和数学研究的强大助手,简直是数学爱好者的“瑞士军刀”!MathType最新mac官方版本下载如下:https://wm.makeding.com/iclk/?zoneid=61764......
  • 2024年五款企业加密软件(企业加密软件排行最新)
    关键是数字化时代,企业数据安全至关重要。一般来说信息技术快速发展,企业数据泄露风险上升。你知道的加密软件保护企业核心数据和商业机密,不可或缺。对我而言介绍几款企业加密软件,帮助选择适合需求的解决方案。固信加密软件https://www.gooxion.com/一.固信加密软件......
  • 2024最新,李宏毅深度学习!绝对值得反复阅读!
    介绍    李宏毅老师是台湾大学的教授,他的《机器学习》(2021年春)视频课程是深度学习领域中文视频中的经典之一。李老师风趣幽默的授课风格深受学生喜爱,他以很多动漫相关的有趣例子来解释深度学习理论,让原本晦涩难懂的内容变得轻松易懂。这门课程内容涵盖了深度学习中必须......
  • 上交2024最新-动手学大模型
    介绍  今天分享一个上海交大的免费的大模型,有相关文档和Slides,目前是2.2K星标,还是挺火的!获取:上交2024最新-《动手学大模型》实战分享!  《动手学大模型》系列编程实践,由上海交通大学2024年春季《人工智能安全技术》(NIS3353)讲义拓展而来(教师:张倬胜),旨在提供大模型相......
  • 入门的第一课-随笔记录
    Markdown学习标题一级标题:#+空格+标题名称二级标题:##+空格+标题名称三级标题:###+空格+标题名称(最多支持六级标题)字体Hello,World!字体两边各加两个*成为粗体Hello,world!字体两边各加一个*成为斜体Hello,World!斜体加粗则是两边各加三个*9.99两边加两个~则......
  • MathType7.4.6.87最新永久汉化补丁器!附带破解注册码激活码
    嘿,亲爱的的朋友们,今天我要给大家安利一个神奇的工具——MathType!如果你是一位学生、教师或者科研人员,那么你一定不能错过这个神器。它不仅能让你的数学公式变得简洁美观,还能让你在处理复杂公式时游刃有余。让我们一起来了解一下吧!MathType最新mac官方版本下载如下:https://wm......