QT超市管理系统
前言
写在前面:
小型超市管理系统需要有用户登录界面,登录之后需要有商品列表,需要有付款界面。
我们在此项目中用的QT+Mysql,用户名和密码需要读者自行参考代码.项目名为Supermaket.
学生、图书、超市等管理系统结构都差不多,都可以借鉴一下。
QT介绍
Qt 是一个1991年由Qt Company开发的跨平台C++图形用户界面应用程序开发框架。它既可以开发GUI程序,也可用于开发非GUI程序,比如控制台工具和服务器。Qt是面向对象的框架,使用特殊的代码生成扩展(称为元对象编译器(Meta Object Compiler, moc))以及一些宏,Qt很容易扩展,并且允许真正地组件编程。
2008年,Qt Company科技被诺基亚公司收购,Qt也因此成为诺基亚旗下的编程语言工具。2012年,Qt被Digia收购。
2014年4月,跨平台集成开发环境Qt Creator 3.1.0正式发布,实现了对于iOS的完全支持,新增WinRT、Beautifier等插件,废弃了无Python接口的GDB调试支持,集成了基于Clang的C/C++代码模块,并对Android支持做出了调整,至此实现了全面支持iOS、Android、WP,它提供给应用程序开发者建立艺术级的图形用户界面所需的所有功能。基本上,Qt 同 X Window 上的 Motif,Openwin,GTK 等图形界面库和 Windows 平台上的 MFC,OWL,VCL,ATL 是同类型的东西。
.pro文件
.pro文件是项目文件,其中包含了项目相关信息
#-------------------------------------------------
#
# Project created by QtCreator 2023-05-23T23:37:15
#
#-------------------------------------------------
QT += core gui
QT += sql
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = Supermaket
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as 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 you use 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 += \
main.cpp \
mainwindow.cpp \
user_login.cpp \
maketsql.cpp \
dlg_addmak.cpp \
picture.cpp
HEADERS += \
mainwindow.h \
user_login.h \
maketsql.h \
dlg_addmak.h \
picture.h
FORMS += \
mainwindow.ui \
user_login.ui \
dlg_addmak.ui \
picture.ui
RESOURCES += \
pos.qrc
主文件(main函数)
#include "mainwindow.h"
#include <QApplication>
#include<user_login.h>
#include<maketsql.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
//w.show();
maketsql sql;
return a.exec();
}
窗口函数(mainwindow)
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include<user_login.h>
#include<QSqlError>
#include<QSqlQuery>
#include<QString>
#include<QDebug>
#include<QMessageBox>
#include<maketsql.h>
#include<QStringList>
#include<dlg_addmak.h>
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
void updateTable();
private slots:
void on_pushButton_clicked();
void on_btn_sln_clicked();
void on_btn_add_clicked();
void on_btn_clear_clicked();
void on_btn_update_clicked();
void on_btn_del_clicked();
void on_btn_select_clicked();
void on_btn_over_clicked();
void on_btn_maket_clicked();
private:
Ui::MainWindow *ui;
user_login user;
maketsql *m_ptrMaketSql;
QStringList m_lName;
Dlg_AddMak m_DlgAddMak;
};
#endif // MAINWINDOW_H
mainwindow .cpp文件
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QSqlError>
#include<QSqlQuery>
#include<QString>
#include<QDebug>
#include<QMessageBox>
#include<maketsql.h>
#include<user_login.h>
#include<dlg_addmak.h>
#include<QVector>
#include<picture.h>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
m_ptrMaketSql(nullptr)
{
ui->setupUi(this);
user.show(); //打开商家窗口
auto f=[&](){
this->show();
};
connect(&user,&user_login::sendloginSuccess,this,f);
ui->treeWidget->clear();
QStringList l;
l<<"超市管理界面";
QTreeWidgetItem *pf=new QTreeWidgetItem(ui->treeWidget,l);
ui->treeWidget->addTopLevelItem(pf);
l.clear();
l<<"用户界面";
QTreeWidgetItem *p1=new QTreeWidgetItem(pf,l);
l.clear();
l<<"商家界面";
QTreeWidgetItem *p2=new QTreeWidgetItem(pf,l);
pf->addChild(p1);
pf->addChild(p2);
ui->line_userName->setText(" ACMer");
ui->treeWidget->expandAll();
ui->stackedWidget->setCurrentIndex(0);
m_ptrMaketSql = maketsql::getinstance();
m_ptrMaketSql->init();
m_lName<<"乐事薯片";
m_lName<<"百事可乐";
m_lName<<"可口可乐";
m_lName<<"速溶咖啡";
m_lName<<"芒果干干";
m_lName<<"草莓布丁";
m_lName<<"字母饼干";
m_lName<<"西湖蜜梨";
m_lName<<"辣条豆皮";
m_lName<<"蜜汁肉脯";
m_lName<<"酸奶果粒";
m_lName<<"盐焗大虾";
m_lName<<"辣条烤肠";
m_lName<<"裹个凉皮";
m_lName<<"濮阳壮馍";
m_lName<<"林州皮渣";
ui->tableWidget->clear();
auto cnt= m_ptrMaketSql->getMaketCnt();
QList<MaketInfo> lMaket=m_ptrMaketSql->getPageMa(0,cnt);
//ui->tableWidget->clear();
ui->tableWidget->setRowCount(cnt);
for(int i=0;i<lMaket.size();i++)
{
//ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number((i))));
ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(lMaket[i].id)));
ui->tableWidget->setItem(i,1,new QTableWidgetItem(lMaket[i].name));
ui->tableWidget->setItem(i,2,new QTableWidgetItem(lMaket[i].address));
ui->tableWidget->setItem(i,3,new QTableWidgetItem(lMaket[i].quality));
ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(lMaket[i].price)));
}
updateTable();
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_pushButton_clicked()
{
exit(0);
}
void MainWindow::on_btn_sln_clicked()
{
MaketInfo info;
for(int i=0;i<m_lName.size();i++)
{
info.name=m_lName[i];
info.id=i+1;
info.quality="12";
if(i%3)
{
info.price=i*2;
info.address="河南濮阳";
}
else if(i%2)
{
info.address="河南新乡";
info.price=i*2/3;
}
else
{
info.address="河南郑州";
info.price=i+2;
}
if(i==15)
{
info.address="河南林州";
}
m_ptrMaketSql ->addMaket(info);
}
auto cnt= m_ptrMaketSql->getMaketCnt();
QList<MaketInfo> lMaket=m_ptrMaketSql->getPageMa(0,cnt);
//ui->tableWidget->clear();
ui->tableWidget->setRowCount(cnt);
for(int i=0;i<lMaket.size();i++)
{
//ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number((i))));
ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(lMaket[i].id)));
ui->tableWidget->setItem(i,1,new QTableWidgetItem(lMaket[i].name));
ui->tableWidget->setItem(i,2,new QTableWidgetItem(lMaket[i].address));
ui->tableWidget->setItem(i,3,new QTableWidgetItem(lMaket[i].quality));
ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(lMaket[i].price)));
}
}
void MainWindow::on_btn_add_clicked()
{
m_DlgAddMak.show();
auto cnt= m_ptrMaketSql->getMaketCnt();
QList<MaketInfo> lMaket=m_ptrMaketSql->getPageMa(0,cnt);
//ui->tableWidget->clear();
ui->tableWidget->setRowCount(cnt);
for(int i=0;i<lMaket.size();i++)
{
//ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number((i))));
ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(lMaket[i].id)));
ui->tableWidget->setItem(i,1,new QTableWidgetItem(lMaket[i].name));
ui->tableWidget->setItem(i,2,new QTableWidgetItem(lMaket[i].address));
ui->tableWidget->setItem(i,3,new QTableWidgetItem(lMaket[i].quality));
ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(lMaket[i].price)));
}
}
void MainWindow::on_btn_clear_clicked()
{
m_ptrMaketSql->clearMaket();
auto cnt= m_ptrMaketSql->getMaketCnt();
QList<MaketInfo> lMaket=m_ptrMaketSql->getPageMa(0,cnt);
//ui->tableWidget->clear();
ui->tableWidget->setColumnCount(5);
QStringList l;
l<<"商品编号"<<"商品名称"<<"生产地址"<<"保质期(月)"<<"价格(元)";
ui->tableWidget->setRowCount(cnt);
for(int i=0;i<lMaket.size();i++)
{
//ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number((i))));
ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(lMaket[i].id)));
ui->tableWidget->setItem(i,1,new QTableWidgetItem(lMaket[i].name));
ui->tableWidget->setItem(i,2,new QTableWidgetItem(lMaket[i].address));
ui->tableWidget->setItem(i,3,new QTableWidgetItem(lMaket[i].quality));
ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(lMaket[i].price)));
}
}
void MainWindow::on_btn_update_clicked()
{
/*ui->tableWidget->clear();
ui->tableWidget->setColumnCount(5);
QStringList l;
l<<"商品编号"<<"商品名称"<<"生产地址"<<"保质期(月)"<<"价格(元)";
ui->tableWidget->setHorizontalHeaderLabels(l);
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
auto cnt= m_ptrMaketSql->getMaketCnt();
QList<MaketInfo> lMaket=m_ptrMaketSql->getPageMa(0,cnt);
ui->tableWidget->setRowCount(cnt);
for(int i=0;i<lMaket.size();i++)
{
//ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number((i))));
ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(lMaket[i].id)));
ui->tableWidget->setItem(i,1,new QTableWidgetItem(lMaket[i].name));
ui->tableWidget->setItem(i,2,new QTableWidgetItem(lMaket[i].address));
ui->tableWidget->setItem(i,3,new QTableWidgetItem(lMaket[i].quality));
ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(lMaket[i].price)));
}*/
MaketInfo info;
int i=ui->tableWidget->currentRow();
if(i>=0)
{
info.id=ui->tableWidget->item(i,0)->text().toUInt();
info.name=ui->tableWidget->item(i,1)->text();
info.address=ui->tableWidget->item(i,2)->text();
info.quality=ui->tableWidget->item(i,3)->text();
info.price=ui->tableWidget->item(i,4)->text().toUInt();
m_DlgAddMak.setType(false,info);
m_DlgAddMak.exec();
}
updateTable();
}
void MainWindow::on_btn_del_clicked()
{
int i=ui->tableWidget->currentRow();
if(i>=0)
{
quint32 id=ui->tableWidget->item(i,0)->text().toUInt();
m_ptrMaketSql->delMaket(id);
updateTable();
/*auto cnt= m_ptrMaketSql->getMaketCnt();
QList<MaketInfo> lMaket=m_ptrMaketSql->getPageMa(0,cnt);
ui->tableWidget->setRowCount(cnt);
for(int i=0;i<lMaket.size();i++)
{
//ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number((i))));
ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(lMaket[i].id)));
ui->tableWidget->setItem(i,1,new QTableWidgetItem(lMaket[i].name));
ui->tableWidget->setItem(i,2,new QTableWidgetItem(lMaket[i].address));
ui->tableWidget->setItem(i,3,new QTableWidgetItem(lMaket[i].quality));
ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(lMaket[i].price)));
}*/
QMessageBox::information(nullptr,"信息","删除成功");
}
}
void MainWindow::updateTable()
{
ui->tableWidget->clear();
ui->tableWidget->setColumnCount(5);
QStringList l;
l<<"商品编号"<<"商品名称"<<"生产地址"<<"保质期(月)"<<"价格(元)";
ui->tableWidget->setHorizontalHeaderLabels(l);
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
auto cnt= m_ptrMaketSql->getMaketCnt();
QList<MaketInfo> lMaket=m_ptrMaketSql->getPageMa(0,cnt);
ui->tableWidget->setRowCount(cnt);
for(int i=0;i<lMaket.size();i++)
{
//ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number((i))));
ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString::number(lMaket[i].id)));
ui->tableWidget->setItem(i,1,new QTableWidgetItem(lMaket[i].name));
ui->tableWidget->setItem(i,2,new QTableWidgetItem(lMaket[i].address));
ui->tableWidget->setItem(i,3,new QTableWidgetItem(lMaket[i].quality));
ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString::number(lMaket[i].price)));
}
}
void MainWindow::on_btn_select_clicked()
{
QString strFill=ui->le_select->text();
if(strFill.isEmpty())
{
QMessageBox::information(nullptr,"信息","搜索为空");
return ;
}
ui->tableWidget->clear();
ui->tableWidget->setColumnCount(5);
QStringList l;
l<<"商品编号"<<"商品名称"<<"生产地址"<<"保质期(月)"<<"价格(元)";
ui->tableWidget->setHorizontalHeaderLabels(l);
ui->tableWidget->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
auto cnt= m_ptrMaketSql->getMaketCnt();
QList<MaketInfo> lMaket=m_ptrMaketSql->getPageMa(0,cnt);
int index=0;
QVector<int>res;
for(int i=0;i<lMaket.size();i++)
{
if(!lMaket[i].name.contains(strFill))
{
continue;
}
res.push_back(i);
ui->tableWidget->setItem(index,0,new QTableWidgetItem(QString::number(lMaket[i].id)));
ui->tableWidget->setItem(index,1,new QTableWidgetItem(lMaket[i].name));
ui->tableWidget->setItem(index,2,new QTableWidgetItem(lMaket[i].address));
ui->tableWidget->setItem(index,3,new QTableWidgetItem(lMaket[i].quality));
ui->tableWidget->setItem(index,4,new QTableWidgetItem(QString::number(lMaket[i].price)));
index++;
}
ui->tableWidget->setRowCount(index);
/*for(int i=0;i<res.size();i++)
{
//ui->tableWidget->setRowCount(index);
ui->tableWidget->setItem(res[i],0,new QTableWidgetItem(QString::number(lMaket[res[i]].id)));
ui->tableWidget->setItem(res[i],1,new QTableWidgetItem(lMaket[res[i]].name));
ui->tableWidget->setItem(res[i],2,new QTableWidgetItem(lMaket[res[i]].address));
ui->tableWidget->setItem(res[i],3,new QTableWidgetItem(lMaket[res[i]].quality));
ui->tableWidget->setItem(res[i],4,new QTableWidgetItem(QString::number(lMaket[res[i]].price)));
}*/
}
void MainWindow::on_btn_over_clicked()
{
picture * sk=new picture;
sk->show();
//emit load_picture();
}
void MainWindow::on_btn_maket_clicked()
{
QMessageBox::information(nullptr,"提示信息","商家页面暂不对外开放,敬请期待!");
return ;
}
mainwindow.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>968</width>
<height>657</height>
</rect>
</property>
<property name="windowTitle">
<string>ACM超市管理系统</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(224, 255, 198);
</string>
</property>
<widget class="QWidget" name="centralWidget">
<widget class="QTreeWidget" name="treeWidget">
<property name="enabled">
<bool>true</bool>
</property>
<property name="geometry">
<rect>
<x>11</x>
<y>36</y>
<width>141</width>
<height>561</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<attribute name="headerVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string notr="true">1</string>
</property>
</column>
</widget>
<widget class="QStackedWidget" name="stackedWidget">
<property name="geometry">
<rect>
<x>170</x>
<y>40</y>
<width>731</width>
<height>561</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<property name="currentIndex">
<number>0</number>
</property>
<widget class="QWidget" name="page">
<widget class="QCheckBox" name="checkBox">
<property name="geometry">
<rect>
<x>10</x>
<y>20</y>
<width>57</width>
<height>19</height>
</rect>
</property>
<property name="text">
<string>全选</string>
</property>
</widget>
<widget class="QPushButton" name="btn_add">
<property name="geometry">
<rect>
<x>80</x>
<y>20</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(160, 255, 116);</string>
</property>
<property name="text">
<string>增加</string>
</property>
</widget>
<widget class="QPushButton" name="btn_update">
<property name="geometry">
<rect>
<x>200</x>
<y>20</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(160, 255, 116);</string>
</property>
<property name="text">
<string>修改</string>
</property>
</widget>
<widget class="QPushButton" name="btn_del">
<property name="geometry">
<rect>
<x>320</x>
<y>20</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(160, 255, 116);</string>
</property>
<property name="text">
<string>删除</string>
</property>
</widget>
<widget class="QPushButton" name="btn_select">
<property name="geometry">
<rect>
<x>480</x>
<y>30</y>
<width>81</width>
<height>31</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(103, 111, 255);
color: rgb(255, 255, 255);</string>
</property>
<property name="text">
<string> 搜索 :</string>
</property>
</widget>
<widget class="QTableWidget" name="tableWidget">
<property name="geometry">
<rect>
<x>0</x>
<y>60</y>
<width>731</width>
<height>491</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
</widget>
<widget class="QLineEdit" name="le_select">
<property name="geometry">
<rect>
<x>560</x>
<y>30</y>
<width>171</width>
<height>31</height>
</rect>
</property>
</widget>
</widget>
<widget class="QWidget" name="page_2"/>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>340</x>
<y>10</y>
<width>211</width>
<height>21</height>
</rect>
</property>
<property name="font">
<font>
<family>DengXian</family>
<pointsize>14</pointsize>
<strikeout>true</strikeout>
</font>
</property>
<property name="styleSheet">
<string notr="true">color: rgb(255, 255, 255);
color: rgb(170, 0, 255);</string>
</property>
<property name="text">
<string>ACM超市管理系统</string>
</property>
</widget>
<widget class="QPushButton" name="pushButton">
<property name="geometry">
<rect>
<x>880</x>
<y>0</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(196, 255, 179);
background-color: rgb(255, 85, 0);</string>
</property>
<property name="text">
<string>退出</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>604</x>
<y>10</y>
<width>61</width>
<height>20</height>
</rect>
</property>
<property name="font">
<font>
<family>Agency FB</family>
<pointsize>10</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>用户名:</string>
</property>
</widget>
<widget class="QLineEdit" name="line_userName">
<property name="geometry">
<rect>
<x>670</x>
<y>10</y>
<width>171</width>
<height>21</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
</widget>
<widget class="QPushButton" name="btn_sln">
<property name="geometry">
<rect>
<x>900</x>
<y>550</y>
<width>71</width>
<height>41</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 123, 242);</string>
</property>
<property name="text">
<string>查看商品</string>
</property>
</widget>
<widget class="QPushButton" name="btn_clear">
<property name="geometry">
<rect>
<x>900</x>
<y>490</y>
<width>71</width>
<height>41</height>
</rect>
</property>
<property name="maximumSize">
<size>
<width>81</width>
<height>16777215</height>
</size>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 123, 242);</string>
</property>
<property name="text">
<string>清空商品</string>
</property>
</widget>
<widget class="QPushButton" name="btn_over">
<property name="geometry">
<rect>
<x>900</x>
<y>430</y>
<width>71</width>
<height>41</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 123, 242);</string>
</property>
<property name="text">
<string>结算商品</string>
</property>
</widget>
<widget class="QPushButton" name="btn_maket">
<property name="geometry">
<rect>
<x>900</x>
<y>370</y>
<width>71</width>
<height>41</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 123, 242);</string>
</property>
<property name="text">
<string>商家界面</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menuBar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>968</width>
<height>26</height>
</rect>
</property>
</widget>
<widget class="QToolBar" name="mainToolBar">
<attribute name="toolBarArea">
<enum>TopToolBarArea</enum>
</attribute>
<attribute name="toolBarBreak">
<bool>false</bool>
</attribute>
</widget>
<widget class="QStatusBar" name="statusBar"/>
</widget>
<layoutdefault spacing="6" margin="11"/>
<resources/>
<connections/>
</ui>
用户登录(user_login)
用户通过user_login登录界面点击登录按钮进入mainwindow界面进行查看商品
user_login.h
#ifndef USER_LOGIN_H
#define USER_LOGIN_H
#include <QWidget>
#include<QSqlError>
#include<QSqlQuery>
#include<QString>
#include<QDebug>
#include<QMessageBox>
namespace Ui {
class user_login;
}
class user_login : public QWidget
{
Q_OBJECT
public:
explicit user_login(QWidget *parent = 0);
~user_login();
signals:
void sendloginSuccess();
private slots:
void on_btn_userlogin_clicked();
//void on_btn_tuichu_clicked();
void on_btn_close_clicked();
private:
Ui::user_login *ui;
};
#endif // USER_LOGIN_H
user_login.cpp
#include "user_login.h"
#include "ui_user_login.h"
#include<QtSql>
#include<QSqlError>
#include<QSqlQuery>
#include<QString>
#include<QDebug>
#include<QMessageBox>
user_login::user_login(QWidget *parent) :
QWidget(parent),
ui(new Ui::user_login)
{
ui->setupUi(this);
}
user_login::~user_login()
{
delete ui;
}
void user_login::on_btn_userlogin_clicked()
{
QString str1=ui->le_user->text();
QString str2=ui->lineEdit_2->text();
QString s1="2228824083";
QString s2="2128824092";
if(str1!=s1&&str1!=s2)
{
QMessageBox::information(nullptr,"用户名错误","数据库中没有该用户");
return ;
}
if(str1!=str2)
{
QMessageBox::information(nullptr,"错误信息","用户名与密码不匹配!");
return ;
}
emit sendloginSuccess();
}
void user_login::on_btn_close_clicked()
{
this->hide();
}
user_login.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>user_login</class>
<widget class="QWidget" name="user_login">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>482</width>
<height>358</height>
</rect>
</property>
<property name="windowTitle">
<string>ACM超市管理系统</string>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(225, 255, 193);
background-color: rgb(135, 206, 250);</string>
</property>
<widget class="QLineEdit" name="lineEdit_2">
<property name="geometry">
<rect>
<x>170</x>
<y>140</y>
<width>171</width>
<height>31</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">
background-color: rgb(255, 255, 255);</string>
</property>
</widget>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>123</x>
<y>143</y>
<width>45</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>用户:</string>
</property>
</widget>
<widget class="QLabel" name="label_2">
<property name="geometry">
<rect>
<x>120</x>
<y>220</y>
<width>45</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>密码:</string>
</property>
</widget>
<widget class="QLabel" name="label_3">
<property name="geometry">
<rect>
<x>120</x>
<y>50</y>
<width>280</width>
<height>26</height>
</rect>
</property>
<property name="font">
<font>
<family>华文仿宋</family>
<pointsize>14</pointsize>
<underline>true</underline>
</font>
</property>
<property name="text">
<string>欢迎光临ACM超市管理系统</string>
</property>
</widget>
<widget class="QWidget" name="widget" native="true">
<property name="geometry">
<rect>
<x>10</x>
<y>280</y>
<width>471</width>
<height>50</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>67</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btn_userlogin">
<property name="styleSheet">
<string notr="true">background-color: rgb(139, 137, 137);</string>
</property>
<property name="text">
<string>用户登录</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>68</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="btn_close">
<property name="styleSheet">
<string notr="true">background-color: rgb(139, 137, 137);</string>
</property>
<property name="text">
<string>退出登录</string>
</property>
<property name="checkable">
<bool>false</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>67</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<widget class="QLineEdit" name="le_user">
<property name="geometry">
<rect>
<x>170</x>
<y>210</y>
<width>171</width>
<height>31</height>
</rect>
</property>
<property name="styleSheet">
<string notr="true">background-color: rgb(255, 255, 255);</string>
</property>
<property name="echoMode">
<enum>QLineEdit::Password</enum>
</property>
</widget>
</widget>
<resources/>
<connections/>
</ui>
超市系统数据库(maketsql)
本文件负责连接数据库MySQL
maketsql.h
#ifndef MAKETSQL_H
#define MAKETSQL_H
#include <QObject>
#include<QSqlDatabase>
struct MaketInfo
{
quint32 id;
QString name;
QString address;
QString quality;
quint32 price;
};
class maketsql : public QObject
{
Q_OBJECT
public:
static maketsql *ptrMaketSql;
static maketsql *getinstance()
{
if(ptrMaketSql == nullptr)
{
ptrMaketSql =new maketsql;
}
return ptrMaketSql;
}
explicit maketsql(QObject *parent = nullptr);
//打开数据库
void init();
//查询数量
quint32 getMaketCnt();
//查询在几页
QList<MaketInfo>getPageMa(quint32 page,quint32 uiCnt);
//增加商品
bool addMaket(MaketInfo info);
//修改商品
void updateMaket(MaketInfo info);
//删除商品
bool delMaket(quint32 id);
//清空商品
bool clearMaket();
private:
QSqlDatabase m_db;
signals:
public slots:
};
#endif // MAKETSQL_H
maketsql.cpp
#include "maketsql.h"
#include<QtSql>
#include<QSqlError>
#include<QSqlQuery>
#include<QString>
#include<QDebug>
#include<QMessageBox>
#include<mainwindow.h>
#include<user_login.h>
maketsql *maketsql::ptrMaketSql =nullptr;
maketsql::maketsql(QObject *parent) : QObject(parent)
{
init();
/*QSqlQuery query;
QString sql="insert into maket(id ,name,address,quality,price) values(212882,'光','濮阳','6',12)";
query.exec(sql);*/
getMaketCnt();
getPageMa(2,3);
}
void maketsql::init()
{
m_db=QSqlDatabase::addDatabase("QMYSQL"); //括号内要写出数据库的类型
//设置数据库
m_db.setHostName("localhost"); //设置数据库的主机ip
//设置数据库的用户名
m_db.setPort(3307);
m_db.setUserName("root");
//设置数据库的密码
m_db.setPassword("mingguang0703"); //这个就是安装MySQL时设置的密码
//设置数据库的名字
m_db.setDatabaseName("qmaket");
//打开数据库(已经安装过mysql驱动了)
if(m_db.open()==false)
{
qDebug() << "No Open DataBase !";
}
else qDebug()<<"Success Open DataBase !";
}
quint32 maketsql::getMaketCnt()
{
QSqlQuery sql(m_db);
sql.exec("select count(id) from maket;" );
quint32 uiCnt=0;
while(sql.next())
{
uiCnt = sql.value(0).toUInt();
}
return uiCnt;
}
QList<MaketInfo> maketsql::getPageMa(quint32 page, quint32 uiCnt)
{
QList<MaketInfo> l;
QSqlQuery sql(m_db);
QString strsql=QString("select * from maket order by id limit %1 offset %2 ").arg(uiCnt).arg(page * uiCnt);
sql.exec(strsql);
MaketInfo info;
while(sql.next())
{
info.id=sql.value(0).toUInt();
info.name=sql.value(1).toString();
info.address=sql.value(2).toString();
info.quality=sql.value(3).toString();
info.price=sql.value(4).toUInt();
l.push_back(info);
}
return l;
}
bool maketsql::addMaket(MaketInfo info)
{
QSqlQuery sql(m_db);
QString strSql=QString("insert into maket(id ,name,address,quality,price) values(%1,'%2','%3','%4',%5)").arg(info.id).arg(info.name).arg(info.address).arg(info.quality).arg(info.price);
return sql.exec(strSql);
}
void maketsql::updateMaket(MaketInfo info)
{
QSqlQuery sql(m_db);
QString strSql=QString("update maket set id = %1 name ='%2' address = '%3' quality ='%4' price= %5 where id = %6").arg(info.id).arg(info.name).arg(info.address).arg(info.quality).arg(info.price).arg(info.id);
sql.exec(strSql);
}
bool maketsql::delMaket(quint32 id)
{
QSqlQuery sql(m_db);
return sql.exec(QString("delete from maket where id = %1").arg(id));
}
bool maketsql::clearMaket()
{
QSqlQuery sql(m_db);
return sql.exec("delete from maket");
}
此文件没有ui界面
超市商品的增删改查(dlg_addmak)
负责超市系统商品的增删改查,点击增加按钮后弹出改ui界面来填写增加的商品名称、地址等。
dlgadd_mak.h
#ifndef DLG_ADDMAK_H
#define DLG_ADDMAK_H
#include <QDialog>
#include<maketsql.h>
namespace Ui {
class Dlg_AddMak;
}
class Dlg_AddMak : public QDialog
{
Q_OBJECT
public:
explicit Dlg_AddMak(QWidget *parent = 0);
~Dlg_AddMak();
void setType(bool isAdd,MaketInfo info);
private slots:
void on_btn_save_clicked();
void on_pushButton_2_clicked();
private:
Ui::Dlg_AddMak *ui;
int m_id;
bool is_mAdd;
MaketInfo m_info;
};
#endif // DLG_ADDMAK_H
dla_addmak.cpp
#include "dlg_addmak.h"
#include "ui_dlg_addmak.h"
#include<maketsql.h>
#include<QMessageBox>
Dlg_AddMak::Dlg_AddMak(QWidget *parent) :
QDialog(parent),
ui(new Ui::Dlg_AddMak)
{
ui->setupUi(this);
}
Dlg_AddMak::~Dlg_AddMak()
{
delete ui;
}
void Dlg_AddMak::setType(bool isAdd, MaketInfo info)
{
is_mAdd=isAdd;
m_info=info;
ui->le_id->setText(QString::number(info.id));
ui->le_name->setText(info.name);
ui->le_address->setText(info.address);
ui->le_quality->setText(info.quality);
ui->le_price->setText(QString::number(info.price));
}
void Dlg_AddMak::on_btn_save_clicked()
{
MaketInfo info;
auto ptr=maketsql::getinstance();
info.id=ui->le_id->text().toUInt();
info.name=ui->le_name->text();
info.address=ui->le_address->text();
info.quality=ui->le_quality->text();
info.price=ui->le_price->text().toUInt();
if(is_mAdd)
{
info.id=m_info.id;
ptr->addMaket(info);
}
else
{
info.id=m_info.id;
ptr->updateMaket(info);
}
QMessageBox::information(nullptr,"商品","增加成功!");
this->hide();
}
void Dlg_AddMak::on_pushButton_2_clicked()
{
this->hide();
}
dla_addmak.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>Dlg_AddMak</class>
<widget class="QDialog" name="Dlg_AddMak">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>346</width>
<height>273</height>
</rect>
</property>
<property name="windowTitle">
<string>增加窗口</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="1" column="1">
<widget class="QLineEdit" name="le_name"/>
</item>
<item row="0" column="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="5" column="1">
<widget class="QLineEdit" name="le_price"/>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="le_address">
<property name="focusPolicy">
<enum>Qt::WheelFocus</enum>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item row="4" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>保质期(月)</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="le_id"/>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="layoutDirection">
<enum>Qt::LeftToRight</enum>
</property>
<property name="text">
<string> 商品编号</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string> 生产地址</string>
</property>
</widget>
</item>
<item row="5" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string> 价格(元)</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string> 商品名称</string>
</property>
</widget>
</item>
<item row="4" column="1">
<widget class="QLineEdit" name="le_quality"/>
</item>
<item row="6" column="0" colspan="3">
<widget class="QWidget" name="widget" native="true">
<widget class="QPushButton" name="btn_cancel">
<property name="geometry">
<rect>
<x>220</x>
<y>50</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>取消</string>
</property>
</widget>
<widget class="QPushButton" name="btn_save">
<property name="geometry">
<rect>
<x>70</x>
<y>50</y>
<width>93</width>
<height>28</height>
</rect>
</property>
<property name="text">
<string>确认</string>
</property>
</widget>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections/>
</ui>
收款码界面(picture)
在mainwindow界面点击结算商品按钮弹出该界面。
本人的收款码在这里就不展示了,读者可自行选择是否使用自己的!。
picture.h
#ifndef PICTURE_H
#define PICTURE_H
#include <QWidget>
#include<mainwindow.h>
namespace Ui {
class picture;
}
class picture : public QWidget
{
Q_OBJECT
public:
explicit picture(QWidget *parent = 0);
~picture();
private:
Ui::picture *ui;
};
#endif // PICTURE_H
picture.cpp
#include "picture.h"
#include "ui_picture.h"
#include<mainwindow.h>
picture::picture(QWidget *parent) :
QWidget(parent),
ui(new Ui::picture)
{
ui->setupUi(this);
/* QPixmap image;
image.load("code.jpg");
ui->label->clear();
ui->label->setPixmap(image);
ui->label->show();
auto p=[&](){
this->show();
};
connect(&minw,&MainWindow::load_picture,this,p);*/
}
picture::~picture()
{
delete ui;
}
picture.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>picture</class>
<widget class="QWidget" name="picture">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>1066</width>
<height>668</height>
</rect>
</property>
<property name="windowTitle">
<string>收款码</string>
</property>
<widget class="QLabel" name="label">
<property name="geometry">
<rect>
<x>-50</x>
<y>0</y>
<width>1131</width>
<height>741</height>
</rect>
</property>
<property name="text">
<string/>
</property>
<property name="pixmap">
<pixmap resource="pos.qrc">:/new/image/code.jpg</pixmap>
</property>
</widget>
</widget>
<resources>
<include location="pos.qrc"/>
</resources>
<connections/>
</ui>
结语
写在最后:
这是我们的软件工程作业,因为快要结课,时间可能不是很充足,所以ui界面有的部分颜色很不搭,请各位读者理解!
如果读者想要下载源码,可以点击GitHub在我的仓库内下载。
⭐学术交流群Q 754410389 持续更新中~~~
2023.5.31 河南师范大学