车辆售卖管理系统:
工程如图:
在.pro中加入:
mainwindow.h:
#include <QMainWindow>
#include<QSqlDatabase>
#include<QSqlError>
#include<QMessageBox>
#include<QDebug>
#include<QSqlQuery>
#include<QSqlQueryModel>
#include<QDateTime>
QT_BEGIN_NAMESPACE
namespace Ui
{ class MainWindow; }
QT_END_NAMESPACE
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget
*parent
= nullptr);
~MainWindow();
void connectDB();
void initData();
QString DateNow
=" ";
private slots
:
void on_actionCAR_triggered();
void on_actionCALS_triggered();
void on_comboBoxFactor_currentTextChanged(const QString
&arg1
);
void on_comboBoxBrand_currentIndexChanged(const QString
&arg1
);
void on_spinBox_valueChanged(int arg1
);
void on_pushButtonCancel_clicked();
void on_pushButtonSure_clicked();
void on_comboBoxcfactory2_currentTextChanged(const QString
&arg1
);
void on_comboBoxbrand2_currentIndexChanged(const QString
&arg1
);
void on_pushButtonSure2_clicked();
void on_pushButton_Cancel2_clicked();
private:
Ui
::MainWindow
*ui
;
};
#endif
domxml.h:`
#ifndef DOMXML_H
#define DOMXML_H
#include<QFile>
#include<QDomDocument>
#include<QDomProcessingInstruction>
#include<QDomElement>
#include<QString>
#include<QStringList>
class domxml
{
public:
domxml();
static void createXML(QString filePath
);
static void appendXML(QString FilePath
,QStringList list
);
static void appendXML1(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
);
static void writeXML1(QDomDocument
&doc
,QDomElement
&root
,QStringList
&list
);
static void readXML1(QString filePath
,
QStringList
&fList
,
QStringList
&bList
,
QStringList
&nList
);
};
#endif
mainwindow.cpp:
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QString>
#include<QTimer>
#include<QMessageBox>
#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");
}
MainWindow
::~MainWindow()
{
delete ui
;
}
void MainWindow
::on_actionCAR_triggered()
{
ui
->stackedWidget
->setCurrentWidget(ui
->Car
);
ui
->label
->setText("车辆管理");
}
void MainWindow
::on_actionCALS_triggered()
{
ui
->stackedWidget
->setCurrentWidget(ui
->Calc
);
ui
->label
->setText("销售统计");
}
void MainWindow
::connectDB()
{
QSqlDatabase db
=QSqlDatabase
::addDatabase("QODBC");
db
.setDatabaseName(QString("DRIVER={SQL SERVER};"
"SERVER=%1;"
"DATABASE=%2;"
"UID=%3;"
"PWD=%4;"
)
.arg("DESKTOP-52HBKHH")
.arg("test")
.arg("sa")
.arg("tianlei0730")
);
if (!db
.open())
{
QMessageBox
::critical(0, qApp
->tr("Cannot open database"),
db
.lastError().databaseText(),
QMessageBox
::Cancel
);
}
else
{
qDebug()<<"success";
}
}
void MainWindow
::initData()
{
QSqlQueryModel
*queryModel
=new QSqlQueryModel(this);
queryModel
->setQuery("select factory from factory");
ui
->comboBoxFactor
->setModel(queryModel
);
ui
->lineEditLast
->setText("0");
ui
->lineEditTotal
->setEnabled(false);
ui
->comboBoxcfactory2
->setModel(queryModel
);
ui
->lineEditSellPrice
->setEnabled(false);
ui
->pushButtonSure2
->setEnabled(false);
ui
->spinBox_2
->setEnabled(false);
}
void MainWindow
::on_comboBoxFactor_currentTextChanged(const QString
&arg1
)
{
if(arg1
=="请选择厂家")
{
ui
->comboBoxBrand
->clear();
ui
->lineEditPrice
->clear();
ui
->lineEditLast
->setText("0");
ui
->lineEditTotal
->clear();
ui
->spinBox
->setValue(0);
ui
->spinBox
->setEnabled(false);
ui
->pushButtonSure
->setEnabled(false);
}
else
{
ui
->comboBoxBrand
->clear();
QSqlQuery query
;
QString sql
=QString("select name from brand where factory='%1'").arg(arg1
);
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
->comboBoxFactor
->currentText())
.arg(arg1
);
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
->lineEditLast
->setText(QString
::number(last
));
}
}
void MainWindow
::on_spinBox_valueChanged(int arg1
)
{
QString factorystr
=ui
->comboBoxFactor
->currentText();
QString brandstr
=ui
->comboBoxBrand
->currentText();
if(0==arg1
)
{
ui
->pushButtonSure
->setEnabled(false);
}
else
{
ui
->pushButtonSure
->setEnabled(true);
}
QSqlQuery query
;
QString sql
=QString("select 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();
}
if(arg1
>last
)
{
ui
->spinBox
->setValue(last
);
return;
}
int tempNum
=last
-arg1
;
ui
->lineEditLast
->setText(QString
::number(tempNum
));
int price
=ui
->lineEditPrice
->text().toInt();
int sum
=price
*arg1
;
ui
->lineEditTotal
->setText(QString
::number(sum
));
}
void MainWindow
::on_pushButtonCancel_clicked()
{
ui
->comboBoxFactor
->setCurrentIndex(0);
ui
->lineEditLast
->setText("0");
}
void MainWindow
::on_pushButtonSure_clicked()
{
int num
=ui
->spinBox
->value();
int last
=ui
->lineEditLast
->text().toInt();
QSqlQuery query
;
QString sql
=QString("select sell from brand where factory='%1'and name='%2'")
.arg(ui
->comboBoxFactor
->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
->comboBoxFactor
->currentText())
.arg(ui
->comboBoxBrand
->currentText());
query
.exec(sql
);
QStringList list
;
list
<<ui
->comboBoxFactor
->currentText()
<<ui
->comboBoxBrand
->currentText()
<<ui
->lineEditPrice
->text()
<<QString
::number(num
)
<<ui
->lineEditTotal
->text();
QDateTime date
= QDateTime
::currentDateTime();
QString dateStr
=date
.toString("yyyy-MM-dd");
if(DateNow
!=dateStr
)
{
ui
->textEdit
->append(dateStr
);
DateNow
=dateStr
;
}
domxml
::appendXML("../demo.xml",list
);
QStringList fList
;
QStringList bList
;
QStringList pList
;
QStringList nList
;
QStringList tList
;
QDateTime time
= QDateTime
::currentDateTime();
QString timeStr
=time
.toString("hh-mm-ss");
domxml
::readXML("../demo.xml",fList
,bList
,pList
,nList
,tList
);
int i
=fList
.length()-1;
QString str
=QString("%2:%3:单价:%4万 卖出:%5辆 金额:%6万 时间:%7")
.arg(fList
.at(i
))
.arg(bList
.at(i
))
.arg(pList
.at(i
))
.arg(nList
.at(i
))
.arg(tList
.at(i
))
.arg(timeStr
);
ui
->textEdit
->append(str
);
ui
->pushButtonSure
->setEnabled(false);
on_pushButtonCancel_clicked();
}
void MainWindow
::on_comboBoxcfactory2_currentTextChanged(const QString
&arg1
)
{
if(arg1
=="请选择厂家")
{
ui
->comboBoxbrand2
->clear();
ui
->lineEditSellPrice
->clear();
ui
->spinBox_2
->clear();
}
else
{
ui
->comboBoxbrand2
->clear();
QSqlQuery query
;
QString sql
=QString("select name from brand where factory='%1'").arg(arg1
);
query
.exec(sql
);
while(query
.next())
{
QString name
=query
.value(0).toString();
ui
->comboBoxbrand2
->addItem(name
);
}
ui
->spinBox_2
->setEnabled(true);
ui
->pushButtonSure2
->setEnabled(true);
}
}
void MainWindow
::on_comboBoxbrand2_currentIndexChanged(const QString
&arg1
)
{
QSqlQuery query
;
QString sql
=QString("select price from brand where factory='%1'and name='%2'")
.arg(ui
->comboBoxcfactory2
->currentText())
.arg(arg1
);
query
.exec(sql
);
while(query
.next())
{
int price
=query
.value("price").toInt();
ui
->lineEditSellPrice
->setText(QString
::number(price
));
}
}
void MainWindow
::on_pushButtonSure2_clicked()
{
QString factorystr
=ui
->comboBoxcfactory2
->currentText();
QString brandstr
=ui
->comboBoxbrand2
->currentText();
int num
=ui
->spinBox_2
->value();
QSqlQuery query
;
QString sql
=QString("select 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();
}
last
+=num
;
sql
=QString("update brand set last='%1' where factory='%2'and name='%3'")
.arg(last
)
.arg(factorystr
)
.arg(brandstr
);
query
.exec(sql
);
QStringList list
;
list
<<ui
->comboBoxcfactory2
->currentText()
<<ui
->comboBoxbrand2
->currentText()
<<ui
->spinBox_2
->text();
domxml
::appendXML1("../demo.xml",list
);
QDateTime date
= QDateTime
::currentDateTime();
QString dateStr
=date
.toString("yyyy-MM-dd");
if(DateNow
!=dateStr
)
{
ui
->textEdit
->append(dateStr
);
DateNow
=dateStr
;
}
QStringList fList
;
QStringList bList
;
QStringList nList
;
QDateTime time
= QDateTime
::currentDateTime();
QString timeStr
=time
.toString("hh-mm-ss");
domxml
::readXML1("../demo.xml",fList
,bList
,nList
);
int i
=fList
.length()-1;
QString str
=QString("%1:%2:添加数量为:%3 库存数量:%4 时间:%5")
.arg(fList
.at(i
))
.arg(bList
.at(i
))
.arg(nList
.at(i
))
.arg(last
)
.arg(timeStr
);
ui
->textEdit
->append(str
);
ui
->pushButtonSure2
->setEnabled(false);
on_pushButton_Cancel2_clicked();
}
void MainWindow
::on_pushButton_Cancel2_clicked()
{
ui
->comboBoxcfactory2
->setCurrentIndex(0);
}
domxml.cpp:
#include "domxml.h"
#include<QFile>
#include<QDomDocument>
#include<QDomProcessingInstruction>
#include<QDomElement>
#include<QDebug>
#include<QTextStream>
#include<QDateTime>
domxml
::domxml()
{
}
void domxml
::createXML(QString filePath
)
{
QFile
file(filePath
);
if(true==file
.exists())
{
qDebug()<<"文件已经存在";
return;
}
else
{
bool isok
= file
.open(QIODevice
::WriteOnly
);
if(true==isok
)
{
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);
file
.close();
}
else
{
qDebug()<<"writeonly error";
return;
}
}
}
void domxml
::appendXML(QString FilePath
, QStringList list
)
{
QFile
file(FilePath
);
bool isok
=file
.open(QIODevice
::ReadOnly
);
if(true==isok
)
{
QDomDocument doc
;
isok
=doc
.setContent(&file
);
if(isok
)
{
file
.close();
QDomElement root
=doc
.documentElement();
QDateTime date
= QDateTime
::currentDateTime();
QString dateStr
=date
.toString("yyyy-MM-dd");
if(root
.hasChildNodes())
{
QDomElement lastEmt
=root
.lastChildElement();
if(lastEmt
.attribute("date")==dateStr
)
{
writeXML(doc
,lastEmt
,list
);
}
else
{
QDomElement dateEmt
=doc
.createElement("日期");
QDomAttr dateAttr
=doc
.createAttribute("date");
dateAttr
.setNodeValue(dateStr
);
dateEmt
.setAttributeNode(dateAttr
);
root
.appendChild(dateEmt
);
writeXML(doc
,dateEmt
,list
);
}
}
else
{
QDomElement dateEmt
=doc
.createElement("日期");
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();
}
}
else
{
qDebug()<<" setContent error";
return;
}
}
else
{
qDebug()<<"readonly error";
}
}
void domxml
::appendXML1(QString FilePath
, QStringList list
)
{
QFile
file(FilePath
);
bool isok
=file
.open(QIODevice
::ReadOnly
);
if(true==isok
)
{
QDomDocument doc
;
isok
=doc
.setContent(&file
);
if(isok
)
{
file
.close();
QDomElement root
=doc
.documentElement();
QDateTime date
= QDateTime
::currentDateTime();
QString dateStr
=date
.toString("yyyy-MM-dd");
if(root
.hasChildNodes())
{
QDomElement lastEmt
=root
.lastChildElement();
if(lastEmt
.attribute("date")==dateStr
)
{
writeXML1(doc
,lastEmt
,list
);
}
else
{
QDomElement dateEmt
=doc
.createElement("日期");
QDomAttr dateAttr
=doc
.createAttribute("date");
dateAttr
.setNodeValue(dateStr
);
dateEmt
.setAttributeNode(dateAttr
);
root
.appendChild(dateEmt
);
writeXML1(doc
,dateEmt
,list
);
}
}
else
{
QDomElement dateEmt
=doc
.createElement("日期");
QDomAttr dateAttr
=doc
.createAttribute("date");
dateAttr
.setNodeValue(dateStr
);
dateEmt
.setAttributeNode(dateAttr
);
root
.appendChild(dateEmt
);
writeXML1(doc
,dateEmt
,list
);
}
isok
=file
.open(QIODevice
::WriteOnly
);
if(isok
)
{
QTextStream
stream(&file
);
doc
.save(stream
,4);
file
.close();
}
}
else
{
qDebug()<<" setContent error";
return;
}
}
else
{
qDebug()<<"readonly error";
}
}
void domxml
::writeXML(QDomDocument
&doc
, QDomElement
&root
, QStringList
&list
)
{
QDateTime time
= QDateTime
::currentDateTime();
QString timeStr
=time
.toString("hh-mm-ss");
QDomElement timeEmt
=doc
.createElement("时间");
QDomAttr timeAttr
=doc
.createAttribute("time");
timeAttr
.setNodeValue(timeStr
);
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(true==isok
)
{
QDomDocument doc
;
isok
=doc
.setContent(&file
);
if(isok
)
{
QDomElement root
=doc
.documentElement();
file
.close();
QDateTime date
=QDateTime
::currentDateTime();
QString dateStr
=date
.toString("yyyy-MM-dd");
if(root
.hasChildNodes())
{
QDomElement lastEmt
=root
.lastChildElement();
if(lastEmt
.attribute("date")==dateStr
)
{
QDomNodeList list
= lastEmt
.childNodes();
for(int i
=0;i
<list
.size();i
++)
{
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
{
qDebug()<<"没有当天日期";
return;
}
}
else
{
qDebug()<<"没有子节点";
return;
}
}
else
{
qDebug()<<"setcontent error";
return;
}
}
else
{
qDebug()<<"readonly error";
return;
}
}
void domxml
::writeXML1(QDomDocument
&doc
, QDomElement
&root
, QStringList
&list
)
{
QDateTime time
= QDateTime
::currentDateTime();
QString timeStr
=time
.toString("hh-mm-ss");
QDomElement timeEmt
=doc
.createElement("时间");
QDomAttr timeAttr
=doc
.createAttribute("time");
timeAttr
.setNodeValue(timeStr
);
timeEmt
.setAttributeNode(timeAttr
);
root
.appendChild(timeEmt
);
QDomElement factory
=doc
.createElement("厂家");
QDomElement brand
=doc
.createElement("品牌");
QDomElement num
=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));
num
.appendChild(text
);
timeEmt
.appendChild(factory
);
timeEmt
.appendChild(brand
);
timeEmt
.appendChild(num
);
}
void domxml
::readXML1(QString filePath
, QStringList
&fList
, QStringList
&bList
,
QStringList
&nList
)
{
QFile
file(filePath
);
bool isok
=file
.open(QIODevice
::ReadOnly
);
if(true==isok
)
{
QDomDocument doc
;
isok
=doc
.setContent(&file
);
if(isok
)
{
QDomElement root
=doc
.documentElement();
file
.close();
QDateTime date
=QDateTime
::currentDateTime();
QString dateStr
=date
.toString("yyyy-MM-dd");
if(root
.hasChildNodes())
{
QDomElement lastEmt
=root
.lastChildElement();
if(lastEmt
.attribute("date")==dateStr
)
{
QDomNodeList list
= lastEmt
.childNodes();
for(int i
=0;i
<list
.size();i
++)
{
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 num
= subList
.at(2).toElement().text();
nList
.append(num
);
}
}
else
{
qDebug()<<"没有当天日期";
return;
}
}
else
{
qDebug()<<"没有子节点";
return;
}
}
else
{
qDebug()<<"setcontent error";
return;
}
}
else
{
qDebug()<<"readonly error";
return;
}
}
相关控件:
数据库表内容:
运行结果:
数据库对应自动更新:
转载请注明原文地址:https://ipadbbs.8miu.com/read-62209.html