用C++写可视化界面的图书馆管理系统
时间: 2024-02-18 14:04:44 浏览: 28
下面是一个使用Qt库实现的简单图书馆管理系统的示例代码,其中包括添加、删除、更新和搜索书籍的功能:
```c++
#include <QtWidgets/QApplication>
#include <QtWidgets/QMainWindow>
#include <QtWidgets/QWidget>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QMessageBox>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
MainWindow(QWidget *parent = nullptr)
: QMainWindow(parent)
{
// 创建窗口控件
QWidget *centralWidget = new QWidget(this);
QLabel *titleLabel = new QLabel(tr("图书馆管理系统"), centralWidget);
QLabel *nameLabel = new QLabel(tr("书名:"), centralWidget);
QLabel *authorLabel = new QLabel(tr("作者:"), centralWidget);
QLineEdit *nameEdit = new QLineEdit(centralWidget);
QLineEdit *authorEdit = new QLineEdit(centralWidget);
QPushButton *addButton = new QPushButton(tr("添加"), centralWidget);
QPushButton *deleteButton = new QPushButton(tr("删除"), centralWidget);
QPushButton *updateButton = new QPushButton(tr("更新"), centralWidget);
QPushButton *searchButton = new QPushButton(tr("搜索"), centralWidget);
QComboBox *bookList = new QComboBox(centralWidget);
// 设置窗口控件位置和大小
titleLabel->setGeometry(QRect(150, 20, 100, 30));
nameLabel->setGeometry(QRect(20, 70, 50, 30));
nameEdit->setGeometry(QRect(80, 70, 150, 30));
authorLabel->setGeometry(QRect(20, 120, 50, 30));
authorEdit->setGeometry(QRect(80, 120, 150, 30));
addButton->setGeometry(QRect(20, 170, 70, 30));
deleteButton->setGeometry(QRect(100, 170, 70, 30));
updateButton->setGeometry(QRect(180, 170, 70, 30));
searchButton->setGeometry(QRect(260, 170, 70, 30));
bookList->setGeometry(QRect(20, 220, 310, 30));
// 设置窗口标题和大小
setWindowTitle(tr("图书馆管理系统"));
setCentralWidget(centralWidget);
setFixedSize(350, 280);
// 连接数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("library.db");
if (!db.open()) {
QMessageBox::critical(this, tr("错误"), tr("无法连接数据库!"));
return;
}
// 创建书籍表
QSqlQuery query;
if (!query.exec("CREATE TABLE IF NOT EXISTS books (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, author TEXT)")) {
QMessageBox::critical(this, tr("错误"), tr("无法创建书籍表!"));
return;
}
// 添加书籍
connect(addButton, &QPushButton::clicked, [=]() {
QString name = nameEdit->text().trimmed();
QString author = authorEdit->text().trimmed();
if (name.isEmpty() || author.isEmpty()) {
QMessageBox::warning(this, tr("警告"), tr("书名和作者不能为空!"));
return;
}
query.prepare("INSERT INTO books (name, author) VALUES (:name, :author)");
query.bindValue(":name", name);
query.bindValue(":author", author);
if (!query.exec()) {
QMessageBox::critical(this, tr("错误"), tr("无法添加书籍!"));
return;
}
nameEdit->clear();
authorEdit->clear();
bookList->addItem(name);
});
// 删除书籍
connect(deleteButton, &QPushButton::clicked, [=]() {
QString name = bookList->currentText();
if (name.isEmpty()) {
QMessageBox::warning(this, tr("警告"), tr("请选择要删除的书籍!"));
return;
}
int index = bookList->currentIndex();
bookList->removeItem(index);
query.prepare("DELETE FROM books WHERE name = :name");
query.bindValue(":name", name);
if (!query.exec()) {
QMessageBox::critical(this, tr("错误"), tr("无法删除书籍!"));
return;
}
nameEdit->clear();
authorEdit->clear();
});
// 更新书籍
connect(updateButton, &QPushButton::clicked, [=]() {
QString name = bookList->currentText();
QString author = authorEdit->text().trimmed();
if (name.isEmpty()) {
QMessageBox::warning(this, tr("警告"), tr("请选择要更新的书籍!"));
return;
}
if (author.isEmpty()) {
QMessageBox::warning(this, tr("警告"), tr("作者不能为空!"));
return;
}
query.prepare("UPDATE books SET author = :author WHERE name = :name");
query.bindValue(":name", name);
query.bindValue(":author", author);
if (!query.exec()) {
QMessageBox::critical(this, tr("错误"), tr("无法更新书籍!"));
return;
}
authorEdit->clear();
});
// 搜索书籍
connect(searchButton, &QPushButton::clicked, [=]() {
QString keyword = nameEdit->text().trimmed();
if (keyword.isEmpty()) {
QMessageBox::warning(this, tr("警告"), tr("关键字不能为空!"));
return;
}
bookList->clear();
query.prepare("SELECT name FROM books WHERE name LIKE :keyword");
query.bindValue(":keyword", "%" + keyword + "%");
if (!query.exec()) {
QMessageBox::critical(this, tr("错误"), tr("无法搜索书籍!"));
return;
}
while (query.next()) {
bookList->addItem(query.value(0).toString());
}
});
}
};
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
```
此示例代码使用Qt库创建了一个包含添加、删除、更新和搜索书籍的简单图书馆管理系统。在此示例代码中,我们使用了Qt的GUI组件和QtSql库来连接SQLite数据库并执行SQL查询。