最近不怎么忙,使用qt实现了一个简易的仓库管理物料信息,基本上的功能就是添加库存,删除库存。
添加新物料,还有明确知道出库时间和入库时间,以及操作员是谁(这个我默认写死,需要的可以自己加一下)
先看效果图:

入库可以增加已经存在的物料的数量,出库同入库一样。新增物料可以增加表格中不存在的。右击可以进行删除选中的物料
直接看代码:
.h

#ifndef WIDGET_H
#define WIDGET_H#include <QWidget>
#include <QPushButton>
#include <QLabel>
#include <QTableWidget>
#include <QDialog>
#include <QLineEdit>
#include <QComboBox>
#include <QMenu>
#include <QAction>#pragma execution_character_set("utf-8")class Widget : public QWidget
{Q_OBJECTpublic:Widget(QWidget *parent = nullptr);~Widget();void Init();void Readstock();private:void WriteFile();void SetTableWgtAligment();void CreateMenu();protected slots:void onAddThings();void onAddClicked();void onTableContextMenuClicked();void onInputClicked();void oninputAddClicked();void onOutPutClicked();void onoutPutBtnClicked();void onDeleteClicked();private:QPushButton *m_pInputBtn=nullptr;QPushButton *m_pOutPutBtn=nullptr;QPushButton *m_pAddThingBtn=nullptr;QMenu *m_pMenu = nullptr;QTableWidget *m_pTableWgt=nullptr;QStringList m_NameList;QDialog *m_pAddDialog=nullptr;QLineEdit *m_pAddNameEdit=nullptr;QLineEdit *m_pAddCountEdit=nullptr;QDialog *m_pInputDialog=nullptr;QComboBox *m_pInputComboBox=nullptr;QLineEdit *m_pInputCountEdit=nullptr;QDialog *m_poutputDialog=nullptr;QComboBox *m_poutputComboBox=nullptr;QLineEdit *m_poutputCountEdit=nullptr;
};
#endif // WIDGET_H

.cpp

#include "widget.h"
#include <QVBoxLayout>
#include <QHeaderView>
#include <QIntValidator>
#include <QMessageBox>
#include <QDateTime>
#include <QFile>
#include <QTextStream>
#include <qdebug.h>Widget::Widget(QWidget *parent): QWidget(parent)
{this->resize(800,600);this->setWindowTitle(tr("库存管理"));Init();Readstock();CreateMenu();}Widget::~Widget()
{}void Widget::Init()
{m_pInputBtn=new QPushButton(tr("入库"),this);m_pOutPutBtn=new QPushButton(tr("出库"),this);m_pAddThingBtn=new QPushButton(tr("新增物料"),this);m_pInputBtn->setStyleSheet("background:rgb(60,200,169);color:white;border-radius:3px");m_pOutPutBtn->setStyleSheet("background:rgb(60,200,169);color:white;border-radius:3px");m_pAddThingBtn->setStyleSheet("background:rgb(60,200,169);color:white;border-radius:3px");m_pInputBtn->setFixedSize(120, 28);m_pOutPutBtn->setFixedSize(120, 28);m_pAddThingBtn->setFixedSize(120, 28);m_pTableWgt=new QTableWidget(this);m_pTableWgt->setColumnCount(5);QStringList tableHeadList;tableHeadList<<QString("物料名称")<<QString("数量")<<QString("最后一次出库时间")<<QString("最后一次入库时间")<<QString("操作员");m_pTableWgt->setHorizontalHeaderLabels(tableHeadList);m_pTableWgt->setSelectionBehavior(QAbstractItemView::SelectRows);m_pTableWgt->setContextMenuPolicy(Qt::CustomContextMenu);connect(m_pTableWgt, &QTableWidget::customContextMenuRequested, this, &Widget::onTableContextMenuClicked);m_pTableWgt->setAlternatingRowColors(true);m_pTableWgt->horizontalHeader()->setStretchLastSection(true);m_pTableWgt->setShowGrid(false);m_pTableWgt->horizontalHeader()->setStyleSheet("QHeaderView::section {background-color:qlineargradient(x1:0, y1:0, x2:0, y2:1,\stop:0 rgba(69,133,245,38), stop: 0.5 rgba(69,133,245,38),stop: 0.6 rgba(69,133,245,38), stop:1 rgba(69,133,245,38));color: black;border:1px solid rgba(69,133,245,38)}");     //border:1px solid rgba(153,153,153,51)m_pTableWgt->horizontalHeader()->setFixedHeight(45);m_pTableWgt->setStyleSheet("border:1px solid rgba(153,153,153,51)");m_pTableWgt->setStyleSheet("QTableCornerButton::section{ background:white; }""QTableWidget{alternate-background-color:rgba(69,133,245,38);background:rgba(240,240,240,38)}");QHBoxLayout *btnLayout=new QHBoxLayout(this);QWidget *btnWgt=new QWidget(this);btnLayout->addWidget(m_pInputBtn);btnLayout->addWidget(m_pOutPutBtn);btnLayout->addWidget(m_pAddThingBtn);btnLayout->setMargin(10);btnLayout->setSpacing(5);btnWgt->setLayout(btnLayout);btnWgt->setFixedHeight(40);QVBoxLayout *mainLayout=new QVBoxLayout(this);mainLayout->addWidget(btnWgt);mainLayout->addWidget(m_pTableWgt);mainLayout->setMargin(0);mainLayout->setSpacing(0);this->setLayout(mainLayout);connect(m_pAddThingBtn,&QPushButton::clicked,this,&Widget::onAddThings);connect(m_pInputBtn,&QPushButton::clicked,this,&Widget::onInputClicked);connect(m_pOutPutBtn,&QPushButton::clicked,this,&Widget::onOutPutClicked);}void Widget::Readstock()
{QFile file("material.txt");if (!file.open(QIODevice::ReadOnly | QIODevice::Text))return;while (!file.atEnd()) {QString line = file.readLine();QStringList strList=line.split("|");if(strList.size()>=5){int rowCount=m_pTableWgt->rowCount();m_pTableWgt->setRowCount(rowCount+1);m_NameList.append(strList[0]);QTableWidgetItem *nameItem=new QTableWidgetItem(strList[0]);QTableWidgetItem *countItem=new QTableWidgetItem(strList[1]);QTableWidgetItem *startTimeItem=new QTableWidgetItem(strList[2]);QTableWidgetItem *endTimeItem=new QTableWidgetItem(strList[3]);QTableWidgetItem *userItem=new QTableWidgetItem(strList[4]);m_pTableWgt->setItem(rowCount,0,nameItem);m_pTableWgt->setItem(rowCount,1,countItem);m_pTableWgt->setItem(rowCount,2,startTimeItem);m_pTableWgt->setItem(rowCount,3,endTimeItem);m_pTableWgt->setItem(rowCount,4,userItem);}}file.close();SetTableWgtAligment();
}void Widget::WriteFile()
{QFile file("material.txt");if (!file.open(QIODevice::WriteOnly | QIODevice::Text))return;QTextStream out(&file);out.setCodec("utf-8");int rowCount=m_pTableWgt->rowCount();for(int i=0;i<rowCount;++i){QString strName=m_pTableWgt->item(i,0)->text();QString strCount=m_pTableWgt->item(i,1)->text();QString strStartTime=m_pTableWgt->item(i,2)->text();QString strEndTime=m_pTableWgt->item(i,3)->text();QString user=m_pTableWgt->item(i,4)->text();QString strText=strName+"|"+strCount+"|"+strStartTime+"|"+strEndTime+"|"+user;out << strText<< "\n";}file.close();
}void Widget::SetTableWgtAligment()
{for (int i = 0; i < m_pTableWgt->rowCount(); i++){for (int j = 0; j < m_pTableWgt->columnCount(); j++){m_pTableWgt->item(i,j)->setTextAlignment(Qt::AlignHCenter|Qt::AlignVCenter);}}m_pTableWgt->resizeColumnsToContents();
}void Widget::CreateMenu()
{m_pMenu = new QMenu(this);QAction *deleteAction = new QAction(tr("Delete..."), this);connect(deleteAction,&QAction::triggered,this,&Widget::onDeleteClicked);m_pMenu->addAction(deleteAction);m_pMenu->hide();
}void Widget::onAddThings()
{QFont textFont;textFont.setPointSize(10);textFont.setFamily("Microsoft YaHei");m_pAddDialog=new QDialog();m_pAddDialog->setWindowTitle(tr("新增物料"));m_pAddNameEdit=new QLineEdit(m_pAddDialog);m_pAddCountEdit=new QLineEdit(m_pAddDialog);QLabel *nameLabel=new QLabel(tr("Name"),m_pAddDialog);QLabel *CountLabel=new QLabel(tr("Count"),m_pAddDialog);m_pAddCountEdit->setValidator(new QIntValidator());m_pAddNameEdit->setFixedSize(200,28);m_pAddCountEdit->setFixedSize(200,28);nameLabel->setFixedWidth(100);CountLabel->setFixedWidth(100);m_pAddNameEdit->setFont(textFont);m_pAddCountEdit->setFont(textFont);nameLabel->setFont(textFont);CountLabel->setFont(textFont);QPushButton *addBtn=new QPushButton(tr("增加"),m_pAddDialog);addBtn->setFixedSize(120,28);addBtn->setFont(textFont);QHBoxLayout *addLayout=new QHBoxLayout(m_pAddDialog);addLayout->addWidget(nameLabel);addLayout->addWidget(m_pAddNameEdit);addLayout->addWidget(CountLabel);addLayout->addWidget(m_pAddCountEdit);addLayout->addWidget(addBtn);addLayout->setMargin(20);addLayout->setSpacing(5);m_pAddDialog->setLayout(addLayout);m_pAddDialog->hide();connect(addBtn,&QPushButton::clicked,this,&Widget::onAddClicked);m_pAddDialog->exec();
}void Widget::onAddClicked()
{QString strName=m_pAddNameEdit->text();QString strCount=m_pAddCountEdit->text();if(!m_NameList.contains(strName)){m_NameList.append(strName);int rowCount=m_pTableWgt->rowCount();m_pTableWgt->setRowCount(rowCount+1);QString user="用户";QTableWidgetItem *nameItem=new QTableWidgetItem(strName);QTableWidgetItem *countItem=new QTableWidgetItem(strCount);QDateTime dateTime=QDateTime::currentDateTime();QString strTime=dateTime.toString("yyyy-MM-dd hh:mm:ss");QTableWidgetItem *startTimeItem=new QTableWidgetItem(strTime);QTableWidgetItem *endTimeItem=new QTableWidgetItem(strTime);QTableWidgetItem *userItem=new QTableWidgetItem(user);m_pTableWgt->setItem(rowCount,0,nameItem);m_pTableWgt->setItem(rowCount,1,countItem);m_pTableWgt->setItem(rowCount,2,startTimeItem);m_pTableWgt->setItem(rowCount,3,endTimeItem);m_pTableWgt->setItem(rowCount,4,userItem);SetTableWgtAligment();WriteFile();}else{QMessageBox::information(m_pAddDialog,"提示信息","物料已经存在,请勿重复添加");}}void Widget::onTableContextMenuClicked()
{m_pMenu->setStyleSheet("QMenu{background:white;border: 1px solid rgb(153,153,153);}""QMenu::item:selected{background-color:rgb(69,133,245);}");m_pMenu->move(QCursor::pos().x(), QCursor::pos().y());m_pMenu->show();
}void Widget::onInputClicked()
{QFont textFont;textFont.setPointSize(10);textFont.setFamily("Microsoft YaHei");m_pInputDialog=new QDialog();m_pInputComboBox=new QComboBox(m_pInputDialog);m_pInputCountEdit=new QLineEdit(m_pInputDialog);m_pInputCountEdit->setValidator(new QIntValidator);QPushButton *inputAddBtn=new QPushButton(tr("入库"));QLabel *nameLabel=new QLabel(tr("物料名称"),m_pInputDialog);QLabel *countLabel=new QLabel(tr("入库数量"),m_pInputDialog);m_pInputComboBox->setFixedSize(200,28);m_pInputCountEdit->setFixedSize(200,28);nameLabel->setFixedWidth(100);countLabel->setFixedWidth(100);inputAddBtn->setFixedSize(120,27);inputAddBtn->setFont(textFont);m_pInputComboBox->setFont(textFont);m_pInputCountEdit->setFont(textFont);nameLabel->setFont(textFont);countLabel->setFont(textFont);QGridLayout *inputLayout=new QGridLayout(m_pInputDialog);inputLayout->addWidget(nameLabel,0,0);inputLayout->addWidget(m_pInputComboBox,0,1);inputLayout->addWidget(countLabel,1,0);inputLayout->addWidget(m_pInputCountEdit,1,1);inputLayout->addWidget(inputAddBtn,2,1);inputLayout->setMargin(20);inputLayout->setSpacing(5);m_pInputDialog->setLayout(inputLayout);int rowCount=m_pTableWgt->rowCount();for(int i=0;i<rowCount;++i){m_pInputComboBox->addItem(m_pTableWgt->item(i,0)->text());}m_pInputComboBox->setEditable(true);connect(inputAddBtn,&QPushButton::clicked,this,&Widget::oninputAddClicked);m_pInputDialog->exec();
}void Widget::oninputAddClicked()
{QString strName=m_pInputComboBox->currentText();if(m_NameList.contains(strName)){int Count=m_pInputCountEdit->text().toInt();for(int i=0;i<m_pTableWgt->rowCount();++i){if(m_pTableWgt->item(i,0)->text()==strName){int lastCount=m_pTableWgt->item(i,1)->text().toInt();QString newCount=QString::number(Count+lastCount);QTableWidgetItem *item=new QTableWidgetItem(newCount);QDateTime dateTime=QDateTime::currentDateTime();QString strTime=dateTime.toString("yyyy-MM-dd hh:mm:ss");QTableWidgetItem *startTimeItem=new QTableWidgetItem(strTime);m_pTableWgt->setItem(i,1,item);m_pTableWgt->setItem(i,3,startTimeItem);}}}SetTableWgtAligment();WriteFile();
}void Widget::onOutPutClicked()
{QFont textFont;textFont.setPointSize(10);textFont.setFamily("Microsoft YaHei");m_poutputDialog=new QDialog();m_poutputComboBox=new QComboBox(m_poutputDialog);m_poutputCountEdit=new QLineEdit(m_poutputDialog);m_poutputCountEdit->setValidator(new QIntValidator);QPushButton *outputAddBtn=new QPushButton(tr("出库"));QLabel *nameLabel=new QLabel(tr("物料名称"),m_poutputDialog);QLabel *countLabel=new QLabel(tr("出库数量"),m_poutputDialog);m_poutputComboBox->setFixedSize(200,28);m_poutputCountEdit->setFixedSize(200,28);nameLabel->setFixedWidth(100);countLabel->setFixedWidth(100);outputAddBtn->setFixedSize(120,27);outputAddBtn->setFont(textFont);m_poutputComboBox->setFont(textFont);m_poutputCountEdit->setFont(textFont);nameLabel->setFont(textFont);countLabel->setFont(textFont);QGridLayout *inputLayout=new QGridLayout(m_pInputDialog);inputLayout->addWidget(nameLabel,0,0);inputLayout->addWidget(m_poutputComboBox,0,1);inputLayout->addWidget(countLabel,1,0);inputLayout->addWidget(m_poutputCountEdit,1,1);inputLayout->addWidget(outputAddBtn,2,1);inputLayout->setMargin(20);inputLayout->setSpacing(5);m_poutputDialog->setLayout(inputLayout);int rowCount=m_pTableWgt->rowCount();for(int i=0;i<rowCount;++i){m_poutputComboBox->addItem(m_pTableWgt->item(i,0)->text());}m_poutputComboBox->setEditable(true);connect(outputAddBtn,&QPushButton::clicked,this,&Widget::onoutPutBtnClicked);m_poutputDialog->exec();
}void Widget::onoutPutBtnClicked()
{QString strName=m_poutputComboBox->currentText();if(m_NameList.contains(strName)){int Count=m_poutputCountEdit->text().toInt();for(int i=0;i<m_pTableWgt->rowCount();++i){if(m_pTableWgt->item(i,0)->text()==strName){int lastCount=m_pTableWgt->item(i,1)->text().toInt();QString newCount;if(lastCount-Count<0){newCount=QString::number(0);}else{newCount=QString::number(lastCount-Count);}QTableWidgetItem *item=new QTableWidgetItem(newCount);QDateTime dateTime=QDateTime::currentDateTime();QString strTime=dateTime.toString("yyyy-MM-dd hh:mm:ss");QTableWidgetItem *startTimeItem=new QTableWidgetItem(strTime);m_pTableWgt->setItem(i,1,item);m_pTableWgt->setItem(i,2,startTimeItem);}}}SetTableWgtAligment();WriteFile();
}void Widget::onDeleteClicked()
{QItemSelectionModel *selections = m_pTableWgt->selectionModel();QModelIndexList selected = selections->selectedIndexes();QList<int> rowList;for(int i=0;i<selected.size();++i){int row=selected[i].row();if(!rowList.contains(row)){rowList.append(row);}}for(int i=0;i<rowList.size();++i){m_pTableWgt->removeRow(rowList[rowList.size()-i-1]);}SetTableWgtAligment();WriteFile();
}

qt实现仓库物料管理(小工具)相关推荐

  1. 软件加入使用时间_Mac实用菜单栏管理小工具 Bartender 3 | Mac软件天堂

    小伙伴们,感谢关注「Mac软件天堂」,遇到"XX已损坏"."XX意外退出"."无法确认开发者"等问题记得点击菜单栏的「帮助」解决哦 !同时, ...

  2. 微信小程序|使用小程序制作一个时间管理小工具

    适时而学,适时而息,张弛有度的生活态度才能让我们走得更远.此文使用小程序制作一个日程管理小工具,将时间进行分解以实现有效管理. 开发步骤 一.创建小程序 二.功能实现 2.1.首页 2.2.记录页 3 ...

  3. 防火墙策略管理小工具——网络安全

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言 一.防火墙配置文件 1. 地址集合 2. 服务集合 3. 规则集合 二.防火墙策略管理小工具的开发 1. 主要功能 2 ...

  4. 一个简单的图片管理小工具

    e图片管理是一个用go语言开发,图片管理小工具,支持按文件名搜索,点击图片名称显示图片. 在图片上所展示出内容之外有添加按钮和删除按钮,删除按钮带有二次确认.不需要联网. 文件下载链接 演示视频 ![ ...

  5. Jar批量管理小工具

    文章目录 前言 一.代码结构 二.开源仓库地址 总结 前言 一个Jar包管理的小工具,目前有如下几个作用,后面可以慢慢扩展 1.搜索当前项目路径下的所有jar并复制到某一路径下 2.执行某个目录下的j ...

  6. linux物料管理,SAP-PM 工具管理篇之出入库

    继续昨天的话题 第二节  工具的使用-出入库管理 说到库存的东西很自然就能想到MM模块啦,很显然,咱们这个工具也逃不开.当然,从第一节的内容来看,我得具体问题具体分析来着. 1. CF01 内创建的工 ...

  7. 【远程桌面管理小工具】

    平时项目多 当然需要使用的后台服务器也会多 记性不好 会去翻文档查找账号 密码 甚至有的IP也会忘记 该小工具解决了您的烦恼   一键管理服务器 不再忘记 ------------------> ...

  8. ERP需求调研之仓库物料管理十问十答

    一.库存料件如何进行分类管理? 二.库存单位与换算单位如何处理? 三.物料是否做条码管理? 四.是否需要进行呆滞料的管理? 五.物料管理是否采取编码? 六.当前库存帐的准确率有多少? 七.存货成本如何 ...

  9. 【python】openstack管理小工具(增删查改,批量ping)

    工具介绍: 创建虚拟机会使用多线程队列的方式去控制同时创建的虚拟机数量 如果在创建的虚拟机比较多,会2次创建.第一次会在各个宿主机上创建一台,其余的会再第一次创建成功后创建. 因为第一次创建需要传输镜 ...

  10. 带外管理小工具--IPMITOOL常用操作指令

    一.开关机,重启 1. 查看开关机状态: ipmitool -H (BMC的管理IP地址) -I lanplus -U (BMC登录用户名) -P (BMC 登录用户名的密码) power statu ...

最新文章

  1. linux启动spark命令,在linux上安装spark
  2. 帆软报表等于空的时候不显示_查询结果为空时不显示报表内容
  3. 手机app 服务器 网页同步,手机app接入云服务器
  4. 独家直播!阿里移动前端开源框架Weex揭秘
  5. linux awk菜鸟教程,Linux awk 命令
  6. Visual C++ 6.0静态、动态链接库
  7. redis rua解决库存问题_库存秒杀问题-redis解决方案- 接口限流
  8. URL传递中文、Ajax传递中文,Java如何编码如何解决乱码
  9. SQL Server中授予用户查看对象定义的权限
  10. 【转载】为什么要用黑莓?
  11. 16kb等于多少b_一篇文章讲透MySQL为什么要用B+树实现索引
  12. 获取spring 框架源码方式
  13. 电影mysql设计_电影院数据库设计(案例)
  14. 计算机中1024换算单位,数据换算(计算机中数据的单位换算)
  15. 什么叫工业4.0,这篇接地气的文章终于讲懂了(ZT)
  16. 求多项式浮点java思路,求多项式函数实数根的方法
  17. 微信小程序电商项目商品详情页开发实战之数据绑定与事件应用
  18. HITS(HITS(Hyperlink - Induced Topic Search) ) 算法
  19. 000python路--pycharm使用
  20. 推荐8个良心安全测试工具,快来取走

热门文章

  1. [2018.07.21 T3] Booom
  2. ntp服务器的搭建(内网时钟服务区的搭建)
  3. spring boot 应用设置session path_springboot整合redis实现分布式session
  4. html使表格位于页面的右下方,在HTML表格的右下角显示一个数字以显示齐平
  5. 2008 php mysql_windows 2008配置Nginx+PHP+Mysql
  6. python计算地球重量_python – 在Tensorflow中使用Earth Mover的Distance作为损失函数
  7. 函数不可以直接调用其他函数内部定义的变量_JavaScript(4) 函数
  8. mysql怎么查看记录时间戳_mysql TIMESTAMP(时间戳)详解——查询最近一段时间操作的记录...
  9. Java集合类源码解析:Vector
  10. 博客主之自我介绍(不长,随便瞅瞅)