#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QListWidgetItem>
#include <QDateTime>
#include <QDebug>
#include <QSqlError>
#include <QMessageBox>
#include <QSqlQuery>
#include <QDate>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
contents_create();
// 物品增减
query_model_add = new QSqlQueryModel(this);
query_model_add->setQuery("select id, name, type, state, position, price, supplier, owner, date_enter, date_expiration from goods");
query_model_add->setHeaderData(0, Qt::Horizontal, tr("ID"));
query_model_add->setHeaderData(1, Qt::Horizontal, tr("名称"));
query_model_add->setHeaderData(2, Qt::Horizontal, tr("种类"));
query_model_add->setHeaderData(3, Qt::Horizontal, tr("状态"));
query_model_add->setHeaderData(4, Qt::Horizontal, tr("位置"));
query_model_add->setHeaderData(5, Qt::Horizontal, tr("价格"));
query_model_add->setHeaderData(6, Qt::Horizontal, tr("供应商"));
query_model_add->setHeaderData(7, Qt::Horizontal, tr("拥有者"));
query_model_add->setHeaderData(8, Qt::Horizontal, tr("入库日期"));
query_model_add->setHeaderData(9, Qt::Horizontal, tr("有效日期"));
// query_model_add->setHeaderData(10, Qt::Horizontal, tr("变更日期"));
// query_model_add->setHeaderData(11, Qt::Horizontal, tr("备注"));
ui->tableView_add->setModel(query_model_add);
ui->tableView_add->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// 物品总览
QStringList labels;
labels << "物品ID" << "名称" << "种类" << "状态" << "所在位置";
ui->tableWidget->setColumnCount(5);
ui->tableWidget->setHorizontalHeaderLabels(labels);
ui->tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
// 物品状态更改
query_model_state = new QSqlQueryModel(this);
// query_model_state->setQuery("select * from goods");
// query_model_state->setHeaderData(0, Qt::Horizontal, tr("ID"));
// query_model_state->setHeaderData(1, Qt::Horizontal, tr("名称"));
// query_model_state->setHeaderData(2, Qt::Horizontal, tr("种类"));
// query_model_state->setHeaderData(3, Qt::Horizontal, tr("状态"));
// query_model_state->setHeaderData(4, Qt::Horizontal, tr("位置"));
// query_model_state->setHeaderData(5, Qt::Horizontal, tr("价格"));
// query_model_state->setHeaderData(6, Qt::Horizontal, tr("供应商"));
// query_model_state->setHeaderData(7, Qt::Horizontal, tr("拥有者"));
// query_model_state->setHeaderData(8, Qt::Horizontal, tr("入库日期"));
// query_model_state->setHeaderData(9, Qt::Horizontal, tr("有效日期"));
// query_model_state->setHeaderData(10, Qt::Horizontal, tr("备注"));
ui->tableView_state->setModel(query_model_state);
ui->tableView_state->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
}
MainWindow::~MainWindow()
{
delete ui;
}
// 左侧菜单栏生成
void MainWindow::contents_create()
{
QFont font;
//创建QListWidget
// this->ui->lwt_contents = new QListWidget(this);
this->ui->lwt_contents->setViewMode(QListView::IconMode);
this->ui->lwt_contents->setIconSize(QSize(90, 60));
this->ui->lwt_contents->setMovement(QListView::Static);
this->ui->lwt_contents->setMaximumWidth(110); //75
this->ui->lwt_contents->setSpacing(5);
connect(this->ui->lwt_contents, SIGNAL(currentRowChanged(int)), this, SLOT(slot_content_changed(int)));
//创建QListWidgetItem子项
font.setPointSize(12); //设置字体大小
QListWidgetItem *lwti_add = new QListWidgetItem(this->ui->lwt_contents);
lwti_add->setIcon(QIcon(tr(":/images/debug.jpg")));
lwti_add->setFont(font); //设置字体
lwti_add->setText(tr("物品增加"));
lwti_add->setTextAlignment(Qt::AlignCenter);
lwti_add->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
QListWidgetItem *lwti_overview = new QListWidgetItem(this->ui->lwt_contents);
lwti_overview->setIcon(QIcon(tr(":/images/setting.png")));
lwti_overview->setFont(font);
lwti_overview->setText(tr("物品总览"));
lwti_overview->setTextAlignment(Qt::AlignCenter);
lwti_overview->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
QListWidgetItem *lwti_state = new QListWidgetItem(this->ui->lwt_contents);
lwti_state->setIcon(QIcon(tr(":/images/firewareUpdate.jpg")));
lwti_state->setFont(font);
lwti_state->setText(tr("物品状态"));
lwti_state->setTextAlignment(Qt::AlignCenter);
lwti_state->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
QListWidgetItem *lwti_illustration = new QListWidgetItem(this->ui->lwt_contents);
lwti_illustration->setIcon(QIcon(tr(":/images/update.png")));
lwti_illustration->setFont(font);
lwti_illustration->setText(tr("软件简介"));
lwti_illustration->setTextAlignment(Qt::AlignCenter);
lwti_illustration->setFlags(Qt::ItemIsSelectable|Qt::ItemIsEnabled);
}
// 界面切换
void MainWindow::slot_content_changed(int row)
{
if(row > this->ui->stackedWidget->count())
{
return;
}
this->ui->stackedWidget->setCurrentIndex(row);
// 将生产日期设置为当前时间
if(row == 0) {
ui->ldt_date_enter->setText(getDateTime(Date));
// ui->ldt_date_expiration->setText(getDateTime(Date));
}
else if(row == 1) {
// "物品ID" << "名称" << "种类" << "状态" << "所在位置"
QSqlQuery query;
query.exec(QString("select id, name, type, state, position from goods"));
query_show(query);
}
}
// 获取当前的日期或者时间
QString MainWindow::getDateTime(MainWindow::DateTimeType type)
{
QDateTime datetime = QDateTime::currentDateTime();
QString date = datetime.toString("yyyy-MM-dd");
QString time = datetime.toString("hh:mm:ss");
QString dateAndTime = datetime.toString("yyyy-MM-dd dddd hh:mm");
if(type == Date)
return date;
else if(type == Time)
return time;
else
return dateAndTime;
}
void MainWindow::on_btn_add_clicked()
{
QString id, name, type, state, position, supplier, owner, date_enter, date_expiration;
double price;
int index1, index2;
id = ui->ldt_id->text();
name = ui->ldt_name->text();
index1 = ui->cmb_type->currentIndex();
if(index1 == 0) {
type = tr("工具");
}
else if(index1 == 1) {
type = tr("仪器");
}
else if(index1 == 2) {
type = tr("消耗品");
}
index2 = ui->cmb_state->currentIndex();
if(index2 == 0) {
state = tr("库房");
}
else if(index2 == 1) {
state = tr("借出");
}
else if(index2 == 2) {
state = tr("损坏");
}
position = ui->ldt_position->text();
price = ui->ldt_price->text().toDouble();
supplier = ui->ldt_supplier->text();
owner = ui->ldt_owner->text();
date_enter = ui->ldt_date_enter->text();
date_expiration = ui->ldt_date_expiration->text();
if(index1 <= 1) {
query_model_add->setQuery(QString("insert into goods values(%1, '%2', '%3', '%4', '%5', %6, '%7', '%8', '%9', default, default, default)")\
.arg(id).arg(name).arg(type).arg(state).arg(position).arg(price).arg(supplier).arg(owner).arg(date_enter));
}
else {
query_model_add->setQuery(QString("insert into goods values(%1, '%2', '%3', '%4', '%5', %6, '%7', '%8', '%9', '%10', default, default)")\
.arg(id).arg(name).arg(type).arg(state).arg(position).arg(price).arg(supplier).arg(owner).arg(date_enter).arg(date_expiration));
}
query_model_add->setQuery("select id, name, type, state, position, price, supplier, owner, date_enter, date_expiration from goods");
if (query_model_add->lastError().isValid()) // 执行SQL语句有问题时,将错误输出 (�