第九章 管理员界面
https://gitee.com/mayonaka/LibraryManageSystem
百度云:https://pan.baidu.com/s/1G95yPyGG080b6yXcjc8B0g
提取码:4q8b
到上一章为止,我们已经实现了登录界面,用户界面,在用户界面中还实现了借书界面和还书界面。这一章,我们将介绍管理员界面。在Login界面中,当用户输入用户名和密码,按下登录按钮后,会进入LoginSystemSLot函数,在函数中我们把用户输入的用户名,密码和数据库中存储的用户名,密码,一个一个地比较,如果有相同的,则说明用户的输入正确,这时候再判断输入的用户名是管理员还是普通用户,在本项目中,为了方便起见,直接通过用户名为“root”来判断是否是管理员。
在实现Login界面时,为了能体现登录的效果,我们已经建立了一个名为AdminInterface的Qt设计师界面类,界面模板为Widget,类名是AdminInterface。
首先现在设计一下管理员界面的界面:
管理员界面的主要控件是一个Tab Widget,在第一个Tab我们在里面显示所有书籍,提供添加书籍,删除书籍的功能。在第二个Tab我们在里面显示所有用户,并提供添加用户和删除用户的功能。
Tab1中有一个Tree Widget,两个Push Button:
1. Tree Widtet:显示文字:无,变量名:bookTreeWidget。
2. Push Button:显示文字:“添加书籍”,变量名:addBookPushButton。
3. Push Button:显示文字:“删除书籍”,变量名:deleteBookPushButton。
Tab2与Tab1一样,也是一个TreeWidget,两个Push Button:
1. Tree Widget:显示文字:无,变量名:userTreeWidget。
2. Push Button:显示文字:“添加用户”,变量名:addUserPushButton。
3. Push Button:显示文字:“删除用户”,变量名:deleteUserPushButton。
下面开始介绍AdminInterface的声明:
类的属性:
1. user:储存所有用户的变量。
2. addUser:储存添加的用户。
3. deleteUser:储存删除的用户。
4. book:储存所有书的变量。
5. addBook:储存添加的书。
6. deleteBook:储存删除的书。
类的方法:
1. ShowBookInfoSlot:显示书的详细信息的槽函数,当用户双击一行书时,会打开一个界面,在这个界面上详细显示书的信息。
2. AddBookSlot:添加书的槽函数,当用户点击添加书籍按钮时,会打开添加书籍的界面。在界面中填写添加的书的详细信息。
3. DeleteBookSlot:删除书的槽函数,当用户点击删除书籍按钮时,会打开删除书籍界面,在界面中显示所有书籍,让用户选择要删除的书。
4. AddUserSlot:添加用户槽函数,当用户点击添加用户按钮时,打开添加用户界面,用户在界面上输入添加的用户的详细信息。
5. DeleteUserSlot:删除用户槽函数,当用户点击删除用户按钮时,打开删除用户界面,在界面上显示所有用户,让用户选择要删除的用户。
6. ShowAllBooks:在bookTreeWidget上显示所有的书。
7. ShowAllUsers:在userTreeWidget上显示所有用户。
#include <QWidget>
#include "user.h"
#include "book.h"
#include "QTreeWidgetItem"
namespace Ui {
class AdminInterface;
}
class AdminInterface : public QWidget
{
Q_OBJECT
public:
explicit AdminInterface(QWidget *parent = 0);
~AdminInterface();
public slots:
// 显示书的详细信息
void ShowBookInfoSlot(QTreeWidgetItem* item, int column);
// 添加书籍
void AddBookSlot();
// 删除书籍
void DeleteBookSlot();
// 添加用户
void AddUserSlot();
// 删除用户
void DeleteUserSlot();
// 显示所有的书籍信息
void ShowAllBooks();
// 显示所有用户的信息
void ShowAllUsers();
private:
Ui::AdminInterface *ui;
// 储存所有的用户信息
User* user = NULL;
// 储存所有书的信息
Book* book = NULL;
// 储存添加的用户的信息
User* addUser = NULL;
// 储存删除的用户的信息
User* deleteUser = NULL;
// 储存添加的书信息
Book* addBook = NULL;
// 储存删除的书信息
Book* deleteBook = NULL;
};
类的实现:
1. 在构造函数中,先设置一下窗口标题,Tab Widget的两个tab的标题,然后初始化一下变量,最后连接一下信号与槽。注意addBook,addUser的头节点的id要设为1,表示链表中的信息要存到数据库中,deleteBook,deleteUser的头节点的id要设为-1,表示链表中的信息要从数据中删掉。这四个变量都不需要调用Init函数,Init函数是从数据库中读取信息来初始化变量的,而这四个变量是用来回写数据库的。
2. ShowBookInfoSlot:该函数的作用是申请一个BookInfo界面,并让这个界面显示出来,BookInfo界面已经在之前的内容中实现过了,这里只要new一个BookInfo对象,并调用show函数就行了。
3. AddBookSlot:等AddBook界面实现后,再来实现。
4. DeleteBookSlot:等DeleteBook界面实现后,再来实现。
5. AddUserSlot:等AddUser界面实现后,再来实现。
6. DeleteUserSlot:等DeleteUser界面实现后,再来实现。
7. ShowAllBooks:该函数的作用是在Book Tree Widget上显示所有的书,只要遍历book链表,把每个节点的信息转换成QTreeWidgetItem对象,插入Tree Widget即可。
8. ShowAllUsers:该函数的作用是在User Tree Widget上显示所有的用户,只要遍历user链表,把每个节点的信息转换成QTreeWidgetItem对象,插入Tree Widget即可。
#include "admininterface.h"
#include "ui_admininterface.h"
#include "bookinfo.h"
AdminInterface::AdminInterface(QWidget *parent) :
QWidget(parent),
ui(new Ui::AdminInterface)
{
ui->setupUi(this);
this->setWindowTitle(QString::fromLocal8Bit("管理员界面"));
// 设置两个tab的标题
ui->tabWidget->setTabText(0, QString::fromLocal8Bit("书籍管理"));
ui->tabWidget->setTabText(1, QString::fromLocal8Bit("用户管理"));
this->book = new Book();
this->book->Init();
this->user = new User();
this->user->Init();
// 链表头节点的id值表示添加或删除
// 1表示把链表中的信息添加到数据库中
// -1表示把链表中的信息从数据库中删掉
this->addBook = new Book();
this->addBook->GetNode(-1)->SetId(1);
this->deleteBook = new Book();
this->deleteBook->GetNode(-1)->SetId(-1);
this->addUser = new User();
this->addUser->GetNode(-1)->SetId(1);
this->deleteUser = new User();
this->deleteUser->GetNode(-1)->SetId(-1);
// 双击book Tree Widget 的每一行,会显示书的详细信息
QObject::connect(ui->bookTreeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)),
this, SLOT(ShowBookInfoSlot(QTreeWidgetItem*,int)));
// 点击添加书籍按钮,会进入添加书籍的界面
QObject::connect(ui->addBookPushButton, SIGNAL(clicked(bool)),
this, SLOT(AddBookSlot()));
// 点击删除书籍按钮,会进入删除书籍的界面
QObject::connect(ui->deleteBookPushButton, SIGNAL(clicked(bool)),
this, SLOT(DeleteBookSlot()));
// 点击添加用户按钮,会进入添加用户的界面
QObject::connect(ui->addUserPushButton, SIGNAL(clicked(bool)),
this, SLOT(AddUserSlot()));
// 点击删除用户按钮,会进入删除用户的界面
QObject::connect(ui->deleteUserPushButton, SIGNAL(clicked(bool)),
this, SLOT(DeleteUserSlot()));
}
AdminInterface::~AdminInterface()
{
delete ui;
if (this->book != NULL)
{
delete this->book;
this->book = NULL;
}
if (this->user != NULL)
{
delete this->user;
this->user = NULL;
}
if (this->addBook != NULL)
{
delete this->addBook;
this->addBook = NULL;
}
if (this->addUser != NULL)
{
delete this->addUser;
this->addUser = NULL;
}
if (this->deleteBook != NULL)
{
delete this->deleteBook;
this->deleteBook = NULL;
}
if (this->deleteUser != NULL)
{
delete this->deleteUser;
this->deleteUser = NULL;
}
}
void AdminInterface::ShowBookInfoSlot(QTreeWidgetItem *item, int column)
{
BookType* node = (BookType*)this->book->GetNode(item->text(0).toInt());
BookInfo* w = new BookInfo(node);
w->show();
}
void AdminInterface::ShowAllBooks()
{
if (this->book == NULL)
{
return;
}
ui->bookTreeWidget->clear();
BookType* node = (BookType*)this->book->GetNode(-1);
node = (BookType*)node->GetNext();
while (node != NULL)
{
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText(0, QString::number(node->GetId()));
item->setText(1, node->GetName());
item->setText(2, node->GetAuthor());
item->setText(3, node->GetCategory());
ui->bookTreeWidget->insertTopLevelItem(0, item);
node = (BookType*)node->GetNext();
}
}
void AdminInterface::ShowAllUsers()
{
if (this->user == NULL)
{
return;
}
ui->userTreeWidget->clear();
UserType* node = (UserType*)this->user->GetNode(-1);
node = (UserType*)node->GetNext();
while (node != NULL)
{
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText(0, QString::number(node->GetId()));
item->setText(1, node->GetName());
item->setText(2, node->GetPassword());
ui->userTreeWidget->insertTopLevelItem(0, item);
node = (UserType*)node->GetNext();
}
}
void AdminInterface::AddBookSlot()
{
}
void AdminInterface::DeleteBookSlot()
{
}
void AdminInterface::AddUserSlot()
{
}
void AdminInterface::DeleteUserSlot()
{
}