门禁人员管理界面构建 c++实现
时间: 2023-07-31 21:04:41 浏览: 163
人员管理系统的简易实现(C++)
下面是一个简单的门禁人员管理界面的C++实现示例,使用的是Qt图形界面库和SQLite数据库。在这个示例中,我们实现了一个简单的门禁人员管理界面,可以实现添加、删除、修改和查询门禁人员信息的功能。
```
#include <QtWidgets>
#include <QtSql>
class MainWindow : public QMainWindow
{
Q_OBJECT
public:
explicit MainWindow(QWidget *parent = nullptr);
~MainWindow();
private slots:
void onAddButtonClicked();
void onEditButtonClicked();
void onDeleteButtonClicked();
void onSearchButtonClicked();
void onTableSelectionChanged();
private:
QSqlDatabase db;
QTableView *tableView;
QLineEdit *nameEdit;
QLineEdit *idEdit;
QLineEdit *phoneEdit;
QPushButton *addButton;
QPushButton *editButton;
QPushButton *deleteButton;
QPushButton *searchButton;
};
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
// 初始化数据库
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("door.db");
if (!db.open()) {
QMessageBox::critical(this, "Error", "Failed to open database");
return;
}
// 创建表格
QSqlQuery query;
if (!query.exec("CREATE TABLE IF NOT EXISTS door_person (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, id_card TEXT, phone TEXT)")) {
QMessageBox::critical(this, "Error", "Failed to create table");
return;
}
// 创建界面控件
tableView = new QTableView(this);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
connect(tableView, SIGNAL(clicked(QModelIndex)), this, SLOT(onTableSelectionChanged()));
nameEdit = new QLineEdit(this);
idEdit = new QLineEdit(this);
phoneEdit = new QLineEdit(this);
addButton = new QPushButton("Add", this);
connect(addButton, SIGNAL(clicked()), this, SLOT(onAddButtonClicked()));
editButton = new QPushButton("Edit", this);
editButton->setEnabled(false);
connect(editButton, SIGNAL(clicked()), this, SLOT(onEditButtonClicked()));
deleteButton = new QPushButton("Delete", this);
deleteButton->setEnabled(false);
connect(deleteButton, SIGNAL(clicked()), this, SLOT(onDeleteButtonClicked()));
searchButton = new QPushButton("Search", this);
connect(searchButton, SIGNAL(clicked()), this, SLOT(onSearchButtonClicked()));
// 创建布局
QGridLayout *mainLayout = new QGridLayout;
mainLayout->addWidget(new QLabel("Name:"), 0, 0);
mainLayout->addWidget(nameEdit, 0, 1);
mainLayout->addWidget(new QLabel("ID Card:"), 1, 0);
mainLayout->addWidget(idEdit, 1, 1);
mainLayout->addWidget(new QLabel("Phone:"), 2, 0);
mainLayout->addWidget(phoneEdit, 2, 1);
mainLayout->addWidget(addButton, 0, 2);
mainLayout->addWidget(editButton, 1, 2);
mainLayout->addWidget(deleteButton, 2, 2);
mainLayout->addWidget(searchButton, 3, 0);
mainLayout->addWidget(tableView, 4, 0, 1, 3);
// 创建主窗口
QWidget *centralWidget = new QWidget(this);
centralWidget->setLayout(mainLayout);
setCentralWidget(centralWidget);
setWindowTitle("Door Person Management");
}
MainWindow::~MainWindow()
{
db.close();
}
void MainWindow::onAddButtonClicked()
{
QString name = nameEdit->text().trimmed();
QString idCard = idEdit->text().trimmed();
QString phone = phoneEdit->text().trimmed();
if (name.isEmpty() || idCard.isEmpty() || phone.isEmpty()) {
QMessageBox::warning(this, "Warning", "Please fill in all fields");
return;
}
QSqlQuery query;
query.prepare("INSERT INTO door_person (name, id_card, phone) VALUES (?, ?, ?)");
query.bindValue(0, name);
query.bindValue(1, idCard);
query.bindValue(2, phone);
if (!query.exec()) {
QMessageBox::critical(this, "Error", "Failed to add door person");
return;
}
QSqlTableModel *model = qobject_cast<QSqlTableModel *>(tableView->model());
if (model) {
model->select();
}
}
void MainWindow::onEditButtonClicked()
{
QModelIndexList selection = tableView->selectionModel()->selectedRows();
if (selection.isEmpty()) {
return;
}
QString name = nameEdit->text().trimmed();
QString idCard = idEdit->text().trimmed();
QString phone = phoneEdit->text().trimmed();
if (name.isEmpty() || idCard.isEmpty() || phone.isEmpty()) {
QMessageBox::warning(this, "Warning", "Please fill in all fields");
return;
}
QSqlQuery query;
query.prepare("UPDATE door_person SET name = ?, id_card = ?, phone = ? WHERE id = ?");
query.bindValue(0, name);
query.bindValue(1, idCard);
query.bindValue(2, phone);
query.bindValue(3, selection.first().data(Qt::DisplayRole).toInt());
if (!query.exec()) {
QMessageBox::critical(this, "Error", "Failed to edit door person");
return;
}
QSqlTableModel *model = qobject_cast<QSqlTableModel *>(tableView->model());
if (model) {
model->select();
}
}
void MainWindow::onDeleteButtonClicked()
{
QModelIndexList selection = tableView->selectionModel()->selectedRows();
if (selection.isEmpty()) {
return;
}
int ret = QMessageBox::warning(this, "Warning", "Are you sure to delete the selected door person?", QMessageBox::Yes | QMessageBox::No);
if (ret != QMessageBox::Yes) {
return;
}
QSqlQuery query;
query.prepare("DELETE FROM door_person WHERE id = ?");
query.bindValue(0, selection.first().data(Qt::DisplayRole).toInt());
if (!query.exec()) {
QMessageBox::critical(this, "Error", "Failed to delete door person");
return;
}
QSqlTableModel *model = qobject_cast<QSqlTableModel *>(tableView->model());
if (model) {
model->select();
}
}
void MainWindow::onSearchButtonClicked()
{
QString name = nameEdit->text().trimmed();
QString idCard = idEdit->text().trimmed();
QString phone = phoneEdit->text().trimmed();
QString queryStr = "SELECT * FROM door_person WHERE 1 = 1";
if (!name.isEmpty()) {
queryStr += " AND name LIKE '%" + name + "%'";
}
if (!idCard.isEmpty()) {
queryStr += " AND id_card LIKE '%" + idCard + "%'";
}
if (!phone.isEmpty()) {
queryStr += " AND phone LIKE '%" + phone + "%'";
}
QSqlQueryModel *model = new QSqlQueryModel(this);
model->setQuery(queryStr);
tableView->setModel(model);
}
void MainWindow::onTableSelectionChanged()
{
QModelIndexList selection = tableView->selectionModel()->selectedRows();
if (selection.isEmpty()) {
nameEdit->clear();
idEdit->clear();
phoneEdit->clear();
editButton->setEnabled(false);
deleteButton->setEnabled(false);
return;
}
QSqlQuery query;
query.prepare("SELECT * FROM door_person WHERE id = ?");
query.bindValue(0, selection.first().data(Qt::DisplayRole).toInt());
if (!query.exec() || !query.first()) {
return;
}
nameEdit->setText(query.value("name").toString());
idEdit->setText(query.value("id_card").toString());
phoneEdit->setText(query.value("phone").toString());
editButton->setEnabled(true);
deleteButton->setEnabled(true);
}
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow window;
window.show();
return app.exec();
}
#include "main.moc"
```
在这个示例中,我们首先创建了一个Qt窗口,并在窗口中添加了列表框、文本框、按钮等控件。然后,我们使用SQLite数据库创建了一个door_person表格,并实现了添加、删除、编辑和查询门禁人员信息的功能。最后,我们将门禁人员管理界面的逻辑代码放在了MainWindow类中,并通过信号和槽机制实现了各个控件之间的交互。
阅读全文