效果展示
数据库数据
利用XML文档记录销售记录
代码展示
carmanager.cpp 文件
#include "carmanager.h"
#include "ui_carmanager.h"
#include <QMessageBox>
#include <QSqlError>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlTableModel>
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include <QSqlRecord>
#include <QDebug>
CarManager::CarManager(QWidget *parent) :QMainWindow(parent),ui(new Ui::CarManager)
{
ui->setupUi(this);
this->setMinimumSize(900, 550);
ui->toolBox->setCurrentIndex(0);
// 创建空xml
createXML();
// 连接数据库
bool bl = connectDB();
if(!bl)
{
return;
}
// 销售车辆
initSaleCar();
// 统计
initTongji();
// 新车
initNewCar();
ui->title->setText("<center><h1>车辆管理</h1></center>");
ui->stackedWidget->setCurrentIndex(0);
// 车辆管理
connect(ui->actionCar, &QAction::triggered, [=]()
{
// 车辆管理
ui->stackedWidget->setCurrentIndex(0);
ui->title->setText("<center><h1>车辆管理</h1></center>");
});
// 销售统计
connect(ui->actionSale, &QAction::triggered, [=]()
{
ui->stackedWidget->setCurrentWidget(ui->tongji);
ui->title->setText("<center><h1>销售统计</h1></center>");
});
}
CarManager::~CarManager()
{
delete ui;
}
// 连接数据库
bool CarManager::connectDB()
{
// 添加连接
m_db = QSqlDatabase::addDatabase("QMYSQL" );
// 连接设置
m_db.setHostName("localhost");
m_db.setDatabaseName("car");
m_db.setUserName("root");
m_db.setPassword("123456");
// 打开数据库
if(!m_db.open())
{
QMessageBox::critical(this, "Error", m_db.lastError().text());
return false;
}
return true;
}
void CarManager::initSaleCar()
{
QSqlQueryModel* model = new QSqlQueryModel(this);
model->setQuery("select name from factory");
// 厂家
ui->factory->setModel(model);
ui->changjia->setModel(model);
factoryChanged("请选择厂家");
void (QComboBox::* sigItemChanged)(const QString &) = &QComboBox::currentIndexChanged;
connect(ui->factory, sigItemChanged, this, &CarManager::factoryChanged); // 厂家
connect(ui->brand, sigItemChanged, this, &CarManager::brandChanged); // 品牌
void (QSpinBox::* sigValueChanged)(int) = &QSpinBox::valueChanged;
connect(ui->number, sigValueChanged, [=](int value)
{
if(value == 0)
{
ui->ok->setEnabled(false);
ui->sum->clear();
ui->sum->setEnabled(false);
}
else
{
ui->ok->setEnabled(true);
int sum = ui->price->text().toInt() * value;
ui->sum->setText(QString::number(sum));
}
});
// 确定
connect(ui->ok, &QPushButton::clicked, [=]()
{
// 准备数据
QString factory = ui->factory->currentText();
QString brand = ui->brand->currentText();
int number = ui->number->value(); // 销售数量
int last = ui->last_num->text().toInt() - number; // 卖完之后剩余的数量
QSqlQuery query;
QString sql = QString("select sell from brand where factory='%1' and name ='%2'").arg(factory).arg(brand);
query.exec(sql);
if(query.next())
{
int sell = query.value(0).toInt() + number;
// 更新数据库
QSqlDatabase::database().transaction(); // 记录更新前的状态
sql = QString("update brand set sell=%1, last=%2 where factory='%3' and name='%4'")
.arg(sell).arg(last).arg(factory).arg(brand);
bool bl = query.exec(sql);
if(bl)
{
QSqlDatabase::database().commit(); // 提交
// 销售数据写入到文件 xml
writeXMLData();
// 显示销售数据
showDailyList();
}
else
{
// 状态回滚
QSqlDatabase::database().rollback();
}
}
// 初始化界面
ui->factory->setCurrentText("请选择厂家");
});
// 取消
connect(ui->cancle, &QPushButton::clicked, [=]()
{
ui->factory->setCurrentText("请选择厂家");
});
}
void CarManager::factoryChanged(const QString &name)
{
// 初始化
ui->brand->clear();
if(name == "请选择厂家")
{
ui->price->clear();
ui->number->setValue(0);
ui->last_num->clear()