C++实现图书管理系统(Qt C++ GUI界面版)

前瞻
本项目基于【C++】图书管理系统(完整版)

图书管理系统功能概览:

  1. 登录,注册
  2. 学生,老师借书,查看自己当前借书情况,还书。
  3. 管理员增加书,查看当前借阅情况,查看当前所有借阅人,图书信息。

效果概览:
登录:输入用户名和密码,选择对应角色(管理员、学生、老师),点击登录,对应信息存储在txt文件中,程序会自动校验是否输入的用户名和密码存在并对应
在这里插入图片描述
在这里插入图片描述

注册:当用户点击注册时,将跳转到注册页面,程序会将用户信息存入txt文件中,注册成功后,会自动跳转到登录界面,用户也可以通过点击登录按钮跳转回登录界面
在这里插入图片描述
用户界面(学生或老师):当用户登录成功后,进入操作界面,包括功能如下:
借书:
在这里插入图片描述
查看自己借阅情况
在这里插入图片描述
还书:
在这里插入图片描述
管理员界面: 包括功能如下:
增加书:
在这里插入图片描述
查看当前借阅情况:
在这里插入图片描述
查看所有图书:
在这里插入图片描述
查看借阅人详细信息:
在这里插入图片描述
目前该系统大致功能如上。

代码概览:

各个模块主要负责功能
主要用于数据处理、存储部分,类似后端CBook.h  用于对书抽象,并实现了>>的重载,便于文件读入,读出CUser.h  工具人,作为老师,学生的父类CStudent.h  对学生进行的抽象CTeacher.h 对老师进行的抽象CAdministrator.h 对管理的抽象CManagementBooks.h  用户所有的相关操作的中间执行者,有设计模式中代理的思想QtLibrarySystem.cpp  界面的的实现与系统入口主要用于数据暂时,处理用户输入,类似前端RegistrationPage.h 注册界面实现类UserPage.h用户界面实现类LoginPage.h登录界面实现类AdminPage.h管理员界面实现类

说明:获取方式见文章最后,点击查看,还有就是文件读入建议大家都直接放在相应项目里面,不然就写绝对路径。有问题欢迎大家在评论区提问,喜欢就点个赞咯!


全部代码与讲解:

CUser.cpp

#include "CUser.h"
#include<iostream>
#include<fstream>
using namespace std;CUser::CUser()
{m_name = "陈1";
}void CUser::setId(string id)
{m_id = id;
}void CUser::setName(string name)
{m_name = name;
}void CUser::setGender(string gender)
{m_gender = gender;
}void CUser::setDepartment(string department)
{m_department = department;
}void CUser::setPhone(string phone)
{m_phone = phone;
}CUser::~CUser()
{
}string CUser::returnBook(CManagementBooks& mb,string bookName)
{int all = mb.checkBorrowedBook(m_id);if (all == 0){return  "您暂未借书,无需归还";}else{if (mb.checkTrueBorrow(m_id, bookName)){mb.Return(m_id, bookName);return "";}else{return "您并未借阅此书";}}return "";
}string CUser::getId()
{return m_id;
}string CUser::getName()
{return m_name;
}string CUser::getGender()
{return m_gender;
}string CUser::getDepartment()
{return m_department;
}string CUser::getPhone()
{return m_phone;
}std::ostream& operator<<(std::ostream& os, const CUser* user)
{os << endl << user->m_id << " " << user->m_name << " " << user->m_gender << " " << user->m_department << " " << user->m_phone;return os;
}std::istream& operator>>(std::istream& is, CUser* user)
{is >> user->m_id >> user->m_name >> user->m_gender >> user->m_department >> user->m_phone;return is;
}

CTeacher.cpp

#include "CTeacher.h"
#include<fstream>CTeacher::CTeacher()
{m_name = "刘一";
}string CTeacher::borrowBookFromLibrary(CManagementBooks& mb, string name)
{int all = mb.checkBorrowedBook(m_id);if (all < 5){if (mb.borrow(name)){ofstream writeFile("borrowedBook.txt", ios::app);mb.setMapValue(m_id, name);writeFile << endl << m_id << " " << name;writeFile.close();}}else{return "您已经超过了最大可借阅数";}return "";
}
void CTeacher::showInfo()
{cout << "姓名:" << m_name << " " << "学号:" << m_id << " " << "性别:" << m_gender << " " << "院系:" << m_department << " " << "联系方式:" << m_phone << endl;
}

CStudent.cpp

#include "CStudent.h"
#include<fstream>
using  namespace std;CStudent::CStudent()
{m_class = "软件";
}void CStudent::showInfo()
{cout << "姓名:" << m_name << " " << "学号:" << m_id << " " << "性别:" << m_gender << " " << "院系:" << m_department << " " << "班级:" << m_class << " " << "联系方式:" << m_phone << endl;
}string CStudent::borrowBookFromLibrary(CManagementBooks& mb, string name)
{int all = mb.checkBorrowedBook(m_id);if (all < 3){if (mb.borrow(name)){ofstream writeFile("borrowedBook.txt", ios::app);mb.setMapValue(m_id, name);writeFile << endl << m_id << " " << name;writeFile.close();}else{return "此书数量不足或暂无此书";}}else{return "您已经超过了最大可借阅数";}return "";}void CStudent::setClass(string Class)
{m_class = Class;
}string CStudent::getClass()
{return m_class;
}

CManagementBooks.cpp

#include "CManagementBooks.h"
#include<algorithm>
using namespace std;
static bool cmp(CBook book1, CBook book2)
{return book1.getNum() > book2.getNum();
}
void CManagementBooks::showCurrentAllBook()
{sort(m_books.begin(), m_books.end(), cmp);for (int i = 0; i < m_allBookNum; i++){cout << "书名:" << m_books[i].getName() << " " << "剩余数量" << m_books[i].getNum() << endl;}
}CManagementBooks::CManagementBooks()
{m_allBookNum = 0;
}void CManagementBooks::addBook(CBook book)
{int flag = 0;for (int i = 0; i < m_allBookNum; i++){if (m_books[i].getName() == book.getName()){flag = 1;m_books[i].setNum(m_books[i].getNum() + book.getNum());ofstream writeFile("library.txt", ios::out);for (int i = 0; i < m_allBookNum; i++){writeFile << m_books[i];}writeFile.close();break;}}if (!flag){ofstream writeFile("library.txt", ios::app);m_books.push_back(book);m_allBookNum++;writeFile << book;writeFile.close();}
}int CManagementBooks::getAllBookNum()
{return m_allBookNum;
}void CManagementBooks::showAllBooksInfo()
{sort(m_books.begin(), m_books.end(), cmp);for (int i = 0; i < m_allBookNum; i++){m_books[i].showInfo();}
}bool CManagementBooks::borrow(string name)
{for (int i = 0; i < m_allBookNum; i++){if (m_books[i].getName() == name){if (m_books[i].getNum() >= 1){m_books[i].setNum(m_books[i].getNum() - 1);ofstream writeFile("library.txt");for (int i = 0; i < m_allBookNum; i++){writeFile << m_books[i];}writeFile.close();return true;}else{return false;}}}return false;
}void CManagementBooks::Return(string id, string bookName)
{CBook book;book.setName(bookName);addBook(book);ofstream writeFile("borrowedBook.txt", ios::out);for (auto iter = m_outBookMap.begin(); iter != m_outBookMap.end(); ++iter){if (iter->first == id && iter->second == bookName){m_outBookMap.erase(iter);break;}}for (auto map : m_outBookMap){writeFile << endl << map.first << " " << map.second;}writeFile.close();
}void CManagementBooks::initOutBook()
{ifstream readFile("borrowedBook.txt");if (!readFile.is_open()){cout << "查看全体借书情况数据读取出错" << endl;}else{while (!readFile.eof()){string studentId, bookName;readFile >> studentId >> bookName;m_outBookMap.insert(pair<string, string>(studentId, bookName));}}readFile.close();}void CManagementBooks::checekOutBook()
{for (auto map : m_outBookMap){cout << "借阅人学工号:" << map.first << " " << "借阅书名:" << map.second << endl;}
}void CManagementBooks::initBooks()
{ifstream readFile;readFile.open("library.txt");if (!readFile.is_open()){cout << "图书数据读取错误" << endl;readFile.close();return;}while (!readFile.eof()){CBook book;readFile >> book;m_allBookNum++;m_books.push_back(book);}readFile.close();
}int CManagementBooks::checkBorrowedBook(string userId)
{int flag = 0;for (auto map : m_outBookMap){if (userId == map.first){if (!flag){cout << "您已经借的全部图书如下:" << endl;flag++;}else{flag++;}cout << map.second << " ";}}if (!flag){cout << "您目前没有借书" << endl;}cout << "共:" << flag << "本";cout << endl;return flag;
}vector<userInfo> CManagementBooks::viewBorrowerDetails()
{ifstream readFile("teacher.txt");ifstream readFile1("student.txt");vector<userInfo>users;if (!readFile1.is_open() || !readFile.is_open()){cout << "用户数据读取错误" << endl;}while (!readFile1.eof()){string act1, name, department, gender,phone;readFile1 >> act1 >> name >> gender >> department>> phone;if(act1!="" && name != "")users.push_back({ act1,name ,gender ,department,"学生",phone});}while (!readFile.eof()){string act1, name, department, gender, phone;readFile >> act1 >> name >> gender >> department;if (act1 != "" && name != "")users.push_back({ act1,name ,gender ,department,"老师",phone });}readFile.close();readFile1.close();return users;
}bool CManagementBooks::checkTrueBorrow(string id, string bookName)
{for (auto map : m_outBookMap){if (map.first == id && map.second == bookName){return true;}}return false;
}void CManagementBooks::setMapValue(string userId, string bookName)
{m_outBookMap.insert(pair<string, string>(userId, bookName));
}vector<CBook> CManagementBooks::getAllBook()
{return m_books;
}unordered_multimap<string, string> CManagementBooks::getOutBookMap()
{return m_outBookMap;
}

CBook.cpp

#include "CBook.h"
#include <iostream>
#include<fstream>
using namespace std;CBook::CBook()
{string b = "";string randStr = "0123456789X";for (int i = 0; i <= 12; i++){if (i == 1 || i == 5 || i == 11){b += '-';}else{if (i == 12){b += randStr[rand() % 11];}else{b += randStr[rand() % 10];}}}m_num = 1;m_name = "等待戈多";m_author = "xx元";m_isbn = b;m_page = rand();m_pressInfo = "xx出版社";m_price = rand();
}void CBook::setNum(int num)
{m_num = num;
}int CBook::getNum()
{return m_num;
}void CBook::setName(string name)
{m_name = name;
}string CBook::getName()
{return m_name;
}void CBook::setIsbn(string isbn)
{m_isbn = isbn;
}string CBook::getIsbn()
{return m_isbn;
}void CBook::setPressInfo(string perssInfo)
{m_pressInfo = perssInfo;
}string CBook::getPressInfo()
{return m_pressInfo;
}void CBook::setPrice(double price)
{m_price = price;
}double CBook::getPrice()
{return m_price;
}void CBook::setPage(int page)
{m_page = page;}int CBook::getPage()
{return m_page;
}void CBook::setAuthor(string author)
{m_author = author;
}string CBook::getAuthor()
{return m_author;
}void CBook::checkIsnb()
{int sum = 0;for (int i = 0, j = 1; i < m_isbn.size(); i++) {if (m_isbn[i] != '-' && i != 12) {sum += (m_isbn[i] - '0') * j;j++;}}sum %= 11;char c = 'X';if (sum < 10) c = sum + '0';if (m_isbn[12] == c) puts("This book isbn are Right!");else puts("This book isbn are wrong!");
}bool CBook::isBorrowed()
{if (m_num >= 1){m_num--;return true;}return false;
}void CBook::showInfo()
{cout << "作者:" << m_author << " " << "isbn号码:" << m_isbn << " " << "书本名称:" << m_name << " "<< "总页数:" << m_page << " " << "出版社:" << m_pressInfo << " " << "价格:" << m_price<< " " << "剩余本数:" << m_num << endl;
}std::ostream& operator <<(std::ostream& os, const CBook& book)
{os << endl << book.m_name << " " << book.m_isbn << " " << book.m_pressInfo << " " << book.m_price << " " << book.m_page << " " << book.m_author << " " << book.m_num;return os;
}std::istream& operator>>(std::istream& is, CBook& book)
{is >> book.m_name >> book.m_isbn >> book.m_pressInfo >> book.m_price >> book.m_page >> book.m_author >> book.m_num;return is;
}}

userPage.cpp

#include "UserPage.h"
#include <QMessageBox>
#include <QFont>UserPage::UserPage(QWidget* parent) : QWidget(parent)
{// 创建主布局QVBoxLayout* mainLayout = new QVBoxLayout(this);setWidgetFont(this);// 创建选项卡部件tabWidget = new QTabWidget(this);// 借书选项卡// 创建显示当前图书列表的QTableWidgetQTableWidget* currentBooksList = new QTableWidget(borrowTab);currentBooksList->setObjectName("currentBooksList");setWidgetFont(currentBooksList);// 创建标签用于说明图书列表QLabel* currentBooksLabel = createStyledLabel("当前图书列表:");borrowLayout->addWidget(currentBooksLabel);borrowLayout->addWidget(currentBooksList);// 添加书名输入标签和输入框QLabel* bookNameLabel = createStyledLabel("输入要借的书名:");borrowLayout->addWidget(bookNameLabel);borrowLayout->addWidget(bookNameEdit);borrowLayout->addWidget(borrowButton);tabWidget->addTab(borrowTab, "借书");// 查看借书情况选项卡// 还书选项卡mainLayout->addWidget(tabWidget);// 设置编码codec = QTextCodec::codecForName("GBK");// 连接借书按钮点击事件connect(borrowButton, &QPushButton::clicked, this, &UserPage::onBorrowButtonClicked);// 连接还书按钮点击事件connect(returnButton, &QPushButton::clicked, this, &UserPage::onReturnButtonClicked);connect(tabWidget, &QTabWidget::currentChanged, [this](int index) {if (index == tabWidget->indexOf(borrowedBooksTab)) {onViewBorrowedBooksClicked();}else if (index == tabWidget->indexOf(returnTab)) {onShowBorrowedBooksForReturnClicked();}else if (index == tabWidget->indexOf(borrowTab)) {onBorrowTabClicked();}});mb.initBooks();mb.initOutBook();initCurrentBookList();
}void UserPage::onBorrowButtonClicked()
{QString bookName = bookNameEdit->text();if (bookName.isEmpty()){QMessageBox::warning(this,"借书提示","请输入要借的书名");return;}std::string allow = user->borrowBookFromLibrary(mb, codec->fromUnicode(bookName).toStdString());if (allow != ""){QMessageBox::critical(this, "借书失败", codec->toUnicode(allow.c_str()));return;}// 尝试获取当前图书列表部件QTableWidget* currentBooksList = this->findChild<QTableWidget*>("currentBooksList");if (currentBooksList){QList<QTableWidgetItem*> foundItems = currentBooksList->findItems(bookName, Qt::MatchExactly);if (!foundItems.isEmpty()){QTableWidgetItem* itemToModify = foundItems.first();int row = currentBooksList->row(itemToModify);// 获取当前可借数量并减1QTableWidgetItem* numItem = currentBooksList->item(row, 6);int currentNum = numItem->text().toInt();if (currentNum > 0){currentNum--;numItem->text() = QString::number(currentNum);QMessageBox::information(this, "借书成功", "已成功借阅 " + bookName);// 重新加载图书数据onBorrowTabClicked();}}}
}void UserPage::onReturnButtonClicked()
{QList<QTableWidgetItem*> selectedItems = returnableBooksList->selectedItems();if (selectedItems.isEmpty()){QMessageBox::warning(this, "还书提示", "请选择要归还的书籍");return;}// 使用QSet来避免重复添加书籍IDQSet<QString> addedBookIds;QList<QString> bookIds;for (QTableWidgetItem* item : selectedItems){// 获取该项所在行的索引int row = returnableBooksList->row(item);// 获取该行第二列的表格项(索引为1)QTableWidgetItem* secondColumnItem = returnableBooksList->item(row, 1);if (secondColumnItem){QString bookId = secondColumnItem->text();if (!addedBookIds.contains(bookId)){bookIds.append(bookId);addedBookIds.insert(bookId);}}}// 执行还书操作,假设user有还书相关函数,mb是管理书籍相关数据的对象std::string result;for (const QString& bookId : bookIds){// 这里调用还书函数,根据实际情况可能需要调整参数传递方式result = user->returnBook(mb, codec->fromUnicode(bookId).toStdString());if (result != ""){break;}}if (result == ""){// 还书成功,更新界面显示QMessageBox::information(this, "还书成功", "已成功归还所选书籍");// 重新加载当前用户借阅的书籍列表,以更新界面显示onShowBorrowedBooksForReturnClicked();}else{QMessageBox::warning(this, "还书失败", codec->toUnicode(result.c_str()));}
}QLabel* UserPage::createStyledLabel(const QString& text)
{QLabel* label = new QLabel(text);setWidgetFont(label);return label;
}void UserPage::setWidgetFont(QWidget* widget)
{QFont font;font.setFamily("宋体");font.setPointSize(16);widget->setFont(font);
}void UserPage::initCurrentBookList()
{// 清空之前的显示内容QTableWidget* currentBooksList = this->findChild<QTableWidget*>("currentBooksList");currentBooksList->setEditTriggers(QAbstractItemView::NoEditTriggers);if (currentBooksList){// 设置表格列数和表头currentBooksList->setColumnCount(7);QStringList headers;headers << "作者" << "isbn号码" << "书本名称" << "总页数" << "出版社" << "价格" << "剩余本数";currentBooksList->setHorizontalHeaderLabels(headers);// 设置表格字体大小QFont tableFont;tableFont.setPointSize(10); // 设置字体大小为12,你可以根据需要调整这个值currentBooksList->setFont(tableFont);// 设置表头字体大小QHeaderView* headerView = currentBooksList->horizontalHeader();QFont headerFont;headerFont.setPointSize(10); // 设置字体大小为12,你可以根据需要调整这个值headerView->setFont(headerFont);// 添加书籍信息for (auto& book : mb.getAllBook()){int row = currentBooksList->rowCount();currentBooksList->insertRow(row);QTableWidgetItem* authorItem = new QTableWidgetItem(codec->toUnicode(book.getAuthor().c_str()));QTableWidgetItem* isbnItem = new QTableWidgetItem(codec->toUnicode(book.getIsbn().c_str()));QTableWidgetItem* nameItem = new QTableWidgetItem(codec->toUnicode(book.getName().c_str()));QTableWidgetItem* pressInfoItem = new QTableWidgetItem(codec->toUnicode(book.getPressInfo().c_str()));QTableWidgetItem* pagesItem = new QTableWidgetItem((QString::number(book.getPage())));QTableWidgetItem* priceItem = new QTableWidgetItem((QString::number(book.getPrice())));QTableWidgetItem* numItem = new QTableWidgetItem(QString::number(book.getNum()));currentBooksList->setItem(row, 0, authorItem);currentBooksList->setItem(row, 1, isbnItem);currentBooksList->setItem(row, 2, nameItem);currentBooksList->setItem(row, 3, pagesItem);currentBooksList->setItem(row, 4, pressInfoItem);currentBooksList->setItem(row, 5, priceItem);currentBooksList->setItem(row, 6, numItem);}}// 设置列宽,这里的宽度值可以根据实际情况调整currentBooksList->setColumnWidth(0, 150);currentBooksList->setColumnWidth(1, 150);currentBooksList->setColumnWidth(2, 200);currentBooksList->setColumnWidth(3, 100);currentBooksList->setColumnWidth(4, 200);currentBooksList->setColumnWidth(5, 100);currentBooksList->setColumnWidth(6, 100);
}void UserPage::onUserUpdated(CUser* user)
{this->user = user;
}void UserPage::onViewBorrowedBooksClicked()
{// 清空之前的显示内容borrowedBooksList->clearContents();borrowedBooksList->setRowCount(0);// 设置表格列数和表头borrowedBooksList->setColumnCount(2);borrowedBooksList->setEditTriggers(QAbstractItemView::NoEditTriggers);QStringList headers;headers << "借阅者账号" << "借阅图书";borrowedBooksList->setHorizontalHeaderLabels(headers);// 获取当前用户的借书信息列表std::unordered_multimap<string, string> borrowedBooks = mb.getOutBookMap();// 添加借书信息到表格中for (const auto& book : borrowedBooks) {if (book.first != user->getId()) continue;int row = borrowedBooksList->rowCount();borrowedBooksList->insertRow(row);QTableWidgetItem* bookNameItem = new QTableWidgetItem(codec->toUnicode(book.first.c_str()));QTableWidgetItem* authorItem = new QTableWidgetItem(codec->toUnicode(book.second.c_str()));borrowedBooksList->setItem(row, 0, bookNameItem);borrowedBooksList->setItem(row, 1, authorItem);}borrowedBooksList->setColumnWidth(0, 500);borrowedBooksList->setColumnWidth(1, 500);
}void UserPage::onShowBorrowedBooksForReturnClicked()
{// 设置选择模式为单选整行returnableBooksList->setSelectionBehavior(QAbstractItemView::SelectRows);returnableBooksList->setSelectionMode(QAbstractItemView::SingleSelection);
}
void UserPage::onBorrowTabClicked()
{// 清空之前的显示内容}

loginPage.cpp

#include "LoginPage.h"LoginPage::LoginPage(QWidget* parent) : QWidget(parent)
{// 创建用户名输入框usernameEdit = new QLineEdit(this);// 创建密码输入框,并设置为密码模式passwordEdit = new QLineEdit(this);setWidgetFontAndSize(passwordEdit);setInputBoxHeight(passwordEdit, 35); // 设置用户名输入框高度// 创建角色选择下拉框roleLoginComboBox = new QComboBox(this);roleLoginComboBox->addItems({ "学生", "老师", "管理员" });setWidgetFont(roleLoginComboBox);// 创建登录按钮loginButton = new QPushButton("登录", this);setWidgetFont(loginButton);// 创建注册按钮registerButton = new QPushButton("注册", this);setWidgetFont(registerButton);// 设置布局QVBoxLayout* mainLayout = new QVBoxLayout(this);QHBoxLayout* usernameLayout = new QHBoxLayout();usernameLayout->addWidget(createStyledLabel("用户名:"));usernameLayout->addWidget(usernameEdit);mainLayout->addLayout(usernameLayout);QHBoxLayout* passwordLayout = new QHBoxLayout();passwordLayout->addWidget(createStyledLabel("密码:"));passwordLayout->addWidget(passwordEdit);mainLayout->addLayout(passwordLayout);QHBoxLayout* roleLoginLayout = new QHBoxLayout();roleLoginLayout->addWidget(createStyledLabel("角色:"));roleLoginLayout->addWidget(roleLoginComboBox);mainLayout->addLayout(roleLoginLayout);QHBoxLayout* buttonLayout = new QHBoxLayout();buttonLayout->addWidget(loginButton);buttonLayout->addWidget(registerButton);mainLayout->addLayout(buttonLayout);// 连接按钮点击事件到相应的槽函数connect(loginButton, &QPushButton::clicked, this, &LoginPage::onLoginButtonClicked);connect(registerButton, &QPushButton::clicked, this, &LoginPage::onRegisterButtonClicked);
}void LoginPage::onLoginButtonClicked()
{QString account = usernameEdit->text();QString password = passwordEdit->text();QString role = roleLoginComboBox->currentText();CUser* user = nullptr;CAdministrator admin;if (account.isEmpty() || password.isEmpty()){QMessageBox::critical(this, "错误", "用户名或密码不能为空!");return;}std::ifstream readFile;std::ifstream readFile1;if (role == "老师"){readFile.open("teacherLogin.txt");readFile1.open("teacher.txt");user = new CTeacher();}else if(role == "学生"){readFile1.open("student.txt");readFile.open("studentLogin.txt");user = new CStudent();}else{readFile1.open("admin.txt");readFile.open("adminLogin.txt");int flag = 0;while (!readFile.eof()){std::string act;readFile >> act;if (act == account.toStdString()){std::string pwd;readFile >> pwd;if (pwd == password.toStdString()){QMessageBox::about(this, "登录成功", "登录成功!");flag = 1;while (!readFile1.eof()){readFile1 >> admin;if (admin.getId() == act){emit adminUpdated(admin);break;}}break;}}}if (!flag){QMessageBox::critical(this, "错误", "用户名或密码错误!");return;}emit adminLoginSuccess();readFile.close();readFile1.close();return;}int flag = 0;while (!readFile.eof()){std::string act;readFile >> act;if (act == account.toStdString()){std::string pwd;readFile >> pwd;if (pwd == password.toStdString()){QMessageBox::about(this, "登录成功", "登录成功!");flag = 1;while (!readFile1.eof()){readFile1 >> user;if (user != nullptr && user->getId() == act){emit userUpdated(user);break;}}break;}}}if (!flag){QMessageBox::critical(this, "错误", "用户名或密码错误!");return;}emit loginSuccess();readFile.close();readFile1.close();
}void LoginPage::onRegisterButtonClicked()
{emit registerButtonClicked();
}// 设置输入框宽度的函数
void LoginPage::setInputBoxWidth(QWidget* widget, int width)
{widget->setFixedWidth(width);
}// 创建带有指定样式的标签函数
QLabel* LoginPage::createStyledLabel(const QString& text)
{QLabel* label = new QLabel(text);setWidgetFont(label);return label;
}// 设置输入框字体和大小的函数
void LoginPage::setWidgetFontAndSize(QWidget* widget)
{QFont font;font.setFamily("宋体");font.setPointSize(16);widget->setFont(font);
}
void LoginPage::setInputBoxHeight(QWidget* widget, int height)
{widget->setFixedHeight(height);
}// 设置按钮字体的函数
void LoginPage::setWidgetFont(QWidget* widget)
{QFont font;font.setFamily("宋体");font.setPointSize(16);widget->setFont(font);
}

register.cpp

#include "RegistrationPage.h"
#include <QFont>RegistrationPage::RegistrationPage(QWidget* parent) : QWidget(parent)
{// 创建用户名输入框usernameEdit = new QTextEdit(this);setWidgetFontAndSize(usernameEdit);setInputBoxHeight(usernameEdit, 35); // 设置用户名输入框高度// 创建姓名输入框nameEdit = new QTextEdit(this);setWidgetFontAndSize(nameEdit);setInputBoxHeight(nameEdit, 35); // 设置姓名输入框高度// 创建部门输入框departmentEdit = new QTextEdit(this);setWidgetFontAndSize(departmentEdit);setInputBoxHeight(departmentEdit, 35); // 设置部门输入框高度// 创建电话输入框phoneEdit = new QTextEdit(this);setWidgetFontAndSize(phoneEdit);setInputBoxHeight(phoneEdit, 35); // 设置电话输入框高度// 创建密码输入框,并设置为密码模式passwordEdit = new QTextEdit(this);setWidgetFontAndSize(passwordEdit);setInputBoxHeight(passwordEdit, 35); // 设置密码输入框高度// 创建性别选择下拉框genderComboBox = new QComboBox(this);genderComboBox->addItems({ "男", "女" });setWidgetFont(genderComboBox);// 创建角色选择下拉框roleLoginComboBox = new QComboBox(this);roleLoginComboBox->addItems({ "学生", "老师", "管理员" });setWidgetFont(roleLoginComboBox);// 创建注册按钮registerButton = new QPushButton("注册", this);setWidgetFont(registerButton);// 创建登录按钮loginButton = new QPushButton("登录", this);setWidgetFont(loginButton);// 连接注册按钮的点击事件到槽函数connect(registerButton, &QPushButton::clicked, this, &RegistrationPage::onRegisterButtonClicked);connect(loginButton, &QPushButton::clicked, this, &RegistrationPage::onLoginButtonClicked); // 连接登录按钮点击事件
}void RegistrationPage::onRegisterButtonClicked()
{QString act = usernameEdit->toPlainText();QString name = nameEdit->toPlainText();QString department = departmentEdit->toPlainText();QString phone = phoneEdit->toPlainText();QString pwd = passwordEdit->toPlainText();QString gender = genderComboBox->currentText();QString role = roleLoginComboBox->currentText();if (act == "" || name == "" || department == "" || phone == ""){QMessageBox::critical(this, "错误", "请将信息补充完整!");return;}if (role == "管理员"){QMessageBox::critical(this, "错误", "管理员无法注册!");return;}std::ofstream writeFile;std::ofstream writeFile1;if (role == "老师"){writeFile1.open("teacher.txt", std::ios::app);writeFile.open("teacherLogin.txt", std::ios::app);}else{writeFile1.open("student.txt", std::ios::app);writeFile.open("studentLogin.txt", std::ios::app);}writeFile << std::endl << act.toStdString() << " " << pwd.toStdString();writeFile1 << std::endl << act.toStdString() << " " << name.toStdString() << " " << gender.toStdString() << " " << department.toStdString() << " " << phone.toStdString();writeFile.close();writeFile1.close();QMessageBox::about(this, "注册成功", "快去登录系统吧");// 发射注册成功信号emit registrationSuccess();
}void RegistrationPage::onLoginButtonClicked()
{}// 设置控件字体和字号的函数
void RegistrationPage::setWidgetFont(QWidget* widget)
{QFont font;font.setFamily("宋体");font.setPointSize(16);widget->setFont(font);
}// 设置输入框宽度的函数
void RegistrationPage::setInputBoxWidth(QWidget* widget, int width)
{widget->setFixedWidth(width);
}// 创建带有指定样式的标签函数
QLabel* RegistrationPage::createStyledLabel(const QString& text)
{QLabel* label = new QLabel(text);setWidgetFont(label);return label;
}// 设置输入框字体和大小的函数
void RegistrationPage::setWidgetFontAndSize(QWidget* widget)
{QFont font;font.setFamily("宋体");font.setPointSize(16);widget->setFont(font);
}void RegistrationPage::setInputBoxHeight(QWidget* widget, int height)
{widget->setFixedHeight(height);
}

CAdministrator.cpp

cout<<需要完整源码,只需10<<endl;
cout<<源码+讲解,只需25元 需要请加位心:ch18384322303,备注:图书管理系统GUI源码<<endl;
cout<<讲解包括:<<endl;
cout<<项目讲解视频(如何运行,环境安装,代码思路等)、qt安装流程、qt安装包<<endl;
cout<<本项目问题答疑(不限次数)<<endl;

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/892319.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

使用 NestJS 构建高效且模块化的 Node.js 应用程序,从安装到第一个 API 端点:一步一步指南

一、安装 NestJS 要开始构建一个基于 NestJS 的应用&#xff0c;首先需要安装一系列依赖包。以下是必要的安装命令&#xff1a; npm i --save nestjs/core nestjs/common rxjs reflect-metadata nestjs/platform-express npm install -g ts-node包名介绍nestjs/coreNestJS 框…

鸿蒙面试 2025-01-09

鸿蒙分布式理念&#xff1f;&#xff08;个人认为理解就好&#xff09; 鸿蒙操作系统的分布式理念主要体现在其独特的“流转”能力和相关的分布式操作上。在鸿蒙系统中&#xff0c;“流转”是指涉多端的分布式操作&#xff0c;它打破了设备之间的界限&#xff0c;实现了多设备…

Mysql--基础篇--SQL(DDL,DML,窗口函数,CET,视图,存储过程,触发器等)

SQL&#xff08;Structured Query Language&#xff0c;结构化查询语言&#xff09;是用于管理和操作关系型数据库的标准语言。它允许用户定义、查询、更新和管理数据库中的数据。SQL是一种声明性语言&#xff0c;用户只需要指定想要执行的操作&#xff0c;而不需要详细说明如何…

SQL 幂运算 — POW() and POWER()函数用法详解

POW() and POWER()函数用法详解 POW() 和 POWER() —计算幂运算&#xff08;即一个数的指定次方&#xff09;的函数。 这两个函数是等价的&#xff0c;功能完全相同&#xff0c;只是名字不同。 POW(base, exponent); POWER(base, exponent); base&#xff1a;底数。exponen…

Elasticsearch:聚合操作

这里写目录标题 一、聚合的概述二、聚合的分类1、指标聚合&#xff08;Metric Aggregation&#xff09;2、桶聚合&#xff08;Bucket Aggregation&#xff09;3、管道聚合&#xff08;Pipeline Aggregation&#xff09; 三、ES聚合分析不精准原因分析四、聚合性能优化1、ES聚合…

Ubuntu 磁盘修复

Ubuntu 磁盘修复 在 ubuntu 文件系统变成只读模式&#xff0c;该处理呢&#xff1f; 文件系统内部的错误&#xff0c;如索引错误、元数据损坏等&#xff0c;也可能导致系统进入只读状态。磁盘坏道或硬件故障也可能引发文件系统只读的问题。/etc/fstab配置错误&#xff0c;可能…

重新整理机器学习和神经网络框架

本篇重新梳理了人工智能&#xff08;AI&#xff09;、机器学习&#xff08;ML&#xff09;、神经网络&#xff08;NN&#xff09;和深度学习&#xff08;DL&#xff09;之间存在一定的包含关系&#xff0c;以下是它们的关系及各自内容,以及人工智能领域中深度学习分支对比整理。…

LabVIEW瞬变电磁接收系统

利用LabVIEW软件与USB4432采集卡开发瞬变电磁接收系统。系统通过改进硬件配置与软件编程&#xff0c;解决了传统仪器在信噪比低和抗干扰能力差的问题&#xff0c;实现了高精度的数据采集和处理&#xff0c;特别适用于地质勘探等领域。 ​ 项目背景&#xff1a; 瞬变电磁法是探…

Redis 优化秒杀(异步秒杀)

目录 为什么需要异步秒杀 异步优化的核心逻辑是什么&#xff1f; 阻塞队列的特点是什么&#xff1f; Lua脚本在这里的作用是什么&#xff1f; 异步调用创建订单的具体逻辑是什么&#xff1f; 为什么要用代理对象proxy调用createVoucherOrder方法&#xff1f; 对于代码的详细…

C++笔记之`size_t`辨析

C++笔记之size_t辨析 code review! 文章目录 C++笔记之`size_t`辨析一.什么是 `size_t`?二.`size_t` 的来源和设计目的三.`size_t` 的应用场景四.`size_t` 的优点五.`size_t` 的缺点和注意事项六.`size_t` 和其他类型的比较七.总结与建议在 C/C++ 中,size_t 是一个非常重要的…

MySQL表的增删查改(下)——Update(更新),Delete(删除)

文章目录 Update将孙悟空同学的数学成绩修改为80分将曹孟德同学的数学成绩变更为 60 分&#xff0c;语文成绩变更为 70 分将总成绩倒数前三的 3 位同学的数学成绩加上 30 分将所有同学的语文成绩更新为原来的 2 倍 Delete删除数据删除孙悟空同学的考试成绩删除整张表数据 截断表…

大语言模型训练的数据集从哪里来?

继续上篇文章的内容说说大语言模型预训练的数据集从哪里来以及为什么互联网上的数据已经被耗尽这个说法并不专业&#xff0c;再谈谈大语言模型预训练数据集的优化思路。 1. GPT2使用的数据集是WebText&#xff0c;该数据集大概40GB&#xff0c;由OpenAI创建&#xff0c;主要内…

【hadoop学习遇见的小问题】clone克隆完之后网络连接不上问题解决

vi /etc/udev/rules.d/70-persistent-net.rules注释掉第一行 第二行的eth1 改为eth0 由上图也可以看到物理地址 记录下来在网卡中修改物理地址 vi /etc/sysconfig/network-scripts/ifcfg-eth0修改完之后 重启reboot 即可

PDFMathTranslate: Star13.8k,一款基于AI的PDF文档全文双语翻译PDF文档全文双语翻译,保留格式神器,你应该需要它

嗨&#xff0c;大家好&#xff0c;我是小华同学&#xff0c;关注我们获得“最新、最全、最优质”开源项目和高效工作学习方法 PDFMathTranslate是一个开源项目&#xff0c;旨在为用户提供便捷的PDF科学论文翻译解决方案。它不仅能够翻译文本&#xff0c;还能保留公式、图表、目…

对话|全年HUD前装将超330万台,疆程技术瞄准人机交互“第一屏”

2024年&#xff0c;在高阶智驾进入快速上车的同时&#xff0c;座舱人机交互也在迎来新的增长点。Chat GPT、AR-HUD、车载投影等新配置都在带来新增量机会。 高工智能汽车研究院监测数据显示&#xff0c;2024年1-10月&#xff0c;中国市场&#xff08;不含进出口&#xff09;乘用…

【机器学习案列】学生抑郁可视化及预测分析

&#x1f9d1; 博主简介&#xff1a;曾任某智慧城市类企业算法总监&#xff0c;目前在美国市场的物流公司从事高级算法工程师一职&#xff0c;深耕人工智能领域&#xff0c;精通python数据挖掘、可视化、机器学习等&#xff0c;发表过AI相关的专利并多次在AI类比赛中获奖。CSDN…

微服务保护——Sentinel

什么是微服务保护&#xff1f; 微服务保护是一系列用于保障微服务架构稳定、可靠运行的策略与技术手段&#xff0c;在复杂的分布式微服务系统里&#xff0c;它能避免局部故障引发连锁反应&#xff0c;从而维持整个系统的可用性&#xff0c;主要涵盖以下几个关键部分&#xff1a…

福建双色荷花提取颜色

提取指定颜色 HSV双色荷花代码验证 参照《OpenCV图像处理技术》 HSV 要用HSV的色调、饱和度和亮度来提取指定颜色。 双色荷花 农林大学金山校区观音湖 代码 import cv2 import numpy as npimgcv2.imread("./sucai6/hua.jpg") cv2.imshow("SRC",img) h…

tdengine数据库使用java连接

1 首先给你的项目添加依赖 <dependency> <groupId>com.taosdata.jdbc</groupId> <artifactId>taos-jdbcdriver</artifactId> <version>3.4.0</version> <!-- 表示依赖不会传递 --> </dependency> 注意&am…

MIUI显示/隐藏5G开关的方法,信号弱时开启手机Wifi通话方法

5G网速虽快&#xff0c;手机功耗也大。 1.取消MIUI强制的5G&#xff0c;手动设置4G的方法&#xff01; 【小米澎湃OS, Xiaomi HyperOS显示/隐藏5G开关的方法】 1.1.小米MIUI系统升级后&#xff0c;被强制连5G&#xff0c;手动设置开关被隐藏&#xff0c;如下图&#xff1a; 1…