#include "widget.h"
#include "ui_widget.h"
#include <QtSql>
#include <QMessageBox>
//新增
#include <QSplitter>
#include <QTableView>
#include "pieview.h"
Widget::Widget(QWidget *parent) :
//修改基类
QMainWindow(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
createMenuBar();//生成菜单栏
QSqlQueryModel *factoryModel = new QSqlQueryModel(this);
//执行查询语句
factoryModel->setQuery("select name from factory");
//把查到的结果设置到sellFactoryComboBox组件上
ui->sellFactoryComboBox->setModel(factoryModel);
//显示销售清单
showDailyList();
//将factoryModel设置位factoryComboBox类型
ui->factoryComboBox->setModel(factoryModel);
//创建销售数据的视图和模型
createChartModelView();
}
Widget::~Widget()
{
delete ui;
}
void Widget::createMenuBar()
{
/*
* tr()用来定义一个本地化字符串
* this表示当前对象所在窗口
*/
manageAction = new QAction(tr("品牌车管理"), this);
chartAction = new QAction(tr("销售统计"), this);
quitAction = new QAction(tr("退出"), this);
//设置快捷键
manageAction->setShortcut(tr("Ctrl+M"));
chartAction->setShortcut(tr("Ctrl+C"));
quitAction->setShortcut(tr("Ctrl+Q"));
//添加菜单到菜单栏中
manageMenu = menuBar()->addMenu(tr("销售管理"));
//在菜单等控件中添加动作
manageMenu->addAction(manageAction);
manageMenu->addAction(chartAction);
manageMenu->addSeparator();//添加工具栏中的分隔线
manageMenu->addAction(quitAction);
passwordMenu = menuBar()->addMenu(tr("修改密码"));
connect(manageAction,SIGNAL(triggered()),
this,SLOT(on_manageMenu_clicked()));
connect(chartAction,SIGNAL(triggered()),this,SLOT(on_chartMenu_clicked()));
connect(quitAction,SIGNAL(triggered()),this,SLOT(on_quitMenu_clicked()));
}
void Widget::on_manageMenu_clicked()
{
//stackedWidget通常用于在不同的子页面中进行切换
//setCurrentIndex()设置当前子页面的索引
ui->stackedWidget->setCurrentIndex(0);
}
void Widget::on_chartMenu_clicked()
{
ui->stackedWidget->setCurrentIndex(1);
}
void Widget::on_quitMenu_clicked()
{
this->close();
}
void Widget::on_sellFactoryComboBox_currentIndexChanged(const QString &arg1)
{
if(arg1 == "请选择厂家")
{
//对其他部件的状态设置
on_sellCancelBtn_clicked();
}
else
{
//设置品牌控件为启用
ui->sellBrandComboBox->setEnabled(true);
QSqlQueryModel *model = new QSqlQueryModel(this);
model->setQuery(QString("select name from brand where factory='%1'").arg(arg1));
ui->sellBrandComboBox->setModel(model);
}
}
void Widget::on_sellBrandComboBox_currentIndexChanged(const QString &arg1)
{
//创建查询对象
QSqlQuery query;
query.exec(QString("select price from brand where name='%1' and factory='%2'").arg(arg1).arg(ui->sellFactoryComboBox->currentText()));
//使用next()函数来获取当前游标所指向的结果集的下一行数据
query.next();
//启用该控件,使该控件可以与用户进行交互,我们目前需要他输入销售价格
ui->sellPriceLineEdit->setEnabled(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();
ui->sellNumSpinBox->setEnabled(true);
//数量控件中的数值,不能超过库存最大值
ui->sellNumSpinBox->setMaximum(num);
ui->sellLastNumLabel->setText(
QString::fromLocal8Bit("剩余数量:%1").arg(num));
ui->sellLastNumLabel->setVisible(true);
}
void Widget::on_sellNumSpinBox_valueChanged(int arg1)
{
if (arg1 == 0)
{
//确保每次操作之前,控件不会显示上一次的销售数量
ui->sellNumLineEdit->clear();
//禁用控件
ui->sellNumLineEdit->setEnabled(false);
ui->sellOkBtn->setEnabled(false);
}
else
{
ui->sellNumLineEdit->setEnabled(true);
//计算销售总额
int num = arg1 * ui->sellPriceLineEdit->text().toInt();
ui->sellNumLineEdit->setText(QString::number(num));
ui->sellOkBtn->setEnabled(true);
}
}
void Widget::on_sellCancelBtn_clicked()
{
//清空控件内容
ui->sellFactoryComboBox->setCurrentIndex(0);
ui->sellBrandComboBox->clear();
}
void Widget::on_sellOkBtn_clicked()
{
//计算剩余库存
int last = ui->sellNumSpinBox->maximum()
- ui->sellNumSpinBox->value();
//销售数量
QSqlQuery query;
query.exec(QString("select sell from brand where name='%1' and factory='%2'").arg(ui->sellBrandComboBox->currentText()).arg(ui->sellFactoryComboBox->currentText()));
query.next();
int sell = query.value(0).toInt()
+ ui->sellNumSpinBox->value();
//事务操作
QSqlDatabase::database().transaction();
//更新brand表的销售和库存字段,更新完成rtn设置为true,表示成功
bool rtn = query.exec(QString("update brand set sell=%1,last=%2 where name='%3' and factory='%4'").arg(sell).arg(last).arg(ui->sellBrandComboBox->currentText()).arg(ui->sellFactoryComboBox->currentText()));
if(rtn)
{
QSqlDatabase::database().commit();//提交
//需要引入头文件QMessageBox
QMessageBox::information(
this,
QString::fromLocal8Bit("提示"),
QString::fromLocal8Bit("购买成功"),
QMessageBox::Ok);
//调用xml操作的函数
writeXML();
showDailyList();
on_sellCancelBtn_clicked();
}
else
{
QSqlDatabase::database().rollback();//回滚
}
ui->sellOkBtn->setEnabled(false);
}
//添加获取时间和日期的函数的代码
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 == Time)
{
return time;
}
else if(type == Date)
{
return date;
}
else
{
return dateAndTime;
}
}
//读取XML文件
bool Widget::docRead()
{
QFile file("data.xml");
if(!file.open(QIODevice::ReadOnly))
{
return false;
}
/*
* setContent()是QDomDocument的一个成员函数
* 用于从一个QIODevice对象中读取XML数据并解析
* 成一个QDomDocument文档树
*/
if(!doc.setContent(&file))
{
file.close();
return false;
}
file.close();
return true;
}
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文档函数,该函数在售车成功的时候被调用
//将销售的汽车信息保存到XML文件中
bool Widget::writeXML()
{
if(docRead())
{
QString currentDate = getDateTime(Date);
//返回根节点元素
QDomElement root = doc.documentElement();
//判断在根节点下是否有子节点
if(!root.hasChildNodes())//没有子节点
{
QDomElement date = doc.createElement(QString::fromLocal8Bit("日期"));
//创建属性
QDomAttr curDate = doc.createAttribute("date");
//设置属性
curDate.setValue(currentDate);
//设置节点属性
date.setAttributeNode(curDate);
//在根节点上添加指定子
没有合适的资源?快使用搜索试试~ 我知道了~
基于Qt的汽车管理系统
共21个文件
o:5个
cpp:5个
h:4个
需积分: 0 9 下载量 66 浏览量
2023-06-30
14:22:56
上传
评论 1
收藏 1.53MB ZIP 举报
温馨提示
基于Qt的汽车管理系统
资源推荐
资源详情
资源评论
收起资源包目录
QT-汽车管理系统.zip (21个子文件)
widget.ui 7KB
ui_widget.h 8KB
widget.cpp 15KB
Makefile 77KB
data.xml 569B
moc_pieview.o 553KB
main.cpp 369B
pieview.h 2KB
moc_widget.cpp 5KB
data.db 5KB
manager.pro 409B
manager 1.4MB
moc_widget.o 349KB
main.o 927KB
manager.pro.user 18KB
pieview.o 816KB
pieview.cpp 4KB
moc_pieview.cpp 3KB
widget.o 976KB
connection.h 4KB
widget.h 2KB
共 21 条
- 1
资源评论
归尘揽星河
- 粉丝: 739
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功