.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_OBJECTpublic: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界面
数据库:
