#include "widget.h"
#include "ui_widget.h"
#include "connection.h"
#include <QtWidgets>
#include "pieview.h"
Widget::Widget(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
setFixedSize(750,500);
createMenuBar();
ui->stackedWidget->setCurrentIndex(0);
QSqlQueryModel *factoryModel = new QSqlQueryModel(this);
factoryModel->setQuery("select name from factory");
ui->sellFactoryComboBox->setModel(factoryModel);
QSplitter *splitter = new QSplitter(ui->managePage);
splitter->resize(700,360);
splitter->move(0,50);
splitter->addWidget(ui->toolBox);
splitter->addWidget(ui->dailyList);
splitter->setStretchFactor(0,1);
splitter->setStretchFactor(1,1);
on_sellCancelBtn_clicked();
showDailyList();
ui->factoryComboBox->setModel(factoryModel);
createChartModelView();
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_sellBrandComboBox_currentIndexChanged(const QString &arg1)
{
ui->sellNumSpinBox->setValue(0);
ui->sellNumSpinBox->setEnabled(false);
ui->sellSumLineEdit->clear();
ui->sellSumLineEdit->setEnabled(false);
ui->sellOkBtn->setEnabled(false);
QSqlQuery query;
query.exec(QString("select price from brand where name='%1' and factory='%2'").arg(arg1).arg(ui->sellFactoryComboBox->currentText()));
query.next();
ui->sellPriceLineEdit->setEnabled(true);
ui->sellPriceLineEdit->setReadOnly(true);
ui->sellPriceLineEdit->setText(query.value(0).toString());
query.exec(QString("select last from brand where name='%1' and factory='%2'").arg(arg1).arg(ui->sellFactoryComboBox->currentText()));
query.next();
int num = query.value(0).toInt();
if(num == 0){
QMessageBox::information(this,tr("提示"),tr("该品牌汽车已经售完!"),QMessageBox::Ok);
}
else{
ui->sellNumSpinBox->setEnabled(true);
ui->sellNumSpinBox->setMaximum(num);
ui->sellLastNumLabel->setText(tr("剩余数量:%1").arg(num));
ui->sellLastNumLabel->setVisible(true);
}
}
void Widget::on_sellNumSpinBox_valueChanged(int arg1)
{
if(arg1 == 0){
ui->sellSumLineEdit->clear();
ui->sellSumLineEdit->setEnabled(false);
ui->sellOkBtn->setEnabled(false);
}
else{
ui->sellSumLineEdit->setEnabled(true);
ui->sellSumLineEdit->setReadOnly(true);
qreal sum = arg1*ui->sellPriceLineEdit->text().toInt();
ui->sellSumLineEdit->setText(QString::number(sum));
ui->sellOkBtn->setEnabled(true);
}
}
void Widget::on_sellCancelBtn_clicked()
{
ui->sellFactoryComboBox->setCurrentIndex(0);
ui->sellBrandComboBox->clear();
}
void Widget::on_sellOkBtn_clicked()
{
QString factory = ui->sellFactoryComboBox->currentText();
QString name = ui->sellBrandComboBox->currentText();
int value = ui->sellNumSpinBox->value();
//sellNumSpinBox 的最大值就是以前的剩余量
int last = ui->sellNumSpinBox->maximum()-value;
QSqlQuery query;
//获取以前的销售量
query.exec(QString("select sell from brand where name='%1' and factory='%2'").arg(name).arg(factory));
query.next();
int sell = query.value(0).toInt() + value;
//事务操作
QSqlDatabase::database().transaction();
bool rtn = query.exec(QString("update brand set sell= %1,last= %2 where name='%3' and factory='%4'").arg(sell).arg(last).arg(name).arg(factory));
if(rtn){
QSqlDatabase::database().commit();
QMessageBox::information(this,tr("提示"),tr("购车成功!"),QMessageBox::Ok);
writeXml();
showDailyList();
on_sellCancelBtn_clicked();
}
else{
QSqlDatabase::database().rollback();
}
}
QString Widget::getDateTime(DateTimeType type)
{
QDateTime datetime = QDateTime::currentDateTime();
QString date = datetime.toString("yyyy-MM-dd");
QString time = datetime.toString("hh:mm");
QString dateAndTime = datetime.toString("yyyy-MM-dd dddd hh:mm");
if(type == Date)return date;
else if(type == Time)return time;
else return dateAndTime;
}
//读取 XML 文档
bool Widget::docRead()
{
QFile file("data.xml");
if(!file.open(QIODevice::ReadOnly))
return false;
if(!doc.setContent(&file)){
file.close();
return false;
}
file.close();
return true;
}
//写入 XML 文档
bool Widget::docWrite()
{
QFile file("data.xml");
if(!file.open(QIODevice::WriteOnly|QIODevice::Truncate))
return false;
QTextStream out(&file);
doc.save(out,4);
file.close();
return true;
}
//将销售车辆信息保存到 XML 文件中
void Widget::writeXml()
{
//先从文件读取
if(docRead()){
QString currentDate = getDateTime(Date);
QDomElement root = doc.documentElement();
//根据是否有日期节点进行处理
if(!root.hasChildNodes()){
QDomElement date = doc.createElement(QString("日期"));
QDomAttr curDate = doc.createAttribute("date");
curDate.setValue(currentDate);
date.setAttributeNode(curDate);
root.appendChild(date);
createNodes(date);
}else{
QDomElement date = root.lastChild().toElement();
//根据是否已经有今天的日期节点进行处理
if(date.attribute("date") == currentDate){
createNodes(date);
}else{
QDomElement date = doc.createElement(QString("日期"));
QDomAttr curDate = doc.createAttribute("date");
curDate.setValue(currentDate);
date.setAttributeNode(curDate);
root.appendChild(date);
createNodes(date);
}
}
//写入到文件
docWrite();
}
}
//创建销售车辆信息的节点
void Widget::createNodes(QDomElement &date)
{
QDomElement time = doc.createElement(QString("时间"));
QDomAttr curTime = doc.createAttribute("time");
curTime.setValue(getDateTime(Time));
time.setAttributeNode(curTime);
date.appendChild(time);
QDomElement factory = doc.createElement(QString("厂家"));
QDomElement brand = doc.createElement(QString("品牌"));
QDomElement price = doc.createElement(QString("报价"));
QDomElement num = doc.createElement(QString("数量"));
QDomElement sum = doc.createElement(QString("金额"));
QDomText text;
text = doc.createTextNode(QString("%1").arg(ui->sellFactoryComboBox->currentText()));
factory.appendChild(text);
text = doc.createTextNode(QString("%1").arg(ui->sellBrandComboBox->currentText()));
brand.appendChild(text);
text = doc.createTextNode(QString("%1").arg(ui->sellPriceLineEdit->text()));
price.appendChild(text);
text = doc.createTextNode(QString("%1").arg(ui->sellNumSpinBox->value()));
num.appendChild(text);
text = doc.createTextNode(QString("%1").arg(ui->sellSumLineEdit->text()));
sum.appendChild(text);
time.appendChild(factory);
time.appendChild(brand);
time.appendChild(price);
time.appendChild(num);
time.appendChild(sum);
}
//显示日销售清单
void Widget::showDailyList()
{
ui->dailyList->clear();
if(docRead()){
QDomElement root = doc.documentElement();
QString title = root.tagName();
QListWidgetItem *titleItem = new QListWidgetItem;
titleItem->setText(QString("-----%1-----").arg(title));
titleItem->setTextAlignment(Qt::AlignCenter);
ui->dailyList->addItem(titleItem);
if(root.hasChildNodes()){
QString currentDate = getDateTime(Date);
QDomElement dateElement = root.lastChild().toElement();
没有合适的资源?快使用搜索试试~ 我知道了~
Qt5开发及实例-CH1601.rar,Qt5用PieView控件实现汽车销售系统例程代码
共32个文件
cpp:7个
h:7个
o:7个
需积分: 1 4 下载量 137 浏览量
2023-09-15
18:50:10
上传
评论
收藏 1.88MB RAR 举报
温馨提示
Qt5开发及实例,实例CH1601,PieView控件销售数量浏览,登录、列表数据VIEW,销售系统例程 <日销售清单> <日期 date="2017-02-24"> <时间 time="13:36"> <厂家>上海大众</厂家> <品牌>帕萨特</品牌> <报价>27</报价> <数量>2</数量> <金额>54</金额> </时间> </时间> </日期> </日销售清单>
资源推荐
资源详情
资源评论
收起资源包目录
Qt5开发及实例_CH1601.rar (32个子文件)
build-manager-Desktop_Qt_5_8_0_MinGW_32bit-Debug
ui_widget.h 9KB
Makefile 26KB
data.xml 1016B
debug
logindialog.o 715KB
moc_logindialog.o 408KB
moc_pieview.o 515KB
moc_widget.cpp 6KB
manager.exe 3.92MB
moc_widget.o 346KB
main.o 711KB
pieview.o 692KB
moc_logindialog.cpp 4KB
moc_pieview.cpp 3KB
widget.o 1020KB
moc_predefs.h 10KB
release
data.db 7KB
Makefile.Release 111KB
ui_logindialog.h 3KB
Makefile.Debug 110KB
.qmake.stash 726B
manager
logindialog.h 406B
widget.ui 8KB
widget.cpp 13KB
logindialog.ui 2KB
main.cpp 384B
pieview.h 1KB
manager.pro 1KB
manager.pro.user 23KB
pieview.cpp 3KB
logindialog.cpp 1KB
connection.h 3KB
widget.h 1KB
共 32 条
- 1
资源评论
华为奋斗者精神
- 粉丝: 8201
- 资源: 241
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功