MainWindow.h
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<QDebug>
#include<QSqlDatabase>
#include<QSqlError>
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();//初始化
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_pushButton_2_clicked();
void on_pushButton_clicked();
private:
Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
MainWindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QMessageBox>
#include<QSqlQuery>
#include<QSqlQueryModel>
#include"domxml.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
//默认界面
on_actionCar_triggered();
//打开数据库
connectDB();
//初始化数据
initData();
}
MainWindow::~MainWindow()
{
delete ui;
}
//连接数据库
void MainWindow::connectDB()
{
//打印Qt支持的数据库驱动
qDebug()<<QSqlDatabase::drivers();
//添加MySql数据库
QSqlDatabase db= QSqlDatabase::addDatabase("QMYSQL");
//链接数据库
db.setHostName("127.0.0.1");//数据库服务器IP
db.setUserName("root");//数据库用户名
db.setPassword("root");//数据库密码
db.setDatabaseName("dbtest1");//使用那个数据库
//打开数据库
if(db.open()==false)
{
QMessageBox::information(this,"失败",db.lastError().text());
return;
}
}
//初始化数据
void MainWindow::initData()
{
//新建模型
QSqlQueryModel* queryModel=new QSqlQueryModel(this);
//sql语句
queryModel->setQuery("select name from factory");
ui->comboBoxFACTORY->setModel(queryModel);
//金额不能选
ui->lineEditTOTAL->setEnabled(false);
//数量不能选
ui->spinBox->setEnabled(false);
}
//车辆管理菜单
void MainWindow::on_actionCar_triggered()
{
//车辆管理页面
ui->stackedWidget->setCurrentIndex(0);
ui->label->setText("车辆管理");
}
//销售统计菜单
void MainWindow::on_actionCalc_triggered()
{
//切换到销售统计
ui->stackedWidget->setCurrentWidget(ui->calc);
ui->label->setText("销售统计");
}
//厂家下拉框
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->pushButton_2->setEnabled(false);
}else
{
ui->spinBox->setEnabled(true);
//第一种
//类似厂家下拉框操作
//QSqlQueryModel* queryModel=new QSqlQueryModel(this);
//queryModel->setQuery(QString("select name from brand where '%1'=factory").arg(arg1));
//ui->comboBoxBRAND->setModel(queryModel);
//第二种
//先清空
ui->comboBoxBRAND->clear();
QSqlQuery query;
query.exec(QString("select name from brand where '%1'=factory").arg(arg1));
//获取内容
while(query.next())
{
ui->comboBoxBRAND->addItem(query.value("name").toString());
}
}
}
//品牌下拉框
void MainWindow::on_comboBoxBRAND_currentIndexChanged(const QString &arg1)
{
//厂家+品牌下拉框操作
QSqlQuery query;
query.exec(QString("select price,last from brand where '%1'=name and factory='%2'").arg(arg1).arg(ui->comboBoxFACTORY->currentText()));
//即便一条记录,也用while
while(query.next())
{
//报价
int price=query.value(0).toInt();
//剩余数
int last=query.value(1).toInt();
//报价
ui->lineEditPRICE->setText(QString::number(price));
//剩余数
ui->labelLAST->setText(QString::number(last));
}
}
//数量选择框槽函数
void MainWindow::on_spinBox_valueChanged(int arg1)
{
//取出数据库剩余数量
QSqlQuery query;
query.exec(QString("select sum,last from brand where '%1'=name and factory='%2'").arg(ui->comboBoxBRAND->currentText()).arg(ui->comboBoxFACTORY->currentText()));
if(0 ==arg1)
{
ui->pushButton_2->setEnabled(false);
}else
{
ui->pushButton_2->setEnabled(true);
}
int last=0;
while(query.next())
{
last=query.value("last").toInt();
}
int num=last-arg1;
//spin最大数字,更新剩余数量
if(arg1>last)
{
ui->spinBox->setValue(last);
QMessageBox::warning(this,"提示","数字过大");
}
else
{
ui->labelLAST->setText(QString::number(num));
//报价设置
int price=arg1* ui->lineEditPRICE->text().toInt();
ui->lineEditTOTAL->setText(QString::number(price));
}
}
//确定
void MainWindow::on_pushButton_2_clicked()
{
//销售数据
int num=ui->spinBox->value();
//剩余
int last=ui->labelLAST->text().toInt();
//获取数据库销量
QSqlQuery query;
query.exec(QString("select sell from brand where '%1'=name and factory='%2'").arg(ui->comboBoxBRAND->currentText()).arg(ui->comboBoxFACTORY->currentText()));
int sell;
while(query.next())
{
sell=query.value("sell").toInt();
}
//更新数据库 剩余数量 销售总量
sell+=num;
query.exec(QString("update brand set sell=%1,last=%2 where '%3'=name and factory='%4'")
.arg(sell)
.arg(last)
.arg(ui->comboBoxBRAND->currentText())
.arg(ui->comboBoxFACTORY->currentText()));
//确定后初始化
ui->pushButton_2->setEnabled(false);
on_pushButton_clicked();
}
//取消
void MainWindow::on_pushButton_clicked()
{
on_comboBoxFACTORY_currentIndexChanged("请选择厂家");
ui->comboBoxFACTORY->setCurrentText("请选择厂家");
ui->spinBox->setEnabled(false);
}