下面是一个使用Qt实现简单图书管理系统的示例代码,主要包括以下功能:
1. 添加图书信息
2. 删除图书信息
3. 修改图书信息
4. 查询图书信息
5. 显示图书列表
```
// book.h 头文件
ifndef BOOK_H
define BOOK_H
include <QString>
class Book
{
public:
Book() {}
Book(QString title, QString author, QString publisher, int publishYear,
QString isbn, QString coverPath);
QString getTitle();
QString getAuthor();
QString getPublisher();
int getPublishYear();
QString getIsbn();
QString getCoverPath();
private:
QString title;
QString author;
QString publisher;
int publishYear;
QString isbn;
QString coverPath;
};
endif // BOOK_H
// book.cpp 实现文件
include &34;book.h&34;
Book::Book(QString title, QString author, QString publisher, int publishYear,
QString isbn, QString coverPath)
: title(title), author(author), publisher(publisher),
publishYear(publishYear), isbn(isbn), coverPath(coverPath)
{
}
QString Book::getTitle()
{
return title;
}
QString Book::getAuthor()
{
return author;
}
QString Book::getPublisher()
{
return publisher;
}
int Book::getPublishYear()
{
return publishYear;
}
QString Book::getIsbn()
{
return isbn;
}
QString Book::getCoverPath()
{
return coverPath;
}
// database.h 头文件
ifndef DATAbase_H
define DATAbase_H
include <QList>
include &34;book.h&34;
class Database
{
public:
Database();
void addBook(Book book);
void removeBook(QString isbn);
void updateBook(QString isbn, Book book);
QList<Book> queryBooks(QString keyword);
QList<Book> getBookList();
private:
QList<Book> bookList;
};
endif // DATAbase_H
// database.cpp 实现文件
include &34;database.h&34;
Database::Database()
{
}
void Database::addBook(Book book)
{
bookList.append(book);
}
void Database::removeBook(QString isbn)
{
for (int i=0; i < bookList.size(); ++i) {
if (bookList[i].getIsbn()==isbn) {
bookList.removeAt(i);
break;
}
}
}
void Database::updateBook(QString isbn, Book book)
{
for (int i=0; i < bookList.size(); ++i) {
if (bookList[i].getIsbn()==isbn) {
bookList[i]=book;
break;
}
}
}
QList<Book> Database::queryBooks(QString keyword)
{
QList<Book> result;
for (int i=0; i < bookList.size(); ++i) {
if (bookList[i].getTitle().contains(keyword, Qt::CaseInsensitive) ||
bookList[i].getAuthor().contains(keyword, Qt::CaseInsensitive) ||
bookList[i].getPublisher().contains(keyword, Qt::CaseInsensitive)) {
result.append(bookList[i]);
}
}
return result;
}
QList<Book> Database::getBookList()
{
return bookList;
}
// mainwindow.h 头文件
ifndef MAINWINDOW_H
define MAINWINDOW_H
include <QMainWindow>
include <QStandardItemModel>
include &34;database.h&34;
namespace Ui {
class MainWindow;
}
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent=nullptr);
?MainWindow();
private slots:
void on_actionAdd_triggered();
void on_actionRemove_triggered();
void on_actionUpdate_triggered();
void on_actionQuery_triggered();
void on_actionShow_All_triggered();
private:
Ui::MainWindow *ui;
Database db;
QStandardItemModel model;
};
endif // MAINWINDOW_H
// mainwindow.cpp 实现文件
include &34;mainwindow.h&34;
include &34;ui_mainwindow.h&34;
include <QInputDialog>
include <QFileDialog>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->tableView->setModel(&model);
}
MainWindow::?MainWindow()
{
delete ui;
}
void MainWindow::on_actionAdd_triggered()
{
QString title=QInputDialog::getText(this, &34;Add Book&34;, &34;Title:&34;);
if (title.isEmpty()) return;
QString author=QInputDialog::getText(this, &34;Add Book&34;, &34;Author:&34;);
if (author.isEmpty()) return;
QString publisher=QInputDialog::getText(this, &34;Add Book&34;, &34;Publisher:&34;);
if (publisher.isEmpty()) return;
bool ok;
int publishYear=QInputDialog::getInt(this, &34;Add Book&34;, &34;Publish Year:&34;, 2000, 0, 9999, 1, &ok);
if (!ok) return;
QString isbn=QInputDialog::getText(this, &34;Add Book&34;, &34;ISBN:&34;);
if (isbn.isEmpty()) return;
QString coverPath=QFileDialog::getOpenFileName(this, &34;Select Cover Image&34;, &34;&34;, &34;Image Files (*.bmp *.jpg *.png)&34;);
Book book(title, author, publisher, publishYear, isbn, coverPath);
db.addBook(book);
model.clear();
QList<Book> books=db.getBookList();
for (int i=0; i < books.size(); ++i) {
QList<QStandardItem*> row;
row.append(new QStandardItem(books[i].getTitle()));
row.append(new QStandardItem(books[i].getAuthor()));
row.append(new QStandardItem(books[i].getPublisher()));
row.append(new QStandardItem(QString::number(books[i].getPublishYear())));
row.append(new QStandardItem(books[i].getIsbn()));
model.appendRow(row);
}
}
void MainWindow::on_actionRemove_triggered()
{
QModelIndexList selectedRows=ui->tableView->selectionModel()->selectedRows();
if (selectedRows.isEmpty()) return;
QString isbn=model.item(selectedRows[0].row(), 4)->text();
db.removeBook(isbn);
model.removeRow(selectedRows[0].row());
}
void MainWindow::on_actionUpdate_triggered()
{
QModelIndexList selectedRows=ui->tableView->selectionModel()->selectedRows();
if (selectedRows.isEmpty()) return;
QString title=QInputDialog::getText(this, &34;Update Book&34;, &34;Title:&34;);
if (title.isEmpty()) return;
QString author=QInputDialog::getText(this, &34;Update Book&34;, &34;Author:&34;);
if (author.isEmpty()) return;
QString publisher=QInputDialog::getText(this, &34;Update Book&34;, &34;Publisher:&34;);
if (publisher.isEmpty()) return;
bool ok;
int publishYear=QInputDialog::getInt(this, &34;Update Book&34;, &34;Publish Year:&34;, 2000, 0, 9999, 1, &ok);
if (!ok) return;
QString isbn=QInputDialog::getText(this, &34;Update Book&34;, &34;ISBN:&34;);
if (isbn.isEmpty()) return;
QString coverPath=QFileDialog::getOpenFileName(this, &34;Select Cover Image&34;, &34;&34;, &34;Image Files (*.bmp *.jpg *.png)&34;);
Book book(title, author, publisher, publishYear, isbn, coverPath);
db.updateBook(model.item(selectedRows[0].row(), 4)->text(), book);
model.clear();
QList<Book> books=db.getBookList();
for (int i=0; i < books.size(); ++i) {
QList<QStandardItem*> row;
row.append(new QStandardItem(books[i].getTitle()));
row.append(new QStandardItem(books[i].getAuthor()));
row.append(new QStandardItem(books[i].getPublisher()));
row.append(new QStandardItem(QString::number(books[i].getPublishYear())));
row.append(new QStandardItem(books[i].getIsbn()));
model.appendRow(row);
}
}
void MainWindow::on_actionQuery_triggered()
{
QString keyword=QInputDialog::getText(this, &34;Query Book&34;, &34;Keyword:&34;);
if (keyword.isEmpty()) return;
model.clear();
QList<Book> books=db.queryBooks(keyword);
for (int i=0; i < books.size(); ++i) {
QList<QStandardItem*> row;
row.append(new QStandardItem(books[i].getTitle()));
row.append(new QStandardItem(books[i].getAuthor()));
row.append(new QStandardItem(books[i].getPublisher()));
row.append(new QStandardItem(QString::number(books[i].getPublishYear())));
row.append(new QStandardItem(books[i].getIsbn()));
model.appendRow(row);
}
}
void MainWindow::on_actionShow_All_triggered()
{
model.clear();
QList<Book> books=db.getBookList();
for (int i=0; i < books.size(); ++i) {
QList<QStandardItem*> row;
row.append(new QStandardItem(books[i].getTitle()));
row.append(new QStandardItem(books[i].getAuthor()));
row.append(new QStandardItem(books[i].getPublisher()));
row.append(new QStandardItem(QString::number(books[i].getPublishYear())));
row.append(new QStandardItem(books[i].getIsbn()));
model.appendRow(row);
}
}
// main.cpp 入口文件
include &34;mainwindow.h&34;
include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
```
希望这个示例能够帮助到你,如有任何疑问或需要进一步完善,请随时提问。
标签: # 图书,管理系统,学校,下面,是,一个,使用,实现,