QT-图书管理系统


前言

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()));

	}
}

六、程序链接提示:

https://download.csdn.net/download/u013083044/88152771

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的大海贼

联系博主,为您提供有价值的资源

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值