门禁人员管理界面构建 c++实现

时间: 2023-07-31 21:04:41 浏览: 47
下面是一个简单的门禁人员管理界面的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类中,并通过信号和槽机制实现了各个控件之间的交互。

相关推荐

最新推荐

recommend-type

C语言简单实现门禁系统

主要为大家详细介绍了C语言简单实现门禁系统,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
recommend-type

C语言程序设计实现门禁控制系统

系统将ID卡技术、计算机控制技术与电子门锁有机结合,用ID卡替代钥匙,配合计算机实现智能化门禁控制和管理,有效的解决了传统门锁的使用繁琐和无法信息记录等不足,利用数据控制器采集的数据实现数字化管理可为内部...
recommend-type

实现NFC手机做门禁卡的方法

基础条件:门禁刷卡器能识别NFC手机(卡模拟状态下的CPU卡)。 验证办法:打开手机的NFC,在门禁刷卡器上刷一下,门禁刷卡器有反应就行(响一声),那么就满足了基础条件。如果门禁刷卡器没反应,那就不需要考虑了,...
recommend-type

基于嵌入式Linux平台的指纹门禁系统

本文基于嵌入式Linux平台的指纹门禁系统,该系统基于ARM9芯片Samsung S3C2440AL,以Veridicom公司指纹采集芯片FPS200作为硬件平台,以嵌入式Linux为软件平台。在该研究领域中,基于PC平台的识别系统一直是研究的重点...
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

用matlab绘制高斯色噪声情况下的频率估计CRLB,其中w(n)是零均值高斯色噪声,w(n)=0.8*w(n-1)+e(n),e(n)服从零均值方差为se的高斯分布

以下是用matlab绘制高斯色噪声情况下频率估计CRLB的代码: ```matlab % 参数设置 N = 100; % 信号长度 se = 0.5; % 噪声方差 w = zeros(N,1); % 高斯色噪声 w(1) = randn(1)*sqrt(se); for n = 2:N w(n) = 0.8*w(n-1) + randn(1)*sqrt(se); end % 计算频率估计CRLB fs = 1; % 采样频率 df = 0.01; % 频率分辨率 f = 0:df:fs/2; % 频率范围 M = length(f); CRLB = zeros(M,1); for
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依