如何在Qt上实现一个考勤表,并提供示例代码
时间: 2023-05-29 08:05:14 浏览: 105
考勤报表制作
我不知道您需要的是什么样的考勤表,但是以下是一个简单的示例,它创建一个基本的考勤表,并允许用户添加、删除和编辑条目。
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”。如果您需要不同的格式,请相应地修改代码。
阅读全文