程式碼

灌篮如库里發表於2024-07-09
#include <QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[]) {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}
#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 = nullptr);
    ~MainWindow();

private slots:
    void on_addButton_clicked();
    void on_editButton_clicked();
    void on_deleteButton_clicked();

private:
    Ui::MainWindow *ui;
    QSqlDatabase db;
    QSqlTableModel *model;
    void setupModel();
    void setupDatabase();
};

#endif // MAINWINDOW_H
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QSqlQuery>
#include <QInputDialog>

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, "Database Error", 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);
}

void MainWindow::on_addButton_clicked() {
    QString description = QInputDialog::getText(this, "Add Entry", "Description:");
    double amount = QInputDialog::getDouble(this, "Add Entry", "Amount:");

    QSqlQuery query;
    query.prepare("INSERT INTO accounts (description, amount) VALUES (:description, :amount)");
    query.bindValue(":description", description);
    query.bindValue(":amount", amount);
    query.exec();

    model->select();
}

void MainWindow::on_editButton_clicked() {
    QModelIndexList selected = ui->tableView->selectionModel()->selectedRows();
    if (selected.isEmpty()) {
        QMessageBox::warning(this, "Edit Entry", "Please select an entry to edit.");
        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 = QInputDialog::getText(this, "Edit Entry", "Description:", QLineEdit::Normal, description);
    amount = QInputDialog::getDouble(this, "Edit Entry", "Amount:", 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());
    query.exec();

    model->select();
}

void MainWindow::on_deleteButton_clicked() {
    QModelIndexList selected = ui->tableView->selectionModel()->selectedRows();
    if (selected.isEmpty()) {
        QMessageBox::warning(this, "Delete Entry", "Please select an entry to delete.");
        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);
    query.exec();

    model->select();
}

相關文章