本系统基于QT界面实现,连接了Mysql数据库来存取汽车数量信息,xml来保存销售信息
1.1背景分析
随着现代科学技术的迅猛发展,计算机技术已经渗透到各个领域,成为各行业必不可少的工具,特别是Internet技术的推广和信息高速公路的建立,使IT产业在市场竞争中越发显示出其独特的优势,步入信息化时代,有巨大的数据信息等待加工处理和传输,这使得对数据库的进一步开发和利用显得尤为迫切。汽车工业经历了一百多年的发展历史,对国家经济的发展和腾飞以及对人类社会的文明带来了巨大影响。在许多国家,汽车工业已成为支柱产业,随着人们生活水平以及汽车性能的不断提高,人们对汽车的消费和需求也越来越旺盛,世界汽车工业也保持庞大的市场需求和生产规模。在IT行业中从业的工作人员一般都要求掌握计算机技术,具有一定的软硬件基础,会使用各种管理软件,熟悉IT产品。因为,有的公司对职工的素质要求比较高,从管理层到下面的销售人员,都要求具有一定的计算机基础,所以在新系统投入使用时,只要对职工进行少量的培训,系统的功能和使用方法就基本上能够是系统顺利运行。因为通过网络传递销售信息可以不受距离的限制,因此可以借阅许多的人力和物力,方便管理,由此可以减少不必要的开支,同时该系统可以提高公司的销售效率,提高了公司的经济效益。销售管理系统是一个信息化、智能化和先进管理理念的集合体。而销售管理是一个动态过程,在其运行过程中要采取多项措施。所以在销售管理中获得经济效益是一个综合效益,要对它进行直接定量的分析是比较困难的。一般新系统带来的经济效益是简介的,其最主要的表现就是减少了企业管理费用和人力开支。系统采用基于Windows的图形用户界面,而该系统是大家熟悉的操作系统,对于那些有一般的计算机知识的人员就可以轻松上手。而整个公司管理系统采用最友好的交互界面,简介明了,不需要对数据库进行深入的了解。由此,该系统的操作是可行的,有必要开发该系统。
实现如图所示:
主界面
2.左上角的菜单可以切换不同页面
3.其中一张数据库中的数据
4.选择厂家以及品牌,还有销售数量
5.车辆管理界面,右边的销售信息是用xml写的
6.可视化数据库,增加删除设置了手动提交事务
7.选择厂家爱和品牌可以显示出车辆样子
8.选择相应厂家可以饼图显示相对的销售情况
工程文件中加
QT += core gui sql xml
QT += charts
RC_ICONS = pygame.ico //改变图标
主要实现代码:
关于存取销售信息的xml文件
domxml.cpp
#include "domxml.h"
#include
#include //文件流
#include
#include //文件
#include //xml文件指针
#include //格式头部
#include //元素
#include
#include
#define cout qDebug() << "[" << __FILE__ << ":" << __LINE__ << "]"
DomXML::DomXML()
{
}
void DomXML::createXML(QString filePath)
{
QFile file(filePath);
if(true == file.exists()) //如果文件存在,就不创建
{
cout << "文件已经存在";
return;
}
else //不存在就创建
{
//只写方式打开
bool isOk = file.open(QIODevice::WriteOnly);
if(true == isOk) // 如果打开成功
{
//创建XML文档
QDomDocument doc;
//创建XML头部格式
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(QFile::ReadOnly);
if(true == isOk)//如果打开成功
{
//file和xml文档对象关联
QDomDocument doc;
isOk = doc.setContent(&file);
if(true == 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("日期");
//创建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(true == isOk)
{
QTextStream stream(&file);
doc.save(stream,4);
file.close();
}
}
else
{
cout << "setContent error";
return;
}
//
}
else
{
cout << "ReadOnly error";
return;
}
}
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(QFile::ReadOnly);
if(true == isOk)//如果打开成功
{
//file和xml文档对象关联
QDomDocument doc;
isOk = doc.setContent(&file);
if(true == 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 //没有
{
cout << "没有当天日期";
return;
}
}
else
{
cout << "没有子结点";
}
}
else
{
cout << "setContent error";
return;
}
}
else
{
cout << "ReadOnly error";
return;
}
}
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include
#include
#include
#include
#include
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void connectDB();//连接数据库
void initDate();//初始化数据
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();
void on_buttonAdd_clicked();
void on_buttonQD_clicked();
void on_buttonCX_clicked();
void on_buttonDel_clicked();
void on_buttonFind_clicked();
void on_comboBox1_currentIndexChanged(const QString &arg1);
void on_comboBox2_currentIndexChanged(const QString &arg1);
void on_actiond_triggered();
void on_comboBox_currentIndexChanged(const QString &arg1);
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
QSqlTableModel *model;
QPieSeries *series;
QMainWindow window;
};
#endif // MAINWINDOW_H
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include //数据库
#include //一些错误提示
#include //对话框
#include //数据模型
#include //数据库
#include
#include "domxml.h"
#include
#include
#include //记录
#include
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
//初始车辆管理页面
on_actionCar_triggered();
//连接数据库
connectDB();
//初始化数据
initDate();
//创建空XML
DomXML::createXML("../demo.xml");
//饼图
series = new QPieSeries();
}
MainWindow::~MainWindow()
{
delete ui;
}
//车辆管理菜单
void MainWindow::on_actionCar_triggered()
{
//切换到车辆管理页面
ui->stackedWidget->setCurrentWidget(ui->car);
ui->label->setText("车辆管理");
setWindowTitle("车辆管理");
}
//车辆展示菜单
void MainWindow::on_actionCalc_triggered()
{
//切换到车辆展示页面
ui->stackedWidget->setCurrentWidget(ui->calc);
ui->label->setText("车辆展示");
setWindowTitle("车辆展示");
}
//销售统计菜单
void MainWindow::on_actiond_triggered()
{
//切换到销售统计页面
ui->stackedWidget->setCurrentWidget(ui->page_3);
ui->label->setText("销售统计");
setWindowTitle("销售统计");
}
//连接数据库
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 ;
}
//设置模型
model = new QSqlTableModel(this);
model->setTable("brand");//指定使用哪个表
//把model放在wiew
ui->tableView->setModel(model);
//显示model里的数据
model->select();
//把数据库中字段名改成中文显示
model->setHeaderData(1,Qt::Horizontal,"厂家");
model->setHeaderData(2,Qt::Horizontal,"品牌");
model->setHeaderData(3,Qt::Horizontal,"报价");
model->setHeaderData(4,Qt::Horizontal,"总量");
model->setHeaderData(5,Qt::Horizontal,"已出售数量");
model->setHeaderData(6,Qt::Horizontal,"剩余数量");
//设置model的编辑模式,手动提交修改数据
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
//视图不允许修改
//ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}
void MainWindow::initDate()
{
QSqlQueryModel *queryModel = new QSqlQueryModel(this); //新建模型
queryModel->setQuery("select name from factory");//从数据库中取出数据
ui->comboBoxFactory->setModel(queryModel);//厂家下拉框中放入数据
ui->comboBox1->setModel(queryModel);
ui->comboBox->setModel(queryModel);
ui->labelLast->setText("0"); //剩余数量初始化为0
ui->lineEditTotal->setEnabled(false);//金额不能改
}
//厂家下拉框(组合框)
void MainWindow::on_comboBoxFactory_currentIndexChanged(const QString &arg1)
{
if(arg1 == "请选择厂家")
{
//内容清空
ui->comboBoxBrand->clear(); //品牌
ui->lineEditPrice->clear(); //报价
ui->labelLast->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);
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)
{
//出售数量归0
ui->spinBox->setValue(0);
//取出数据库中售价和剩余数量
QSqlQuery query;
QString sql = QString("select price,last from brand where factory = '%1' and name = '%2'").arg(ui->comboBoxFactory->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->labelLast->setText(QString::number(last));
}
}
//出售数量调整框
void MainWindow::on_spinBox_valueChanged(int arg1)
{
//获取选择的厂家和品牌
QString factoryStr = ui->comboBoxFactory->currentText();
QString brandStr = ui->comboBoxBrand->currentText();
//数量为0,确定按钮不能按
if(0 == arg1)
{
ui->buttonSure->setEnabled(false);
}
else
{
ui->buttonSure->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->labelLast->setText(QString::number(tempnum));
//qDebug() << tempnum << " " << last << " " << arg1;
//总价=价格*出售数量
int price = ui->lineEditPrice->text().toInt();
int sum = price * arg1;
ui->lineEditTotal->setText(QString::number(sum));
}
//取消按钮
void MainWindow::on_buttonCancel_clicked()
{
//工厂下拉框设置到0位置,即“请选择工厂”
ui->comboBoxFactory->setCurrentIndex(0);
ui->labelLast->setText("0"); //剩余数量归0
}
//确定按钮
void MainWindow::on_buttonSure_clicked()
{
//获取当前界面中出售数量和剩余数量
int num = ui->spinBox->value();
int last = ui->labelLast->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->plainTextEdit->appendPlainText(" 日销售清单");
int sum=0;
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(nList.at(i))
.arg(pList.at(i))
.arg(tList.at(i));
sum += tList.at(i).toInt();
ui->plainTextEdit->appendPlainText(str);
//qDebug() << str.toUtf8().data();
}
QString str1 = QString("日总收入:%1").arg(sum);
ui->plainTextEdit->appendPlainText(str1);
//确定按钮不能按,并恢复初始化,即按一下取消按钮
ui->buttonSure->setEnabled(false);
on_buttonCancel_clicked();
}
//新车入库,增加按钮
void MainWindow::on_buttonAdd_clicked()
{
//添加空记录
QSqlRecord record = model->record();//获取空记录
//获取行号
int row = model->rowCount();
model->insertRecord(row, record);
}
//新车入库确定按钮
void MainWindow::on_buttonQD_clicked()
{
//提交事务
if(model->submitAll()==false)
qDebug() << model->lastError();
}
//新车入库撤销按钮
void MainWindow::on_buttonCX_clicked()
{
model->revertAll();//取消所有动作
//提交事务
model->submitAll();
}
//新车入库删除
void MainWindow::on_buttonDel_clicked()
{
//获取选中的模型
QItemSelectionModel *sModel = ui->tableView->selectionModel();
//取出模型中的索引
QModelIndexList list = sModel->selectedRows();
//删除所有选中的行
for(int i = 0; i removeRow( list.at(i).row() );
}
}
//查找
void MainWindow::on_buttonFind_clicked()
{
//得到行编辑输入的姓名,筛选出相应的数据
QString name = ui->lineEdit->text();
QString str = QString("name = '%1'").arg(name);
model->setFilter(str);
model->select();
}
//车辆展示页面中的厂家下拉框
void MainWindow::on_comboBox1_currentIndexChanged(const QString &arg1)
{
if(arg1 == "请选择厂家")
{
}
else
{
ui->comboBox2->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->comboBox2->addItem(name);
}
}
}
//车辆展示中品牌下拉框的选择对应相对显示的图片
void MainWindow::on_comboBox2_currentIndexChanged(const QString &arg1)
{
if(arg1 == "桑塔纳")
{
ui->labelImage->setPixmap(QPixmap("://1.png")); //打开图片
ui->labelImage->setScaledContents(true); //自动适应大小
}
else if(arg1 == "帕萨特")
{
ui->labelImage->setPixmap(QPixmap("://2.png"));
ui->labelImage->setScaledContents(true);
}
else if(arg1 == "奥迪A6")
{
ui->labelImage->setPixmap(QPixmap("://a6.png"));
ui->labelImage->setScaledContents(true);
}
else if(arg1 == "捷达")
{
ui->labelImage->setPixmap(QPixmap("://4.png"));
ui->labelImage->setScaledContents(true);
}
else if(arg1 == "奔驰")
{
ui->labelImage->setPixmap(QPixmap("://5.png"));
ui->labelImage->setScaledContents(true);
}
else if(arg1 == "毕加索")
{
ui->labelImage->setPixmap(QPixmap("://6.png"));
ui->labelImage->setScaledContents(true);
}
else if(arg1 == "富康")
{
ui->labelImage->setPixmap(QPixmap("://7.png"));
ui->labelImage->setScaledContents(true);
}
else if(arg1 == "标致307")
{
ui->labelImage->setPixmap(QPixmap("://8.png"));
ui->labelImage->setScaledContents(true);
}
}
//销售统计中厂家下拉框
void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
{
}
//销售统计中的确定按钮
void MainWindow::on_pushButton_clicked()
{
if(ui->comboBox->currentText() == "请选择厂家")
{
return;
}
else
{
QSqlQuery query;
QStringList nameList;
QStringList sellList;
int sum = 0;
//得到相对应厂家的销售量和车名字
QString sql = QString("select sell,name from brand where factory = '%1'").arg(ui->comboBox->currentText());
query.exec(sql);
while(query.next())
{
nameList << query.value("name").toString();
sellList << query.value("sell").toString();
sum = sum + query.value("sell").toInt();
}
//算出百分比以及相对应的显示
//qDebug() << sum << " " <append(nameList.at(0)+" "+QString(sellList.at(0)), QString(sellList.at(0)).toFloat()/sum);
series->append(nameList.at(1)+" "+QString(sellList.at(1)), QString(sellList.at(1)).toFloat()/sum);
series->append(nameList.at(2)+" "+QString(sellList.at(2)), QString(sellList.at(2)).toFloat()/sum);
series->setLabelsVisible();
QPieSlice *slice_red = series->slices().at(0);
QPieSlice *slice_green = series->slices().at(1);
QPieSlice *slice_blue = series->slices().at(2);
//设置扇形的颜色
slice_red->setColor(QColor(255,0,0,255));
slice_green->setColor(QColor(0,255,0,255));
slice_blue->setColor(QColor(0,0,255,255));
QChart *chart = new QChart();
chart->addSeries(series);
//设置标题
chart->setTitle("一汽大众各车型销售情况图");
//chart->legend()->hide();
QChartView *chartview = new QChartView(chart);
chartview->setRenderHint(QPainter::Antialiasing);
//在窗口中显示
window.setCentralWidget(chartview);
window.resize(480, 360);
window.show();
}
}