.pro
QT += core gui sql xml
domxml.h
#ifndef DOMXML_H
#define DOMXML_H
#include <QString>
#include <QStringList>
#include <QDomDocument>
#include <QDomElement>
class DomXML
{
public:
DomXML();
static void createXML(QString filePath); // 创建xml空文件
static void appendXML(QString filePath, QStringList list); // 追加节点
static void writeXML(QDomDocument &doc, QDomElement &root, QStringList &list); // 写子节点
static void readXML(QString filePath,
QStringList &fList,
QStringList &bList,
QStringList &pList,
QStringList &nList,
QStringList &tList
); // 读内容
};
#endif // DOMXML_H
domxml.cpp
#include "domxml.h"
#include <QFile>
#include <QDomDocument> // 文件指针
#include <QDomProcessingInstruction> // 格式头部
#include <QDomElement> // 元素
#include <QDebug>
#include <QTextStream> // 文本流
#include <QDateTime>
// #include <QXmlParseException>
#define cout qDebug() << "[" << __FILE__ << ":" << __LINE__ << "]"
DomXML::DomXML()
{
}
// 创建xml空文件
void DomXML::createXML(QString filePath)
{
QFile file(filePath); // 关联文件名字
// 如果存在就不创建
if( file.exists() )
{
cout << "文件已存在";
return;
}else
{ // 不存在才创建
// 只写方式打开文件
bool isOk = file.open(QIODevice::WriteOnly);
if(isOk)
{
// 创建xml文档对象
QDomDocument doc;
QDomProcessingInstruction ins;
ins = doc.createProcessingInstruction("xml", "version=\'1.0\' encoding=\'UTF-8\' ");
// 追加元素
doc.appendChild(ins);
// 根节点元素
QDomElement root = doc.createElement("日销售清单");
// 追加元素
doc.appendChild(root);
// 保存文件
QTextStream stream(&file); // 文本流关联文件
doc.save(stream, 4); // 4代表缩进
file.close(); // 关闭文件
}else
{
cout << "WriteOnly error";
return;
}
}
}
// 追加节点
void DomXML::appendXML(QString filePath, QStringList list)
{
QFile file(filePath);
bool isOk = file.open(QIODevice::ReadOnly);
if( isOk == true ) // 打开成功
{
// file和xml文档对象关联
QDomDocument doc;
QString error;
isOk = doc.setContent(&file, false, &error);
if( isOk )
{
cout << "关联成功";
file.close(); // 关闭文件
// 获取根节点元素
QDomElement root = doc.documentElement();
// 获取当前时间
QDateTime date = QDateTime::currentDateTime();
QString dateStr = date.toString("yyyy-MM-dd");
// 判断根节点下有没有子节点
if(root.hasChildNodes())
{ // 有子节点
cout << "有子节点";
// 查找最后一个子节点
QDomElement lastEmt = root.lastChildElement();
if(lastEmt.attribute("date") == dateStr)
{
// 有当天日期
// 写有效数据
writeXML(doc, lastEmt, list);
}else
{
// 没有当天日期
// 创建日期子节点元素
QDomElement dateEmt = doc.createElement("日期");
// 创建date属性
QDomAttr dateAttr = doc.createAttribute("date");
// 设置属性的值
dateAttr.setNodeValue(dateStr);
// 节点和属性关联
dateEmt.setAttributeNode(dateAttr);
// 把日期节点追加的根节点上
root.appendChild(dateEmt);
// 写有效数据
writeXML(doc, dateEmt, list);
}
}else
{ // 无子节点
// 创建日期子节点元素
QDomElement dateEmt = doc.createElement("日期");
// 创建date属性
QDomAttr dateAttr = doc.createAttribute("date");
// 设置属性的值
dateAttr.setNodeValue(dateStr);
// 节点和属性关联
dateEmt.setAttributeNode(dateAttr);
// 把日期节点追加的根节点上
root.appendChild(dateEmt);
// 写有效数据
writeXML(doc, dateEmt, list);
}
// 保存文件
isOk = file.open(QIODevice::WriteOnly);
if(isOk)
{
QTextStream stream( &file );
doc.save(stream, 4);
file.close();
cout << "追加了";
}
}else {
cout << "关联失败";
qDebug() << error;
return;
}
}else
{
cout << "ReadOnly error";
}
}
void DomXML::writeXML(QDomDocument &doc, QDomElement &root, QStringList &list)
{
// 获取当前时间
QDateTime tiem = QDateTime::currentDateTime();
QString tiemStr = tiem.toString("hh-mm-ss");
// 创建时间节点元素
QDomElement timeEmt = doc.createElement("时间");
// 创建属性
QDomAttr timeAttr = doc.createAttribute("time");
// 给属性设置值
timeAttr.setNodeValue(tiemStr);
// 时间节点元素和属性关联
timeEmt.setAttributeNode(timeAttr);
// 把时间节点追加到日期节点后面
root.appendChild(timeEmt);
// 创建子节点
QDomElement factory = doc.createElement("厂家");
QDomElement brand = doc.createElement("品牌");
QDomElement price = doc.createElement("报价");
QDomElement num = doc.createElement("数量");
QDomElement total = doc.createElement("金额");
QDomText text = doc.createTextNode(list.at(0));
factory.appendChild(text);
text = doc.createTextNode(list.at(1));
brand.appendChild(text);
text = doc.createTextNode(list.at(2));
price.appendChild(text);
text = doc.createTextNode(list.at(3));
num.appendChild(text);
text = doc.createTextNode(list.at(4));
total.appendChild(text);
timeEmt.appendChild(factory);
timeEmt.appendChild(brand);
timeEmt.appendChild(price);
timeEmt.appendChild(num);
timeEmt.appendChild(total);
}
void DomXML::readXML(QString filePath, QStringList &fList, QStringList &bList, QStringList &pList, QStringList &nList, QStringList &tList )
{
QFile file(filePath);
bool isOk = file.open(QIODevice::ReadOnly);
if( isOk == true ) // 打开成功
{
cout << "打开成功";
// file和xml文档对象关联
QDomDocument doc;
QString error;
isOk = doc.setContent(&file, false, &error);
file.close();
QDateTime date = QDateTime::currentDateTime();
QString dateStr = date.toString("yyyy-MM-dd");
if( isOk )
{
// 获取根节点
QDomElement root = doc.documentElement();
if(root.hasChildNodes())
{
// 找最后一个节点元素
QDomElement lastEmt = root.lastChildElement();
// 判断最后一个节点是否和当天日期相同
if(lastEmt.attribute("date") == dateStr)
{
// 找出当前日期下所有时间子节点
QDomNodeList list = lastEmt.childNodes();
for(int i = 0; i < list.size(); i++)
{
// list.at(0).toElement();
// 转换为元素,找到时间节点下的所有子节点
QDomNodeList subList = list.at(i).toElement().childNodes();
// 厂家
QString factory = subList.at(0).toElement().text();
fList.append(factory);
// 品牌
QString brand = subList.at(1).toElement().text();
bList.append(brand);
// 价格
QString price = subList.at(2).toElement().text();
pList.append(price);
// 销量
QString num = subList.at(3).toElement().text();
nList.append(num);
// 金额
QString total = subList.at(4).toElement().text();
tList.append(total);
}
}else
{
cout << "无当天日期";
return;
}
}else
{
cout << "没有子节点";
}
}else
{
cout << "关联失败";
}
}else
{
cout << "打开失败";
return;
}
}
mainwindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
void connectDB(); // 连接数据库函数
void initData(); // 初始化
private slots:
void on_actionCar_triggered();
void on_actionCalc_triggered();
void on_comboBoxFactory_currentIndexChanged(const QString &arg1);
void on_comboBoxBrand_currentIndexChanged(const QString &arg1);
void on_spinBox_valueChanged(int arg1);
void on_buttonCancel_clicked();
void on_buttonSure_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QSqlDatabase>
#include <QSqlError>
#include <QMessageBox>
#include <QSqlQueryModel> // 数据库模型
#include <QSqlQuery>
#include "domxml.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
// 初始化时显示的界面
on_actionCar_triggered();
// 打开数据库
connectDB();
// 初始化数据
initData();
DomXML::createXML("../demo.xml"); // 创建空的xml文件
// QStringList list;
// list << "二汽神龙" << "毕加索" << "39" << "1" << "39";
// DomXML::appendXML("../demo1.xml", list);
// QStringList fList;
// QStringList bList;
// QStringList pList;
// QStringList nList;
// QStringList tList;
// DomXML::readXML("../demo1.xml", fList, bList, pList, nList, tList);
// for(int i = 0; i < fList.size(); i++)
// {
// QString str = QString("%1:%2:卖出了%3, 单价:%4, 总价:%5")
// .arg(fList.at(i))
// .arg(bList.at(i))
// .arg(pList.at(i))
// .arg(nList.at(i))
// .arg(tList.at(i));
// ui->textEdit->append(str);
// }
}
MainWindow::~MainWindow()
{
delete ui;
}
// 车辆管理菜单
void MainWindow::on_actionCar_triggered()
{
// 切换到车辆管理页面
ui->stackedWidget->setCurrentWidget(ui->car);
ui->label->setText("车辆管理");
}
// 销售统计菜单
void MainWindow::on_actionCalc_triggered()
{
// 切换到销售统计页面
ui->stackedWidget->setCurrentWidget(ui->calc);
ui->label->setText("销售统计");
}
// 连接数据库
void MainWindow::connectDB()
{
// 添加数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName("127.0.0.1");
db.setUserName("root");
db.setPassword("123456");
db.setDatabaseName("car");
if (!db.open())
{
QMessageBox::warning(this, "数据库打开失败", db.lastError().text());
return;
}
}
// 初始化数据
void MainWindow::initData()
{
QSqlQueryModel *queryModel = new QSqlQueryModel(this); // 新建模型
queryModel->setQuery("select name from factory"); // sql语句
// 设置值
ui->comboBoxFactory->setModel(queryModel);
ui->last->setText("0"); // 剩余数量初始化
ui->lineEditTotal->setEnabled(false); // 金额初始化
}
// 厂家下拉框槽函数
void MainWindow::on_comboBoxFactory_currentIndexChanged(const QString &arg1)
{
if (arg1 == "请选择厂家")
{
// 内容清空
ui->comboBoxBrand->clear(); // 品牌下拉框清空
ui->lineEditPrice->clear(); // 报价清空
ui->last->setText("0");
ui->lineEditTotal->clear(); // 金额清空
ui->spinBox->setValue(0); // 数量选择清空
ui->spinBox->setEnabled(false);
ui->buttonSure->setEnabled(false);
}else
{
ui->comboBoxBrand->clear(); // 先清空
QSqlQuery query;
QString sql = QString("select name from brand where factory = '%1'").arg(arg1);
// 执行sql语句
query.exec(sql);
// 获取内容
while(query.next())
{
QString name = query.value(0).toString();
ui->comboBoxBrand->addItem(name);
}
ui->spinBox->setEnabled(true);
}
}
// 品牌下拉框槽函数
void MainWindow::on_comboBoxBrand_currentIndexChanged(const QString &arg1)
{
QSqlQuery query;
QString sql = QString("select price, last from brand where factory = '%1' and name = '%2'")
.arg(ui->comboBoxFactory->currentText()) // 厂家下拉框当前选中内容
.arg(arg1); // 选中的品牌
// 执行sql语句
query.exec(sql);
// 获取内容
while(query.next())
{
// 报价
int price = query.value("price").toInt();
// 剩余数
int last = query.value("last").toInt();
// 更新到对应的空间
ui->lineEditPrice->setText(QString::number(price));
ui->last->setText(QString::number(last));
ui->spinBox->setMaximum(last);
}
ui->spinBox->setValue(0);
}
// 数据选择槽函数
void MainWindow::on_spinBox_valueChanged(int arg1)
{
// 更新剩余数量
// 厂家
QString factoryStr = ui->comboBoxFactory->currentText();
// 品牌
QString brandStr = ui->comboBoxBrand->currentText();
// 获取数据库剩余值
QSqlQuery query;
QString sql = QString("select sum, last from brand where factory = '%1' and name = '%2'")
.arg(factoryStr)
.arg(brandStr);
// 执行数据库
query.exec(sql);
int last;
while (query.next()) {
last = query.value("last").toInt(); // 剩余
}
int tgempNum = last - arg1;
ui->last->setText(QString::number(tgempNum));
// 金额
int price = ui->lineEditPrice->text().toInt(); // 报价
int sum = price * arg1;
ui->lineEditTotal->setText(QString::number(sum));
// 判断确定按钮状态
if (arg1)
{
ui->buttonSure->setEnabled(true);
} else
{
ui->buttonSure->setEnabled(false);
}
}
// 取消槽
void MainWindow::on_buttonCancel_clicked()
{
on_comboBoxFactory_currentIndexChanged("请选择厂家");
ui->comboBoxFactory->setCurrentIndex(0);
}
// 确定槽
void MainWindow::on_buttonSure_clicked()
{
// 获取信息
int num = ui->spinBox->value(); // 销售数据
int last = ui->last->text().toInt(); // 剩余数据
// 获取数据库的销量
QSqlQuery query;
QString sql = QString("select sell from brand where factory = '%1' and name = '%2'")
.arg( ui->comboBoxFactory->currentText() )
.arg( ui->comboBoxBrand->currentText() );
// 执行数据库
query.exec(sql);
int sell;
while (query.next()) {
sell = query.value("sell").toInt();
}
// 更新数据库,剩余数量,销售总量
sell += num;
sql = QString("update brand set sell = %1, last = %2 where factory = '%3' and name = '%4'")
.arg( sell )
.arg( last )
.arg( ui->comboBoxFactory->currentText() )
.arg( ui->comboBoxBrand->currentText() );
query.exec(sql);
// 把确认后的数据更新到xml中
QStringList list;
list << ui->comboBoxFactory->currentText()
<< ui->comboBoxBrand->currentText()
<< ui->lineEditPrice->text()
<< QString::number(num)
<< ui->lineEditTotal->text();
DomXML::appendXML("../demo.xml", list);
QStringList fList;
QStringList bList;
QStringList pList;
QStringList nList;
QStringList tList;
DomXML::readXML("../demo.xml", fList, bList, pList, nList, tList);
ui->textEdit->clear(); // 先清除框内的数据
for(int i = 0; i < fList.size(); i++)
{
QString str = QString("%1:%2:卖出了%3, 单价:%4, 总价:%5")
.arg(fList.at(i))
.arg(bList.at(i))
.arg(pList.at(i))
.arg(nList.at(i))
.arg(tList.at(i));
ui->textEdit->append(str);
}
ui->spinBox->setValue(0);
}
ui界面
数据库: