您好,欢迎访问代理记账网站
  • 价格透明
  • 信息保密
  • 进度掌控
  • 售后无忧

基于qt的库存管理系统(大作业

学了一学期的cpp,大作业被人安排了个这个什么管理系统,我就想学点qt来写,不至于控制台那么丑。。。还能加分。

一些招聘cpp开发工程师的岗位的话如果你接触过qt会加分,也算是对学了一学期cpp有个交代吧,

虽然挺拉跨的。。。。里面用了点容器和文件IO,书里没有的。

csdn里面有那种连接数据库的,我还没学。。。只能txt将就了。。。确实将就了dian

接下来就是数据结构什么的,自己也想再学点STL。

但是和别人的差距还是太大了唉

代码在这了,想要随便拿,有些我也没写好,属实拉跨,大家看着各自学校的需求自己增删改查吧,毕竟你也是学了cpp的对不对

有问题可以指出可以问,反正我学编程不到一年,底子差得很,变量名称啊驼峰命名啊什么的还是没写上,想到啥写啥,自己看得懂就行略略略。

一些写法可能还是比较繁琐,我感觉我的电脑想要打死我了。。。自己优化吧,比我牛逼的人千千万。

代码:

破,pro文件:
 

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS


# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    bianji.cpp \
    chaxun.cpp \
    jinchu.cpp \
    main.cpp \
    shanchu.cpp \
    tianjia.cpp \
    widget.cpp \
    yesorno.cpp \

HEADERS += \
    bianji.h \
    chaxun.h \
    jinchu.h \
    shanchu.h \
    tianjia.h \
    widget.h \
    yesorno.h

FORMS += \
    bianji.ui \
    chaxun.ui \
    jinchu.ui \
    shanchu.ui \
    tianjia.ui \
    widget.ui \
    yesorno.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target

RESOURCES += \
    logo.qrc

编辑的头文件bianji.h:

#ifndef BIANJI_H
#define BIANJI_H

#include <QWidget>
#include<yesorno.h>
#include<QMessageBox>
#include<QString>
#include<QList>
#include<QTextStream>
#include<QDialog>
#include<QStandardItem>
#include<QStandardItemModel>


namespace Ui {
class bianji;
}

class bianji : public QWidget
{
    Q_OBJECT

public:
    explicit bianji(QWidget *parent = nullptr);
    ~bianji();
    void sendslot();
    void change1();
    yesorno x1;
    int readfile();
    void initialization();
    void search(QString subs);
    void disPlay(int row,QStringList subs);
    void selsct();
    void change();
    void move(QString x);
    void move2(QString y);
signals:
void back();//


private:
    Ui::bianji *ui;
    QList<QString> things;
    QList<QString> things2;
    QStandardItemModel *model;


};

#endif // BIANJI_H

bianji.cpp

#include "bianji.h"
#include "ui_bianji.h"
#include<yesorno.h>
#include<QString>
#include<QTextStream>
#include<QString>
#include<QIODevice>
#include<QStringList>
#include<QStandardItem>
#include<QStandardItemModel>
#include<QtMath>
#include<QCheckBox>
#include<QAbstractItemView>
#include<QDialog>
#include<QList>
#include<QFile>
#include<QDebug>

bianji::bianji(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::bianji)
{
    setWindowTitle("编辑");
    setWindowIcon(QIcon(":/man black.ico"));
    this->setAutoFillBackground(true); // 这句要加上, 否则可能显示不出背景图.
       QPalette palette = this->palette();
       palette.setBrush(QPalette::Window,
               QBrush(QPixmap("C:\\Users\\17590\\Desktop\\pictures\\wallhaven-rddgwm.jpg").scaled(// 缩放背景图.
                   this->size(),
                   Qt::IgnoreAspectRatio,
                   Qt::SmoothTransformation)));             // 使用平滑的缩放方式
       this->setPalette(palette);
    ui->setupUi(this);
    ui->a->show();
    ui->a->setText("back");
    ui->a->setParent(this);
    ui->pushButton->setParent(this);
    ui->pushButton->show();
    ui->name->show();
    ui->place->show();
    ui->price->show();
    ui->bianhao->show();
    ui->number->show();
    ui->label->show();
    ui->label_2->show();
    ui->label_3->show();
    ui->label_4->show();
    ui->label_5->show();
    ui->label_6->show();
    ui->label_7->show();
    ui->pushButton_2->show();
    connect(ui->a,&QPushButton::clicked,this,&bianji::change1);//yesorno选项出现
    connect(ui->pushButton,&QPushButton::clicked,this,&bianji::selsct);
    connect(ui->pushButton_2,&QPushButton::clicked,this,&bianji::change);
//

    initialization();
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);




}
void bianji::change1()//yesorno选项出现
{
  x1.show();
}
void bianji::initialization()
{   this->model= new QStandardItemModel;

    this->model->setHorizontalHeaderItem(0,new QStandardItem("商品名称."));
    this->model->setHorizontalHeaderItem(1,new QStandardItem("商品价格."));
    this->model->setHorizontalHeaderItem(2,new QStandardItem("商品产地."));
    this->model->setHorizontalHeaderItem(3,new QStandardItem("商品编号."));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("商品数量."));;
    this->ui->tableView->setModel(model);
    this->ui->tableView->setColumnWidth(0,180);
    this->ui->tableView->setColumnWidth(1,180);
    this->ui->tableView->setColumnWidth(2,180);
    this->ui->tableView->setColumnWidth(3,180);
    this->ui->tableView->setColumnWidth(4,180);

}

bianji::~bianji()
{
    delete ui;
}
int bianji::readfile()
{
    things.clear();
    things2.clear();
    QFile file2("thing2.txt");
    if(!file2.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        return -1;
    }
    QTextStream in(&file2);
    while(!in.atEnd())
    {
        QString line=in.readLine();
        things2.append(line);
    }
    file2.close();
    QFile file("things.txt");
    if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        return -1;
    }
    QTextStream in2(&file);
    while(!in2.atEnd())//如果文件还没到结尾
    {
        QString line=in2.readLine();
        things.append(line);

    }
    file.close();
    return 0;

}
void bianji::search( QString cnt)
{
    readfile();
    int i=0;
    int row=0;
    for(i=0;i<things.length();i++)
    {
      QString line=things.at(i);
      line =line.trimmed();
      QStringList subs=line.split(" ");
      subs.at(0);
          if(cnt==subs.at(0))//
          {
              disPlay(row++,subs);
              break;
          }
      }
}

void  bianji::disPlay(int row, QStringList subs)//
{
    int i=0;
    for(i=0;i<subs.length();i++)
    {
        this->model->setItem(row,i,new QStandardItem(subs.at(i)));
    }
}

void bianji::selsct()
{
    QString cnt=this->ui->lineEdit->text();
    search(cnt);
}

void bianji::change()
{
    QString name=ui->name->text();
    QString price=ui->place->text();
    QString place =ui->price->text();
    QString bianhao=ui->bianhao->text();
    QString number=ui->number->text();
    QString messagebox="商品名字:"+name+"商品价格:"+price+"商品产地:"+place+
            "商品编号"+bianhao+"商品数量:"+number;
   QString info=name+" "+place+" "+price+" "+bianhao+" "+number+" ";

    if(readfile()==-1)
    {
         this->close();
         QMessageBox::critical(this,"wrong","File reading failed, information not deleted","ok");
         return;
    }
    else
 {
    move(info);//新的那一行
    int i=0;
    int j=0;
    for (j=0;j<things2.length();j++)
    {
      QString line2=things2.at(j);
      QStringList subs2=line2.split(" ");
      if(name==subs2.at(0))
      {
          for (int y=0;y<things2.length();y++)
          {
              if(things2.at(y)==line2){continue;}
              move(things2.at(y));
          }
      }

    }
    for(i=0;i<things.length();i++)
    {
      QString line=things.at(i);
      //line =line.trimmed();
      QStringList subs=line.split(" ");
      qDebug()<<"1";
      if(name==subs.at(0))
      {
          //things.at(i)这个就是需要修改的那一行
          for(int x=0;x<things.length();x++)
          {
            if(things.at(x)==line){continue;}
            move(things.at(x));//旧的那一行

          }
          QMessageBox::information(this,"提示","修改成功!","ok","no");
          break;
      }
    }

   }
    QFile mFile("things.txt");
    QFile mFiles("thing2.txt");
    QFile mFiles2("thing2_temp.txt");
    QFile mfile2("things_temp.txt");
    if(mFiles.exists())
    {
        mFiles.remove();
        mFiles2.rename("thing2.txt");

    }
    if(mFile.exists())
    {
        mFile.remove();
        mfile2.rename("things.txt");
    }
}

void bianji::move(QString x)
{
    this->ui->name->clear();
    this->ui->place->clear();
    this->ui->price->clear();
    this->ui->number->clear();
    this->ui->bianhao->clear();
    QFile mfile2("things_temp.txt");
    QFile mfile3("thing2_temp.txt");
    if(!mfile3.open(QIODevice::Append|QIODevice::Text))
    {
        QMessageBox::critical(this,"错误","文件打开失败,信息没有写入","确认");
        return;
    }
    QTextStream out2(&mfile3);
    out2<<x<<"\n";
    mfile3.close();
    if(!mfile2.open(QIODevice::Append|QIODevice::Text))
    {
        QMessageBox::critical(this,"错误","文件打开失败,信息没有写入","确认");
        return;
    }
    QTextStream out(&mfile2);
    out<<x<<"\n";
    mfile2.close();

}

查询chaxun.h:

#ifndef CHAXUN_H
#define CHAXUN_H

#include <QWidget>
#include<yesorno.h>
#include<QStandardItemModel>
#include<QList>
#include<QString>

namespace Ui {
class chaxun;
}

class chaxun : public QWidget
{
    Q_OBJECT

public:
    explicit chaxun(QWidget *parent = nullptr);
    ~chaxun();
    void change2();
    yesorno x2;
    void initialization();
    void show2();
    int readfile();
    void display(int row,QStringList subs);
    void clearr();


private:
    Ui::chaxun *ui;
    QStandardItemModel *model;
    QList<QString> things_out;


};

#endif // CHAXUN_H

chaxun.cpp:

#include "chaxun.h"
#include "ui_chaxun.h"
#include<yesorno.h>
#include<QString>
#include<QTextStream>
#include<QString>
#include<QIODevice>
#include<QStringList>
#include<QStandardItem>
#include<QStandardItemModel>
#include<QtMath>
#include<QCheckBox>
#include<QAbstractItemView>
#include<QDialog>
#include<QList>
#include<QFile>
#include<QDebug>
#include<QMessageBox>
chaxun::chaxun(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::chaxun)
{
    setWindowTitle("进出货查询");
    setWindowIcon(QIcon(":/man black.ico"));
    ui->setupUi(this);
    this->setAutoFillBackground(true); // 这句要加上, 否则可能显示不出背景图.
       QPalette palette = this->palette();
       palette.setBrush(QPalette::Window,
               QBrush(QPixmap("C:\\Users\\17590\\Desktop\\pictures\\wallhaven-rddgwm.jpg").scaled(// 缩放背景图.
                   this->size(),
                   Qt::IgnoreAspectRatio,
                   Qt::SmoothTransformation)));             // 使用平滑的缩放方式
       this->setPalette(palette);
       ui->b->setParent(this);
       ui->b->setText("back");
       ui->b->show();
       ui->label->show();
       ui->lineEdit->show();
       ui->tableView->show();
       ui->pushButton->show();
       connect(ui->b,&QPushButton::clicked,this,&chaxun::change2);
       connect(ui->pushButton,&QPushButton::clicked,this,&chaxun::show2);
       initialization();
       ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
       ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
//
}
void chaxun::initialization()
{
    this->model= new QStandardItemModel;
    this->model->setHorizontalHeaderItem(0,new QStandardItem("商品."));
    this->model->setHorizontalHeaderItem(1,new QStandardItem("进出货类型."));
    this->model->setHorizontalHeaderItem(2,new QStandardItem("进货日."));
    this->model->setHorizontalHeaderItem(3,new QStandardItem("进货月份."));
    this->model->setHorizontalHeaderItem(4,new QStandardItem("进货日期."));
    this->model->setHorizontalHeaderItem(5,new QStandardItem("进货准确时间."));
    this->model->setHorizontalHeaderItem(6,new QStandardItem("进货年份."));
    this->model->setHorizontalHeaderItem(7,new QStandardItem("进出货数量."));//
    this->model->setHorizontalHeaderItem(8,new QStandardItem("现有数量."));
    this->model->setHorizontalHeaderItem(9,new QStandardItem("进出货总金额."));
    this->ui->tableView->setModel(model);
    this->ui->tableView->setColumnWidth(0,180);
    this->ui->tableView->setColumnWidth(1,180);
    this->ui->tableView->setColumnWidth(2,180);
    this->ui->tableView->setColumnWidth(3,180);
    this->ui->tableView->setColumnWidth(4,180);
    this->ui->tableView->setColumnWidth(5,180);
    this->ui->tableView->setColumnWidth(6,180);
    this->ui->tableView->setColumnWidth(7,180);
    this->ui->tableView->setColumnWidth(8,180);
    this->ui->tableView->setColumnWidth(9,180);


}
int chaxun::readfile()
{
    things_out.clear();
    QFile m("thing3.txt");
    if(!m.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        return -1;
    }
    QTextStream in(&m);
    while (!in.atEnd())
    {
        QString line=in.readLine();
        things_out.append(line);
    }
    m.close();
    return 0;
}
void chaxun::show2()
{
    QString name=ui->lineEdit->text();
    if(readfile()==-1)
    {

        //this->close();
        QMessageBox::critical(this,"wrong","File reading failed, information not deleted","ok");
    }
    else
    {//
        int row=0;
        for (int j=0;j<things_out.length();j++)
        {
            QString line2=things_out.at(j);
            line2=line2.trimmed();
            QStringList subs2=line2.split(" ");

            if(name==subs2.at(0))//找到这个商品
            {
                for(int i=0;i<things_out.length();i++)
                {
                    if(things_out.at(i)==line2)
                    {
                        QString x=things_out.at(j);
                        QStringList y=x.split(" ");
                        display(row,y);
                        row+=1;
                    }
                }
            }
            else if(j==things_out.length()-1)
            {
                ui->lineEdit->clear();
                QMessageBox::information(this,"提示","查无此商品","ok","no");
                return;
            }


         }
    }//

}
void chaxun::display(int row, QStringList subs)
{
    int i=0;
    for(i=0;i<subs.length();i++)
    {
        this->model->setItem(row,i,new QStandardItem(subs.at(i)));
    }
}
void chaxun::change2()
{
    x2.show();
}

chaxun::~chaxun()
{
    delete ui;
}

进出货jinchu.h

#ifndef JINCHU_H
#define JINCHU_H

#include <QWidget>
#include<yesorno.h>
#include<QList>
#include<QString>

namespace Ui {
class jinchu;
}

class jinchu : public QWidget
{
    Q_OBJECT

public:
    explicit jinchu(QWidget *parent = nullptr);
    ~jinchu();
    void change();
    yesorno x3;
    void append();
    void out();
    int readfile();
    void move(QString x);
    void move2(QString y);
    void move3(QString z);

private:
    Ui::jinchu *ui;
    QList<QString> things;
};

#endif // JINCHU_H

jinchu.cpp:

#include "jinchu.h"
#include "ui_jinchu.h"
#include<QString>
#include<QTextStream>
#include<QMessageBox>
#include<QStringList>
#include<QFile>
#include<QDebug>
#include<QTime>
/*进货日期、生产厂家、进货价、数量*/
jinchu::jinchu(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::jinchu)
{
    setWindowTitle("进出货");
    setWindowIcon(QIcon(":/man black.ico"));
    QPalette palette = this->palette();
    palette.setBrush(QPalette::Window,
            QBrush(QPixmap("C:\\Users\\17590\\Desktop\\pictures\\wallhaven-rddgwm.jpg").scaled(// 缩放背景图.
                this->size(),
                Qt::IgnoreAspectRatio,
                Qt::SmoothTransformation)));             // 使用平滑的缩放方式
    this->setPalette(palette);
    ui->setupUi(this);
    ui->c->setParent(this);
    ui->c->show();
    ui->label->show();
    ui->label_2->show();
    ui->label_3->show();
    ui->label_4->show();
    ui->label_5->show();
    ui->label_6->show();
    ui->label_7->show();
    ui->lineEdit->show();
    ui->lineEdit_2->show();
    ui->lineEdit_3->show();
    ui->lineEdit_4->show();
    ui->lineEdit_6->show();
    ui->lineEdit_7->show();
    ui->yes->show();
    ui->yes->show();
    connect(ui->c,&QPushButton::clicked,this,&jinchu::change);
    connect(ui->yes,&QPushButton::clicked,this,&jinchu::append);
    connect(ui->yes_2,&QPushButton::clicked,this,&jinchu::out);

}
int  jinchu::readfile()
{
    things.clear();
    QFile file("thing2.txt");
    if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        return -1;
    }
    QTextStream in(&file);
    while (!in.atEnd())
    {
        QString line=in.readLine();
        things.append(line);
    }
    file.close();
    return 0;
}
void jinchu::append()
{

    QString name=ui->lineEdit->text();
    QString factory=ui->lineEdit_2->text();
    QString in_price=ui->lineEdit_3->text();
    QString innumber=ui->lineEdit_4->text();
    if(readfile()==-1)
    {
        this->close();
        QMessageBox::critical(this,"wrong","File reading failed, information not deleted","ok");
        return;
    }
    else
    {
        for(int i=0;i<things.length();i++)
        {
            QString line1=things.at(i);
            line1=line1.trimmed();
            QStringList sub=line1.split(" ");//算出sub的列表
            if(name==sub.at(0))
            {
               for(int j=0;j<things.length();j++)
              {
                if(things.at(j)==things.at(i))
                {

                    if(sub.length()>5)
                    {
                        QMessageBox::information(this,"提示!","该商品之前已经进货过一次!","ok");
                        int totalNumber=innumber.toInt()+sub.at(4).toInt();
                        QString total=QString::number(totalNumber);
                        QDateTime time = QDateTime::currentDateTime();
                        QString time2= time.toString();
                        QString info=" "+factory+" "+in_price+" "+total+" "+time2;
                        QString before=sub.at(0)+" "+sub.at(1)+" "+sub.at(2)+" "+sub.at(3)+" "+sub.at(4);
                        QString nows=before+info;
                        move(nows);
                          

                    }
                    else
                    {

                    int totalNumber=innumber.toInt()+sub.at(4).toInt();
                    QString total=QString::number(totalNumber);
                    QDateTime time = QDateTime::currentDateTime();
                    int x=innumber.toInt()*in_price.toUInt();
                    QString X=QString::number(x);
                    QString time2= time.toString();
                    QString in_out2=name+" "+"进货"+" "+time2+" "+innumber+" "+total+" "+X;
                    move3(in_out2);
                    QString info=" "+factory+" "+in_price+" "+total+" "+time2;
                    QString after=line1+info;
                    move(after);
                    continue;
                    }
                }
                move(things.at(j));
               }
               QMessageBox::information(this,"提示","修改成功!","ok");
               break;
            }
            else if(i==things.length()-1)
            {
                ui->lineEdit->clear();
                ui->lineEdit_2->clear();
                ui->lineEdit_3->clear();
                ui->lineEdit_4->clear();
                QMessageBox::information(this,"提示","查无此商品","ok","no");
                return;
            }
        }


    }
    QFile file("thing2.txt");
    QFile file1("thing2_ttemp.txt");
    if(file.exists())
    {
        file.remove();
        file1.rename("thing2.txt");
    }
}
void jinchu::move(QString x)
{
    this->ui->lineEdit->clear();
    this->ui->lineEdit_2->clear();
    this->ui->lineEdit_3->clear();
    this->ui->lineEdit_4->clear();
    QFile file2("thing2_ttemp.txt");
    if(!file2.open(QIODevice::Append|QIODevice::Text))
    {
        QMessageBox::critical(this,"错误","文件打开失败,信息未写入","确认");
        return;
    }
    QTextStream out(&file2);
    out<<x<<"\n";
    file2.close();
}
void jinchu::out()
{
    QString out_number=ui->lineEdit_7->text();
    QString name=ui->lineEdit_6->text();
    if(readfile()==-1)
    {
        this->close();
        QMessageBox::critical(this,"wrong","File reading failed, information not deleted","ok");
        return;
    }
    else
    {
        for(int x=0;x<things.length();x++)
        {
            QString line1=things.at(x);
            line1=line1.trimmed();
            QStringList sub=line1.split(" ");         //算出sub的列表

            if(name==sub.at(0))//找到所需的商品
            {
                if(sub.length()<9)
                {
                    QMessageBox::information(this,"提示","这个商品还没进货!","ok","no");
                    ui->lineEdit_6->clear();
                    ui->lineEdit_7->clear();
                    return;
                }



               for(int y=0;y<things.length();y++)
              {

                if(things.at(y)==things.at(x))//找到那一行。
                {
                    if(sub.at(7).toInt()<out_number.toInt())
                    {
                        ui->lineEdit_6->clear();
                        ui->lineEdit_7->clear();
                        QMessageBox::information(this,"提示","库存不足!,请重新输入","ok","no");
                        return;
                    }

                     int temp=sub.at(7).toInt()-out_number.toUInt();
                     //qDebug()<<sub.length();
                      QString temp2=QString::number(temp);
                      int y=out_number.toInt()*sub.at(6).toInt();
                      QString Y=QString::number(y);
                      QDateTime Time = QDateTime::currentDateTime();
                      QString Time2= Time.toString();
                      QString in_out2=name+" "+"出货"+" "+Time2+" "+out_number+" "+temp2+" "+Y;
                      move3(in_out2);
                      sub.replace(8,temp2);
                      QString finals=sub.join(" ");
                      move2(finals);
                      continue;
                }
                move2(things.at(y));
               }
               QMessageBox::information(this,"提示","修改成功!","ok","no");
               break;

           }
            else if(x==things.length()-1)
            {
                ui->lineEdit_6->clear();
                ui->lineEdit_7->clear();
                QMessageBox::information(this,"提示","查无此商品","ok","no");
                return;
            }

        }
    }
    QFile x("thing2.txt");
    QFile y("thing2p__temp.txt");
    if(x.exists())
    {
        x.remove();
        y.rename("thing2.txt");
    }


}
void jinchu::move2(QString y)
{
    this->ui->lineEdit_6->clear();
    this->ui->lineEdit_7->clear();
    QFile file1("thing2P__temp.txt");
    if(!file1.open(QIODevice::Append|QIODevice::Text))
    {
        QMessageBox::critical(this,"错误","文件打开失败,信息没有写入","确认");
        return;
    }
    QTextStream out(&file1);
    out<<y<<"\n";
    file1.close();
}
void jinchu::change()
{
    x3.show();
}
void jinchu::move3(QString z)
{
    QFile m("thing3.txt");
    if(!m.open(QIODevice::Append|QIODevice::Text))
    {
        QMessageBox::critical(this,"错误","文件打开失败,信息没有写入","确认");
        return;
    }
    QTextStream in2(&m);
    in2<<z<<"\n";
    m.close();
}

jinchu::~jinchu()
{
    delete ui;
}

删除shanchu.h:

#ifndef SHANCHU_H
#define SHANCHU_H

#include <QWidget>
#include<yesorno.h>
#include<QString>
#include<QList>
namespace Ui {
class shanchu;
}

class shanchu : public QWidget
{
    Q_OBJECT

public:
    explicit shanchu(QWidget *parent = nullptr);
    ~shanchu();
    void change();
    yesorno x4;
    void datadelete();
    int readfile();
    void delete2(QString x);
    void delete3(QString y);
    void rename();

private:
    Ui::shanchu *ui;
    QList<QString> things;
    QList<QString> things2;
};

#endif // SHANCHU_H

shanchu.cpp:

#include "shanchu.h"
#include "ui_shanchu.h"
#include<QTextStream>
#include<QString>
#include<QMessageBox>
#include<QDebug>
shanchu::shanchu(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::shanchu)
{
    setWindowTitle("删除商品");
    setWindowIcon(QIcon(":/man black.ico"));
    ui->setupUi(this);
    this->setAutoFillBackground(true); // 这句要加上, 否则可能显示不出背景图.
       QPalette palette = this->palette();
       palette.setBrush(QPalette::Window,
               QBrush(QPixmap("C:\\Users\\17590\\Desktop\\pictures\\wallhaven-rddgwm.jpg").scaled(// 缩放背景图.
                   this->size(),
                   Qt::IgnoreAspectRatio,
                   Qt::SmoothTransformation)));             // 使用平滑的缩放方式
       this->setPalette(palette);
       ui->d->setParent(this);
       ui->d->show();
       ui->label->show();
       ui->pushButton->show();
       ui->lineEdit->show();
       connect(ui->d,&QPushButton::clicked,this,&shanchu::change);
       connect(ui->pushButton,&QPushButton::clicked,this,&shanchu::datadelete);
}
int shanchu::readfile()
{
    things.clear();
    things2.clear();
    QFile file("things.txt");
    QFile file2("thing2.txt");
    if(!file.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        return -1;
    }
    QTextStream in(&file);
    while(!in.atEnd())
    {
        QString line=in.readLine();
        things.append(line);
    }
    file.close();

    if(!file2.open(QIODevice::ReadOnly|QIODevice::Text))
    {
        return -1;
    }
    QTextStream in2(&file2);
    while (!in2.atEnd())
    {
        QString line2=in2.readLine();
        things2.append(line2);
    }
    file2.close();
    return 0;
}
void shanchu::change()
{
    x4.show();
}
void shanchu::datadelete()//
{
   QString name=ui->lineEdit->text();

   if(readfile()==-1)
   {
        this->close();
        QMessageBox::critical(this,"wrong","File reading failed, information not deleted","ok");
   }
   else
{
   for (int i=0;i<things.length();i++)
   {
       QString line=things.at(i);
       //line=line.trimmed();
       QStringList sub=line.split(" ");
       if(name==sub.at(0))//找到需要删除的那一行
       {
           for (int j=0;j<things.length();j++)
           {
               if(things.at(j)==things.at(i)){continue;}
               delete2(things.at(j));

           }

       }
       else if(i==things2.length()-1)
       {
           ui->lineEdit->clear();
           QMessageBox::information(this,"提示","查无此商品","ok","no");
           return;
       }
   }
   for(int x=0;x<things2.length();x++)
   {
       QString line2=things2.at(x);
       QStringList sub2=line2.split(" ");
       if(name==sub2.at(0))
       {
           for(int y=0;y<things2.length();y++)
           {
               if(things2.at(y)==things2.at(x)){continue;}
               delete3(things2.at(y));
           }
           QMessageBox::information(this,"提示","删除成功!","ok","no");
           break;
       }
       else if(x==things2.length()-1)
       {
           ui->lineEdit->clear();
           QMessageBox::information(this,"提示","查无此商品","ok","no");
           return;
       }
   }
}

   QFile file("things.txt");
   QFile file1("things_temp1.txt");
   if(file.exists())
   {
      file.remove();
            qDebug()<<"1";
      file1.rename("things.txt");
   }
   QFile file2("thing2.txt");
   QFile file3("thing2_temp.txt");
   if(file2.exists())
   {
       file2.remove();
       file3.rename("thing2.txt");
   }
}
void shanchu::delete2(QString x)
{
    this->ui->lineEdit->clear();
    QFile mfile1("things_temp1.txt");

    if(!mfile1.open(QIODevice::Append|QIODevice::Text))
    {
        QMessageBox::critical(this,"错误","文件打开失败,信息未写入","确认");
        return;
    }
    QTextStream out(&mfile1);
    out<<x<<"\n";
    mfile1.close();
}
void shanchu::delete3(QString y)
{
    this->ui->lineEdit->clear();
    QFile mfile2("thing2_temp1.txt");
    if(!mfile2.open(QIODevice::Append|QIODevice::Text))
    {
        QMessageBox::critical(this,"错误","文件打开失败,信息未写入","确认");
        return;
    }
    QTextStream out2(&mfile2);
    out2<<y<<"\n";
    mfile2.close();
}

shanchu::~shanchu()
{
    delete ui;
}

添加tianjia.h:

#ifndef TIANJIA_H
#define TIANJIA_H

#include <QWidget>
#include<yesorno.h>
namespace Ui {
class tianjia;
}

class tianjia : public QWidget
{
    Q_OBJECT

public:
    explicit tianjia(QWidget *parent = nullptr);
    ~tianjia();
void change();
void ADD();
yesorno x5;
void clear();

void writeIn(QString info);
private:
    Ui::tianjia *ui;
};

#endif // TIANJIA_H

tianjia.cpp:

#include "tianjia.h"
#include "ui_tianjia.h"
#include<QString>
#include "QMessageBox"
#include<QFile>
#include<QTextStream>
#include<QPushButton>
tianjia::tianjia(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::tianjia)
{
    setWindowTitle("添加商品");
    setWindowIcon(QIcon(":/man black.ico"));
    ui->setupUi(this);
    this->setAutoFillBackground(true); // 这句要加上, 否则可能显示不出背景图.
       QPalette palette = this->palette();
       palette.setBrush(QPalette::Window,
               QBrush(QPixmap("C:\\Users\\17590\\Desktop\\classtest\\wallhaven-rddgwm.jpg").scaled(// 缩放背景图.
                   this->size(),
                   Qt::IgnoreAspectRatio,
                   Qt::SmoothTransformation)));             // 使用平滑的缩放方式
       this->setPalette(palette);
       connect(ui->e,&QPushButton::clicked,this,&tianjia::change);//
       ui->e->setParent(this);
       ui->e->show();
       ui->b->show();
       ui->e->show();
       ui->name->show();
       ui->place->show();
       ui->price->show();
       ui->bianhao->show();
       ui->number->show();
       ui->label->show();
       ui->label_2->show();
       ui->label_3->show();
       ui->label_4->show();
       ui->label_5->show();
       ui->pushButton_2->show();
       connect(ui->on_btn_sys_addstu,&QPushButton::clicked,this,&tianjia::ADD);
}
void tianjia::change()
{
    x5.show();
}
tianjia::~tianjia()
{
    delete ui;
}
void tianjia::ADD()
{
    QString name=ui->name->text();
    QString place =ui->place->text();
    QString price =ui->price->text();
    QString bianhao=ui->bianhao->text();
    QString number=ui->number->text();
    long int sum=number.toInt()*price.toInt();
    QString Sum=QString::number(sum);
    QString messagebox_out="名称: "+name+"\n"+"产地: "+place+"\n"+"价格: "+price+"\n"+"编号: "+bianhao+"\n"
            +"数量: "+number+"\n"+"总价值 "+Sum;
    QString info=name+" "+place+" "+price+" "+bianhao+" "+number+" ";
    bool charge=name.length()<=1||place.length()<=1||price.length()<=1||bianhao.length()<=1||
            number.length()<=1;
    if(charge==1)
    {
        QMessageBox::critical(this,"错误","信息填写不完整,请检查","确定");
    }
    else
    {
       int ret=QMessageBox::question(this,"请确认",messagebox_out,"确认","取消");
       if(ret==0)
          {
             writeIn(info);
          }
    }
}
void tianjia::clear()
{
    ui->name->clear();
    ui->price->clear();
    ui->place->clear();
    ui->bianhao->clear();
    ui->number->clear();
}
void tianjia::writeIn(QString info)
 {
     this->ui->name->clear();
     this->ui->place->clear();
     this->ui->price->clear();
     this->ui->number->clear();
     this->ui->bianhao->clear();
     QFile mFile("things.txt");
     QFile mfile2("thing2.txt");
     if(!mFile.open(QIODevice::Append|QIODevice::Text)||!mfile2.open(QIODevice::Append|QIODevice::Text))
     {
         QMessageBox::critical(this,"错误","文件打开失败,信息没有写入","确认");
         return;
     }
     QTextStream out(&mFile);
     QTextStream out2(&mfile2);
     out<<info<<"\n";
     out2<<info<<"\n";
     mFile.flush();
     mFile.close();
     mfile2.flush();
     mfile2.close();
 }

主页面widget.h:

#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include"bianji.h"//子窗口、
#include"chaxun.h"
#include<jinchu.h>
#include<shanchu.h>
#include<tianjia.h>
QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
void dealsub();
void dealsub1();
void dealsub2();
void dealsub3();
void dealsub4();
void change();
void change1();
void change2();
void change3();
void change4();

private:
    Ui::Widget *ui;
    bianji a;//编辑的窗口
    chaxun b;//查询
    jinchu c;//进出货
    shanchu d;//删除
    tianjia e;

};
#endif // WIDGET_H

widget.cpp:

#include "widget.h"
#include "ui_widget.h"
#include<QPixmap>
#include<bianji.h>
#include<yesorno.h>
#include<chaxun.h>


Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    setWindowIcon(QIcon(":/man black.ico"));
   this->setAutoFillBackground(true); // 这句要加上, 否则可能显示不出背景图.
       QPalette palette = this->palette();
       palette.setBrush(QPalette::Window,
               QBrush(QPixmap("C:\\Users\\17590\\Desktop\\pictures\\wallhaven-rddgwm.jpg").scaled(// 缩放背景图.
                   this->size(),
                   Qt::IgnoreAspectRatio,
                   Qt::SmoothTransformation)));             // 使用平滑的缩放方式
       this->setPalette(palette);
    ui->setupUi(this);
    ui->bianji->show();//编辑
    connect(ui->bianji,&QPushButton::clicked,this,&Widget::change);
    connect(&a.x1,&yesorno::back1,this,&Widget::dealsub);
    ui->label->show();//标题


    ui->chaxun->show();//查询
    connect(ui->chaxun,&QPushButton::clicked,this,&Widget::change1);//打开查询页面
    connect(&b.x2,&yesorno::back1,this,&Widget::dealsub1);//查询yes返回主页面

    ui->jinchu->show();//进出货
    connect(ui->jinchu,&QPushButton::clicked,this,&Widget::change2);
    connect(&c.x3,&yesorno::back1,this,&Widget::dealsub2);

    ui->shanchu->show();//删除
    connect(ui->shanchu,&QPushButton::clicked,this,&Widget::change3);
    connect(&d.x4,&yesorno::back1,this,&Widget::dealsub3);

    ui->tianjia->show();//添加
    connect(ui->tianjia,&QPushButton::clicked,this,&Widget::change4);
    connect(&e.x5,&yesorno::back1,this,&Widget::dealsub4);
}
void Widget::change()
{
    a.show();
    this->hide();
}
void Widget::dealsub()//编辑页面摁yes返回主页面
{
 a.x1.close();
 a.close();
 this->show();
}
void Widget::dealsub1()//查询yes返回主页面
{
    b.x2.close();
    b.close();
    this->show();
}
void Widget::change1()//打开查询页面
{
    b.show();
    this->hide();
}
void Widget::change2()//打开进出货页面
{
    c.show();
    this->hide();
}
void Widget::dealsub2()
{
    c.x3.close();
    c.close();
    this->show();
}
void Widget::change3()
{
    d.show();
    this->hide();
}
void Widget::dealsub3()
{
    d.x4.close();
    d.close();
    this->show();
}
void Widget::change4()
{
    e.show();
    this->hide();
}
void Widget::dealsub4()
{
    e.x5.close();
    e.close();
    this->show();
}
Widget::~Widget()
{
    delete ui;
}

yes or no是问你要不要返回主页面的那个玩意,不加也可以

yesorno.h:

#ifndef YESORNO_H
#define YESORNO_H

#include <QWidget>

namespace Ui {
class yesorno;
}

class yesorno : public QWidget
{
    Q_OBJECT

public:
    explicit yesorno(QWidget *parent = nullptr);
    ~yesorno();
    void sendslot1();//发射信号
    void close();//关闭yesorno选项


signals:
void back1();//返回的信号
private:
    Ui::yesorno *ui;
};

#endif // YESORNO_H

yesorno.cpp:

#include "yesorno.h"
#include "ui_yesorno.h"
#include<bianji.h>

yesorno::yesorno(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::yesorno)
{
    setWindowTitle("?");
    setWindowIcon(QIcon(":/man black.ico"));
    ui->setupUi(this);
    ui->no->show();
    ui->no->setParent(this);
    ui->yes->show();
    ui->yes->setParent(this);
    ui->label->show();
    ui->label->setParent(this);
    connect(ui->no,&QPushButton::clicked,this,&yesorno::close);//取消
    connect(ui->yes,&QPushButton::clicked,this,&yesorno::sendslot1);//确定

}

yesorno::~yesorno()
{
    delete ui;
}
void yesorno::sendslot1()//发射信号
{
    emit back1();
}
void yesorno::close()//取消页面
{
    this->hide();
}

main.cpp//这个懂的都懂

#include "widget.h"
#include <QApplication>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Widget w;

    w.show();
    return a.exec();
}

将就着用吧,自己改改也不是不行,又不是不能用。

最后祝大家error(0),waring(0)

天天Accept,代码不超时。

奖学金保研国一信手拈来,大家教教我也不是不行

又熬夜了,啥也不是,睡觉去了

 


分享:

低价透明

统一报价,无隐形消费

金牌服务

一对一专属顾问7*24小时金牌服务

信息保密

个人信息安全有保障

售后无忧

服务出问题客服经理全程跟进