第八章 借书与还书
https://gitee.com/mayonaka/LibraryManageSystem
百度云:https://pan.baidu.com/s/1G95yPyGG080b6yXcjc8B0g
提取码:4q8b
还书界面与借书界面类似,只不过是显示的时候不是显示所有书籍,而是显示用户已借阅的书籍。其他方面都类似。
类的声明:
1. 新建一个C++类,类名为ReturnBook,public继承Form类,在returnbook.cpp中添加#include “ui_form.h”,否则不能使用ui
2. 类的属性:
a) userBook:储存用户已借阅的书。
b) returnBook:储存在还书界面还的书。
c) book:储存所有书的信息
3. 类的方法:
a) ShowAllUserBooks:显示所有用户已借阅的书,让用户还书。
b) ReturnBookSlot:当用户点击确定按钮后,进入此函数,把用户还的书添加到returnBook链表中去。以便以后从数据库中删掉用户还的书。注意:在类的声明中添加Q_OBJECT,并执行一下qmake,否则父类与子类的信号无法传递。
c) closeEvent:Qt提供的虚函数,用来拦截close事件,让用户自己处理。在此函数中,我们将提示用户是否保存操作,是,则把用户还的书添加到userBook链表中,并把书从数据库中删掉,否,则不进行操作,把returnBook链表清空,返回use界面。
#include "form.h"
#include "userbook.h"
#include "book.h"
class ReturnBook : public Form
{
Q_OBJECT
public:
ReturnBook(UserBook* node, UserBook* node2, Book* node3);
void ShowAllUserBooks();
void closeEvent(QCloseEvent* e);
public slots:
void ReturnBookSlot();
protected:
UserBook* userBook = NULL;
UserBook* returnBook = NULL;
Book* book = NULL;
};
类的实现:
1. 在构造函数中,先设置一下界面的显示,然后初始化变量,最后连接一下槽函数。
2. ShowAllUserBooks:遍历useBook链表,根据书的id,从book链表中找到书的详细信息,并显示到界面上。
3. ReturnBookSlot:遍历Tree Widget,检查复选框是否被选中,如果被选中,则把这本书添加到returnBook链表中。
4. closeEvent:弹出一个对话框,询问用户是否保存操作,是,则把还的书从userBook链表中删掉,并把returnBook链表中的信息从数据库中删掉,否,则清空returnBook链表,返回user界面。
#include <QMessageBox>
#include "returnbook.h"
#include "ui_form.h"
ReturnBook::ReturnBook(UserBook *node, UserBook *node2, Book *node3)
{
this->setWindowTitle(QString::fromLocal8Bit("借书"));
ui->treeWidget->setColumnCount(4);
QStringList header;
header << QString::fromLocal8Bit("书籍编号")
<< QString::fromLocal8Bit("书籍名称")
<< QString::fromLocal8Bit("书籍作者")
<< QString::fromLocal8Bit("书籍类型");
ui->treeWidget->setHeaderLabels(header);
this->userBook = node;
this->returnBook = node2;
this->book = node3;
QWidget::connect(ui->confirmPushButton, SIGNAL(clicked()),
this, SLOT(ReturnBookSlot()));
}
void ReturnBook::ShowAllUserBooks()
{
if (this->book == NULL || this->userBook == NULL)
{
return;
}
UserBookType* node = (UserBookType*)this->userBook->GetNode(-1);
node = (UserBookType*)node->GetNext();
while (node != NULL)
{
// 根据书的id获得书的具体信息
BookType* node2 = (BookType*)this->book->GetNode(node->GetBookId());
QTreeWidgetItem* item = new QTreeWidgetItem();
item->setText(0, QString::number(node2->GetId()));
item->setText(1, node2->GetName());
item->setText(2, node2->GetAuthor());
item->setText(3, node2->GetCategory());
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable
| Qt::ItemIsUserCheckable);
item->setCheckState(0, Qt::Unchecked);
ui->treeWidget->insertTopLevelItem(0, item);
node = (UserBookType*)node->GetNext();
}
}
void ReturnBook::ReturnBookSlot()
{
for (int i = 0; i < ui->treeWidget->topLevelItemCount(); i++)
{
QTreeWidgetItem* item = ui->treeWidget->topLevelItem(i);
if (item->checkState(0) == Qt::Checked)
{
UserBookType* node = new UserBookType();
// 删除一本书只要人的id和书的id,其他信息并不需要
node->SetId(this->userBook->userId);
node->SetBookId(item->text(0).toInt());
this->returnBook->AddNode(node);
item->setCheckState(0, Qt::Unchecked);
}
}
this->close();
}
void ReturnBook::closeEvent(QCloseEvent *e)
{
// 弹出对话框,询问是否保存
QMessageBox::StandardButton msg = QMessageBox::information(this,
tr("Prompt"),
tr("Save You Book?"),
QMessageBox::Save,
QMessageBox::Cancel);
switch (msg)
{
case QMessageBox::Save:
{
UserBookType* node = (UserBookType*)this->returnBook->GetNode(-1);
node = (UserBookType*)node->GetNext();
while (node != NULL)
{
// 根据书的id从userBook链表中删除还的书
this->userBook->DeleteNode(node->GetBookId());
node = (UserBookType*)node->GetNext();
}
// 把returnBook链表中的信息从数据库中删掉
this->returnBook->Save();
} break;
case QMessageBox::Cancel:
{
// 清空returBook链表
this->returnBook->Clear();
} break;
default:
break;
}
}