前言
1、使用QT5.13+VS2017的开发环境。
2、支持多种数据库匹配的方式,默认sqllite数据库,免去数据库安装。
3、扁平化的软件样式,十分好看。
4、多层软件结构,清晰简单。
一、演示效果

二、数据库结构
默认使用sqllite数据库,这样程序就省去大数据库环境的安装,当然sql语句都是一样的,你也可以做切换。

三、程序结构
程序结构分层结构来处理,page是图形页面部分,base是基础公用库,tool是工具库,QSotfFrame是软件主界面

四、软件登录
登录可以选择管理员和普通用户登录

1、核心程序
#include "QLogin.h"
#include "QRegisterDialog.h"
#include "Tool/ToolDatabase/ToolDatabase.h"
#include "Tool/ToolSetting/ToolSetting.h"
QLogin::QLogin(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
Qt::WindowFlags flags = Qt::Dialog;
flags |= Qt::WindowCloseButtonHint;
flags |= Qt::FramelessWindowHint;
this->setWindowFlags(flags);
ui.comboBox->addItems(QStringList() << u8"普通用户" << u8"管理员");
initEvent();
}
QLogin::~QLogin()
{
}
void QLogin::initEvent()
{
connect(ui.btnClose, &QPushButton::clicked, this, [=]() {
this->accept();
});
connect(ui.toolButtonLogin, &QPushButton::clicked, this, [=]() {
QString strUser = ui.lineEditUser->text();
QString strPwd = ui.lineEditPwd->text();
QString strId = "";
QString strUserType = "";
bool bFind = false;
auto listHash = ToolDatabase::getInstance()->select(g_SqlAccountManagerTableName);
for (auto& val : listHash)
{
auto itFindAccountId = val.find(g_SqlAccountId);
auto itFindAccount = val.find(g_SqlAccount);
auto itFindPwd = val.find(g_SqlAccountPassword);
auto itFindType = val.find(g_SqlAccountType);
strId = itFindAccountId.value();
strUserType = itFindType.value();
if (itFindAccount != val.end()
&& itFindPwd != val.end()
&& itFindAccount.value() == strUser
&& itFindPwd.value() == strPwd)
{
bFind = true;
break;
}
}
if (bFind)
{
ToolSetting::getInstance()->strCurPwd = strPwd;
ToolSetting::getInstance()->strCurUser = strUser;
ToolSetting::getInstance()->strCurUserType = strUserType;
ToolSetting::getInstance()->strCurUserId = strId;
this->accept();
}
else
{
ui.labelWarn->setText(u8"账号或者密码不存在!");
}
});
connect(ui.toolButtonRegister, &QPushButton::clicked, this, [=]() {
QRegisterDialog dlg(this);
dlg.exec();
});
}
五、软件注册

#include "QRegisterDialog.h"
#include "Tool/ToolDatabase/ToolDatabase.h"
#include <QMessageBox>
#include <QUuid>
#include <QVariant>
#include <QHash>
#include <QComboBox>
QRegisterDialog::QRegisterDialog(QWidget *parent)
: QDialog(parent)
{
ui.setupUi(this);
Qt::WindowFlags flags = Qt::Dialog;
flags |= Qt::WindowCloseButtonHint;
flags |= Qt::FramelessWindowHint;
this->setWindowFlags(flags);
ui.comboBoxSex->addItems(QStringList() << u8"男" << u8"女");
initEvent();
}
QRegisterDialog::~QRegisterDialog()
{
}
void QRegisterDialog::initEvent()
{
connect(ui.btnClose, &QPushButton::clicked, this, [=]() {
this->accept();
});
connect(ui.btnOk, &QPushButton::clicked, this, [=]() {
if ( ui.lineEditSecondPwd->text().isEmpty()
&& ui.lineEditPassword->text().isEmpty())
{
QMessageBox::warning(this, u8"提示", u8"密码不能为空!", QMessageBox::Yes);
return;
}
if (ui.lineEditSecondPwd->text() != ui.lineEditPassword->text()
&& !ui.lineEditSecondPwd->text().isEmpty()
&& !ui.lineEditPassword->text().isEmpty())
{
ui.lineEditSecondPwd->setText("");
QMessageBox::warning(this, u8"提示", u8"两次密码输入不一致!", QMessageBox::Yes);
return;
}
QString strId = QUuid::createUuid().toString();
QString strAccountId = QUuid::createUuid().toString();
QString strAccount = ui.lineEditAccount->text();
QString strPwd = ui.lineEditSecondPwd->text();
QString strName = ui.lineEditName->text();
QString strSex = ui.comboBoxSex->currentText();
QString strNumeber = ui.lineEditNumber->text();
QString strCource = ui.lineEditCource->text();
QString strPhone = ui.lineEditPhone->text();
// 插入到账号表
QHash<QString, QVariant> hash;
hash.insert(g_SqlAccountId, QVariant::fromValue(strAccountId));
hash.insert(g_SqlAccount, QVariant::fromValue(strAccount));
hash.insert(g_SqlAccountPassword, QVariant::fromValue(strPwd));
hash.insert(g_SqlAccountType, QVariant::fromValue(QString(u8"普通用户")));
ToolDatabase::getInstance()->insert(g_SqlAccountManagerTableName, hash);
// 插入到学生表
QHash<QString, QVariant> hash1;
hash1.insert(g_SqlStudentId, QVariant::fromValue(strId));
hash1.insert(g_SqlStudentAccountId, QVariant::fromValue(strAccountId));
hash1.insert(g_SqlStudentType, QVariant::fromValue(QString(u8"学生")));
hash1.insert(g_SqlStudentNumber, QVariant::fromValue(strNumeber));
hash1.insert(g_SqlStudentName, QVariant::fromValue(strName));
hash1.insert(g_SqlStudentSex, QVariant::fromValue(strSex));
hash1.insert(g_SqlStudentCource, QVariant::fromValue(strCource));
hash1.insert(g_SqlStudentPhone, QVariant::fromValue(strPhone));
ToolDatabase::getInstance()->insert(g_SqlStudentManagerTableName, hash1);
QMessageBox::information(this, u8"提示", u8"注册成功!请重新登录!",QMessageBox::Yes);
this->accept();
});
}
四、其他页面








#include "PageWidget.h"
#include "../../Tool/ToolDatabase/ToolDatabase.h"
#include <QMessageBox>
#include <QCompleter>
// 图书管理
PageWidget::PageWidget(QWidget *parent)
: QWidget(parent)
{
ui.setupUi(this);
initForm();
initEvent();
updateTableWidget();
}
PageWidget::~PageWidget()
{
}
// 初始化控件
void PageWidget::initForm()
{
// 初始化列表
int nSize = sizeof(g_BookManagerArray) / sizeof(g_BookManagerArray[0]);
QStringList strList;
for (size_t i = 0; i < nSize; i++)
strList << g_BookManagerArray[i];
auto pTable = ui.tableWidget;
pTable->setColumnCount(strList.size());
pTable->setHorizontalHeaderLabels(strList);
pTable->horizontalHeader()->setMinimumSectionSize(100);
pTable->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);
// 创建对话框
m_pItemDialog = new ItemDialog(this);
// 添加自动补全
QStringList strList1;
auto listHash = ToolDatabase::getInstance()->select(g_SqlBookManagerTableName);
for (auto& val : listHash)
{
auto itFindId = val.find(g_SqlBookId);
auto itFindAuthor = val.find(g_SqlBookAuthor);
auto itFindType = val.find(g_SqlBookType);
auto itFindPublshing = val.find(g_SqlBookPublishing);
auto itFindName = val.find(g_SqlBookName);
auto itFindImage = val.find(g_SqlBookImage);
if (itFindId != val.end())
strList1 << itFindId.value();
if (itFindAuthor != val.end())
strList1 << itFindAuthor.value();
if (itFindName != val.end())
strList1 << itFindName.value();
if (itFindType != val.end())
strList1 << itFindType.value();
if (itFindPublshing != val.end())
strList1 << itFindPublshing.value();
if (itFindImage != val.end())
strList1 << itFindImage.value();
}
static QCompleter completer(strList1);
ui.lineEditFind->setCompleter(&completer);
}
// 初始化事件
void PageWidget::initEvent()
{
//新增
connect(ui.btnAdd, &QPushButton::clicked, this,[=]() {
m_pItemDialog->autoCreateId(true);
if (m_pItemDialog->showView())
{
if (m_pItemDialog->author().isEmpty()
|| m_pItemDialog->type().isEmpty()
|| m_pItemDialog->publishing().isEmpty()
|| m_pItemDialog->name().isEmpty()
|| m_pItemDialog->image().isEmpty())
{
QMessageBox::critical(this, u8"错误", u8"不能存在空字符!",QMessageBox::Yes);
return;
}
QHash<QString, QVariant> hash;
hash.insert(g_SqlBookId, QVariant::fromValue(m_pItemDialog->id()));
hash.insert(g_SqlBookAuthor, QVariant::fromValue(m_pItemDialog->author()));
hash.insert(g_SqlBookType, QVariant::fromValue(m_pItemDialog->type()));
hash.insert(g_SqlBookPublishing, QVariant::fromValue(m_pItemDialog->publishing()));
hash.insert(g_SqlBookName, QVariant::fromValue(m_pItemDialog->name()));
hash.insert(g_SqlBookImage, QVariant::fromValue(m_pItemDialog->image()));
ToolDatabase::getInstance()->insert(g_SqlBookManagerTableName, hash);
updateTableWidget();
}
});
//编辑
connect(ui.btnEdite, &QPushButton::clicked, this, [=]() {
auto pTable = ui.tableWidget;
auto nRow = pTable->currentRow();
if (nRow > -1)
{
m_pItemDialog->autoCreateId(false);
QString strId = pTable->item(nRow, 0)->text();
QString strAuthor = pTable->item(nRow, 1)->text();
QString strName = pTable->item(nRow, 2)->text();
QString strType = pTable->item(nRow, 3)->text();
QString strPublshing = pTable->item(nRow, 4)->text();
QString strImage = pTable->item(nRow, 5)->text();
if (m_pItemDialog->showView(strId,strAuthor,strType,strPublshing,strImage, strName))
{
ToolDatabase::getInstance()->update(g_SqlBookManagerTableName, g_SqlBookId,m_pItemDialog->id(), g_SqlBookAuthor, m_pItemDialog->author());
ToolDatabase::getInstance()->update(g_SqlBookManagerTableName, g_SqlBookId, m_pItemDialog->id(), g_SqlBookType, m_pItemDialog->type());
ToolDatabase::getInstance()->update(g_SqlBookManagerTableName, g_SqlBookId, m_pItemDialog->id(), g_SqlBookPublishing, m_pItemDialog->publishing());
ToolDatabase::getInstance()->update(g_SqlBookManagerTableName, g_SqlBookId, m_pItemDialog->id(), g_SqlBookName, m_pItemDialog->name());
ToolDatabase::getInstance()->update(g_SqlBookManagerTableName, g_SqlBookId, m_pItemDialog->id(), g_SqlBookImage, m_pItemDialog->image());
updateTableWidget();
}
}
});
//删除
connect(ui.btnRemove, &QPushButton::clicked, this, [=]() {
auto pTable = ui.tableWidget;
auto nRow = pTable->currentRow();
if (nRow > -1)
{
m_pItemDialog->autoCreateId(false);
QString strId = pTable->item(nRow, 0)->text();
ToolDatabase::getInstance()->remove(g_SqlBookManagerTableName, g_SqlBookId, strId);
updateTableWidget();
}
});
//查询
connect(ui.btnFind, &QPushButton::clicked, this, [=]() {
auto pTable = ui.tableWidget;
int nRowCount = pTable->rowCount();
int nColumnCount = pTable->columnCount();
QString strFind = ui.lineEditFind->text();
for (size_t iRow = 0; iRow < nRowCount; iRow++)
{
for (size_t iCol = 0; iCol < nColumnCount; iCol++)
{
auto pItem = pTable->item(iRow, iCol);
if (pItem != nullptr)
{
if (pItem->text().indexOf(strFind) != -1)
{
pTable->setCurrentItem(pItem);
break;
}
}
}
}
});
}
// 刷新列表数据
void PageWidget::updateTableWidget()
{
if (!ToolDatabase::getInstance()->openDb(g_SqlDatabaseName))
return;
auto pTable = ui.tableWidget;
while (pTable->rowCount() > 0)
pTable->removeRow(0);
auto listHash = ToolDatabase::getInstance()->select(g_SqlBookManagerTableName);
for (auto& val : listHash)
{
int nRow = pTable->rowCount();
pTable->insertRow(nRow);
auto itFindId = val.find(g_SqlBookId);
auto itFindAuthor = val.find(g_SqlBookAuthor);
auto itFindType = val.find(g_SqlBookType);
auto itFindPublshing = val.find(g_SqlBookPublishing);
auto itFindName = val.find(g_SqlBookName);
auto itFindImage = val.find(g_SqlBookImage);
if (itFindId != val.end())
{
pTable->setItem(nRow, 0, new QTableWidgetItem(itFindId.value()));
pTable->item(nRow, 0)->setIcon(QIcon(itFindImage.value()));
pTable->item(nRow, 0)->setFlags(pTable->item(nRow, 0)->flags() & ~Qt::ItemIsEditable); // 不可以编辑
}
if (itFindAuthor != val.end())
pTable->setItem(nRow, 1, new QTableWidgetItem(itFindAuthor.value()));
if (itFindName != val.end())
pTable->setItem(nRow, 2, new QTableWidgetItem(itFindName.value()));
if (itFindType != val.end())
pTable->setItem(nRow, 3, new QTableWidgetItem(itFindType.value()));
if (itFindPublshing != val.end())
pTable->setItem(nRow, 4, new QTableWidgetItem(itFindPublshing.value()));
if (itFindImage != val.end())
pTable->setItem(nRow, 5, new QTableWidgetItem(itFindImage.value()));
}
}
428

被折叠的 条评论
为什么被折叠?



