第四章 用户界面
https://gitee.com/mayonaka/LibraryManageSystem
百度云:https://pan.baidu.com/s/1G95yPyGG080b6yXcjc8B0g
提取码:4q8b
我们在上一章已经建立了一个用户界面,不过并没有编写实现,因为登陆的是一个用户,所以在登陆成功,打开用户界面时,要传一个参数过来以确定是哪个用户。因此要修改一下UserInterface类的构造函数,当然在登陆界面中,建立用户界面的函数也要修改。
用户界面的构造函数添加了一个int类型的id,用来确定用户的身份。在new一个UserInterface变量时,要把登陆的用户的id穿进去,变量不仅要调用show函数让界面显示,还要调用ShowUserBooks让用户已借阅的书显示。
// Login.cpp
// void Login::LoginSystemSlot()
UserInterface* w = new UserInterface(node->GetId());
// 显示界面
w->show();
// 显示用户已借阅的书
w->ShowAllUserBooks();
#include <QWidget>
#include <QTreeWidget>
#include "book.h"
#include "userbook.h"
namespace Ui {
class UserInterface;
}
class UserInterface : public QWidget
{
Q_OBJECT
public:
explicit UserInterface(int id, QWidget *parent = 0);
~UserInterface();
public slots:
// 打开借书界面的槽
void BorrowBookSlot();
// 打开还书界面的槽
void ReturnBookSlot();
// 显示书籍详细信息的槽
void ShowBookInfoSlot(QTreeWidgetItem* item, int column);
// 显示用户已借阅的书籍的槽
void ShowAllUserBooks();
private:
Ui::UserInterface *ui;
UserBook* userBook = NULL;
Book* book = NULL;
UserBook* borrowBook = NULL;
UserBook* returnBook = NULL;
};
1. 用户界面共添加了四个属性
1. userBook属性:储存当前用户已借阅的书
2. book属性:储存所有书的信息
3. borrowBook属性:储存当前操作借阅的书
4. returnBook属性:储存当前操作还掉的书
2. 用户界面的功能实现如下:
1. 在Tree Widget显示用用户已借阅的书
2. 点击借书按钮,打开借书界面
3. 点击还书按钮,打开还书界面
4. 双击每一行书,会显示书的详细信息
3. 用户界面需要四个槽:
1. 显示用户已借阅书籍的槽
2. 当用户点击借书按钮,打开借书界面的槽。
3. 当用户点击还书按钮,打开还书界面的槽。
4. 当用户双击每一行书,显示书本详细信息的槽。
4. 设计用户界面,双击userinterface.ui,用户界面用了两个Push Button,一个Tree Widget。
1. Push Button: 显示文字:“借书”,变量名:borrowPushButton。
2. Push Button:显示文字:“还书“,变量名:returnPushButton。
3. Tree Widget:显示文字:无,变量名:因为只有一个,可不该。
右击Tree Widget,选择编辑项目,在列那一栏,点+号,可以添加Tree Widget的列。
5. 实现具体的函数:
1. 在构造函数中,先初始化各个熟悉的值,userBook要根据登陆界面传来的ld进行初始化。用户界面有三个槽需要连接,borrowPushButton的clicked信号,与用户界面BorrowBookSlot槽连接;returnPushButton的clicked信号,与用户界面的ReturnBookSlot槽连接;treeWidget的双击信号itemDoubleClicked信号与ShowBookInfoSlot槽连接。
在这里简单介绍一下connect函数,connect函数共有四个参数,第一个参数是发送信号的对象的指针,第二个参数对象是发送的信号,第三个参数是接受信号的对象的指针,第四个参数是槽函数。信号的参数与槽的参数一一对应。
2. 在ShowAllUserBooks槽函数中,先清空treeWidget,然后遍历userBook链表,把链表的每一个节点的信息转换成QTreeWidgetItem对象,插入到treeWidget中。
#include <QDebug>
#include <QTreeWidget>
#include <QTreeWidgetItem>
#include <QDate>
#include "userinterface.h"
#include "ui_userinterface.h"
#include "bookinfo.h"
UserInterface::UserInterface(int id, QWidget *parent) :
QWidget(parent),
ui(new Ui::UserInterface)
{
ui->setupUi(this);
this->setWindowTitle(QString::fromLocal8Bit("用户"));
// 登录后,把登陆的用户的id传过来,
// 根据id创建一个用户
this->userBook = new UserBook(id);
this->userBook->Init();
// 储存的是数据库中所有的书籍信息
this->book = new Book();
this->book->Init();
// 用户在借书界面借的书会储存在borrowBook里,
// 头节点设为1表示链表中的节点要添加到数据库中
this->borrowBook = new UserBook(this->userBook->userId);
this->borrowBook->GetNode(-1)->SetId(1);
// 用户在还书界面还的书会储存在returnBook里
// 头节点设为-1表示链表中的节点要从数据库中删去
this->returnBook = new UserBook(this->userBook->userId);
this->returnBook->GetNode(-1)->SetId(-1);
// 当用户双击书籍时,会显示书籍的详细信息
QObject::connect(ui->treeWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*,int)), this,
SLOT(ShowBookInfoSlot(QTreeWidgetItem*, int)));
// 当用户点击借书按钮,会打开借书界面
QObject::connect(ui->borrowPushButton, SIGNAL(clicked(bool)),
this, SLOT(BorrowBookSlot()));
// 当用户点击还书按钮,会打开还书界面
QObject::connect(ui->returnPushButton, SIGNAL(clicked(bool)),
this, SLOT(ReturnBookSlot()));
}
UserInterface::~UserInterface()
{
delete ui;
if (this->userBook != NULL)
{
delete this->userBook;
this->userBook = NULL;
}
if (this->book != NULL)
{
delete this->book;
this->book = NULL;
}
if (this->borrowBook != NULL)
{
delete this->borrowBook;
this->borrowBook = NULL;
}
if (this->returnBook != NULL)
{
delete this->returnBook;
this->returnBook = NULL;
}
}
void UserInterface::ShowAllUserBooks()
{
// 清空treeWidget
ui->treeWidget->clear();
// 获得头节点
UserBookType* node = (UserBookType*)this->userBook->GetNode(-1);
// 获得第一个节点
node = (UserBookType*)node->GetNext();
// 循环整个链表
while (node != NULL)
{
// 根据书籍id获得该书的节点
BookType* bookNode = (BookType*)this->book->GetNode(node->GetBookId());
// QTreeWidgetItem是Tree Widget的一行
QTreeWidgetItem* item = new QTreeWidgetItem();
// 设置每一行各个项的值
item->setText(0, QString::number(node->GetBookId()));
item->setText(1, bookNode->GetName());
item->setText(2, bookNode->GetAuthor());
item->setText(3, bookNode->GetAuthor());
item->setText(4, QString("%1-%2-%3")
.arg(node->GetYear())
.arg(node->GetMonth())
.arg(node->GetDay()));
// 书的借阅时间是六个月,当前日期加6个月就是还书日期
QDate date(node->GetYear(), node->GetMonth(), node->GetDay());
date.addMonths(6);
item->setText(5, QString("%1-%2-%3")
.arg(date.year())
.arg(date.month())
.arg(date.day()));
// 把QTreeWidgetItem插入Tree Widget
ui->treeWidget->insertTopLevelItem(0, item);
node = (UserBookType*)node->GetNext();
}
}
// 打开显示书本详细信息的界面
void UserInterface::ShowBookInfoSlot(QTreeWidgetItem *item, int column)
{
}
// 打开借书界面
void UserInterface::BorrowBookSlot()
{
}
// 打开还书界面
void UserInterface::ReturnBookSlot()
{
}