如何在Qt上实现一个考勤表,并提供示例代码

时间: 2023-05-29 18:05:14 浏览: 72
我不知道您需要的是什么样的考勤表,但是以下是一个简单的示例,它创建一个基本的考勤表,并允许用户添加、删除和编辑条目。 mainwindow.h: ```c++ #ifndef MAINWINDOW_H #define MAINWINDOW_H #include <QMainWindow> #include <QStandardItemModel> namespace Ui { class MainWindow; } class MainWindow : public QMainWindow { Q_OBJECT public: explicit MainWindow(QWidget *parent = nullptr); ~MainWindow(); private slots: void on_addButton_clicked(); void on_deleteButton_clicked(); void on_saveButton_clicked(); void on_loadButton_clicked(); void on_tableView_doubleClicked(const QModelIndex &index); private: Ui::MainWindow *ui; QStandardItemModel *model; }; #endif // MAINWINDOW_H ``` mainwindow.cpp: ```c++ #include "mainwindow.h" #include "ui_mainwindow.h" #include <QStandardItem> #include <QFileDialog> #include <QMessageBox> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); // Create the model and set it on the table view model = new QStandardItemModel(this); model->setColumnCount(3); model->setHorizontalHeaderLabels(QStringList() << "Name" << "Date" << "Status"); ui->tableView->setModel(model); } MainWindow::~MainWindow() { delete ui; } void MainWindow::on_addButton_clicked() { // Create a new row and add it to the model QList<QStandardItem*> row; row.append(new QStandardItem("New Person")); row.append(new QStandardItem(QDate::currentDate().toString(Qt::ISODate))); row.append(new QStandardItem("Absent")); model->appendRow(row); } void MainWindow::on_deleteButton_clicked() { // Get the selected row and remove it from the model QModelIndexList indexes = ui->tableView->selectionModel()->selectedRows(); if (indexes.size() == 1) { int row = indexes.first().row(); model->removeRow(row); } } void MainWindow::on_saveButton_clicked() { // Open a dialog to choose the file to save to QString fileName = QFileDialog::getSaveFileName(this, "Save File", "", "CSV Files (*.csv)"); if (fileName.isEmpty()) { return; } // Open the file for writing QFile file(fileName); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) { QMessageBox::critical(this, "Error", "Could not save file."); return; } // Write the data to the file QTextStream out(&file); out << "Name,Date,Status\n"; for (int row = 0; row < model->rowCount(); ++row) { QString name = model->item(row, 0)->text(); QString date = model->item(row, 1)->text(); QString status = model->item(row, 2)->text(); out << name << "," << date << "," << status << "\n"; } // Close the file file.close(); } void MainWindow::on_loadButton_clicked() { // Open a dialog to choose the file to load from QString fileName = QFileDialog::getOpenFileName(this, "Load File", "", "CSV Files (*.csv)"); if (fileName.isEmpty()) { return; } // Open the file for reading QFile file(fileName); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { QMessageBox::critical(this, "Error", "Could not load file."); return; } // Clear the current model and read the data from the file model->clear(); model->setColumnCount(3); model->setHorizontalHeaderLabels(QStringList() << "Name" << "Date" << "Status"); QTextStream in(&file); in.readLine(); // Skip the header line while (!in.atEnd()) { QString line = in.readLine(); QStringList fields = line.split(","); QList<QStandardItem*> row; row.append(new QStandardItem(fields.at(0))); row.append(new QStandardItem(fields.at(1))); row.append(new QStandardItem(fields.at(2))); model->appendRow(row); } // Close the file file.close(); } void MainWindow::on_tableView_doubleClicked(const QModelIndex &index) { // Allow the user to edit the status field by double-clicking on it if (index.column() == 2) { ui->tableView->edit(index); } } ``` 这个示例使用一个QStandardItemModel来存储考勤表的数据,并将其设置为QTableView的模型。它包括按钮和双击编辑功能,以便用户可以添加、删除和编辑条目。它还包括保存和加载功能,允许用户将考勤表保存为CSV文件并从CSV文件加载。请注意,这个示例假定CSV文件格式为“Name,Date,Status”。如果您需要不同的格式,请相应地修改代码。

相关推荐

最新推荐

recommend-type

Qt实现保存、浏览、预览、打印功能的示例代码

下面小编就为大家分享一篇Qt实现保存、浏览、预览、打印功能的示例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Qt 使用Poppler实现pdf阅读器的示例代码

下面小编就为大家分享一篇Qt 使用Poppler实现pdf阅读器的示例代码,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
recommend-type

Qt 实现钢笔画线效果示例及详细原理

主要介绍了Qt 实现钢笔画线效果示例及详细原理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
recommend-type

交叉编译qt库并在树莓派上运行qt程序

在window下编写qt程序(helloworld.pro),在linux下编译helloworld.pro,生成可执行文件并上传至树莓派上运行
recommend-type

Qt实现FTP的上传和下载的实例代码

本篇文章主要介绍了Qt实现FTP的上传和下载的实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
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

深入了解MATLAB开根号的最新研究和应用:获取开根号领域的最新动态

![matlab开根号](https://www.mathworks.com/discovery/image-segmentation/_jcr_content/mainParsys3/discoverysubsection_1185333930/mainParsys3/image_copy.adapt.full.medium.jpg/1712813808277.jpg) # 1. MATLAB开根号的理论基础 开根号运算在数学和科学计算中无处不在。在MATLAB中,开根号可以通过多种函数实现,包括`sqrt()`和`nthroot()`。`sqrt()`函数用于计算正实数的平方根,而`nt
recommend-type

react的函数组件的使用

React 的函数组件是一种简单的组件类型,用于定义无状态或者只读组件。 它们通常接受一个 props 对象作为参数并返回一个 React 元素。 函数组件的优点是代码简洁、易于测试和重用,并且它们使 React 应用程序的性能更加出色。 您可以使用函数组件来呈现简单的 UI 组件,例如按钮、菜单、标签或其他部件。 您还可以将它们与 React 中的其他组件类型(如类组件或 Hooks)结合使用,以实现更复杂的 UI 交互和功能。
recommend-type

JSBSim Reference Manual

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