64,654
社区成员
发帖
与我相关
我的任务
分享
#ifndef OWNEDDIALOG_H
#define OWNEDDIALOG_H
#include <QtGui/QDialog>
#include <QList>
#include "userbooks.h"
#include "book.h"
class QSqlTableModel;
namespace Ui {
class OwnedDialog;
}
class OwnedDialog : public QDialog {
Q_OBJECT
Q_DISABLE_COPY(OwnedDialog)
public:
explicit OwnedDialog( UserBooks *books, UserBooks *adminBooks, QWidget *parent = 0);
virtual ~OwnedDialog();
protected:
virtual void changeEvent(QEvent *e);
virtual void showEvent( QShowEvent* );
private:
Ui::OwnedDialog *m_ui;
QSqlTableModel *model; //
UserBooks *books; //当前用户所借的书
UserBooks *adminBooks; //图书馆中所有的书,由管理员掌控
QList<Book> *bookList;
private slots:
void on_returnButton_clicked(); //点击还书按钮
void setBookDetail(QString bookName); //当选择另一个书名时,改变编辑框的值
void on_closeButton_clicked(); //按下关闭按钮
private:
bool updateData(); //当还了书后更新显示
#include <QSqlTableModel>
#include <QSqlQuery>
#include <QMessageBox>
#include <QList>
#include "owneddialog.h"
#include "book.h"
#include "ui_owneddialog.h"
OwnedDialog::OwnedDialog( UserBooks *books, UserBooks *adminBooks, QWidget *parent) :
QDialog(parent),
m_ui(new Ui::OwnedDialog)
{
m_ui->setupUi(this);
this->setModal( true );
this->books = books;
this->adminBooks = adminBooks;
model = new QSqlTableModel;
bookList = books->getBookList();
connect(m_ui->booknameComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(setBookDetail(QString)));
connect( qApp, SIGNAL(lastWindowClosed()), books, SLOT(write())); //退出程序保存数据
//如果当前用户不是管理员,程序退出时保存管理员的书籍信息
if( books->getUser()->getName() != "admin")
connect(qApp, SIGNAL(lastWindowClosed()), adminBooks, SLOT(write()));
}
OwnedDialog::~OwnedDialog()
{
delete m_ui;
}
void OwnedDialog::changeEvent(QEvent *e)
{
switch (e->type()) {
case QEvent::LanguageChange:
m_ui->retranslateUi(this);
break;
default:
break;
}
}
void OwnedDialog::showEvent( QShowEvent *event )
{
updateData();
}
//点击还书按钮,还掉选中书籍
void OwnedDialog::on_returnButton_clicked()
{
QString bookName = m_ui->booknameComboBox->currentText();
Book toReturn = books->getBook( bookName );
//在数据库中删除选中书籍
QSqlQuery delBook;
QString sql = QString("delete from %1 where name = '%2'").arg( books->getUser()->getName()).arg( bookName);
if( delBook.exec( sql ) && books->remove( toReturn ) )
{
QMessageBox::information( this, tr("Book Returned"), tr("Return the book seccussfully!!") );
}
else
{
QMessageBox::information( this, tr("Book Return Failed"), tr("Sorry but Fail to Return the book !!") );
}
}
void OwnedDialog::setBookDetail( QString bookName)
{
Book temp = books->getBook( bookName );
m_ui->authorLineEdit->setText( temp.getAuthor() );
m_ui->copyLineEdit->setText( tr("%1").arg( temp.getCopy()) );
m_ui->pressLineEdit->setText( temp.getPress() );
}
void OwnedDialog::on_closeButton_clicked()
{
close();
}
bool OwnedDialog::updateData()
{
model->setTable( tr("%1").arg(books->getUser()->getName()) );
model->select();
model->removeColumn(0); // don't show the ID
model->setHeaderData(0, Qt::Horizontal, tr("Name"));
model->setHeaderData(1, Qt::Horizontal, tr("Author"));
model->setHeaderData(2, Qt::Horizontal, tr("Copy"));
model->setHeaderData(3, Qt::Horizontal, tr("Press"));
//在表格中显示该用户所借阅的书籍
m_ui->tableView->clearSpans();
m_ui->tableView->setModel( model );
m_ui->tableView->setEditTriggers( QAbstractItemView::NoEditTriggers );
//在退还书籍中显示信息
bookList = books->getBookList();
m_ui->booknameComboBox->clear();
for(int i = 0; i < bookList->size(); i++ )
{
Book temp = bookList->at( i );
m_ui->booknameComboBox->addItem( temp.getName() );
}
return true;
}
OwnedDialog::~OwnedDialog()
{
delete m_ui;
}