qt项目:员工信息管理系统

时间:2021-09-20 10:49:19

开发一个员工信息管理系统

 一、项目具体要求:

   1、用qt开发界面,数据库用QSqlite 数据库文件名:demostudent.db

   2、通过界面能够查看到数据库中员工信息表中内容,包括员工姓名、年龄、性别、

成绩、生日、基本工资和照片。

   3、能够查看指定记录,修改和编辑员工信息

   4、员工信息中包含照片,界面上可以显示学员照片并且可以修改学员照片

5、通过界面可增加员工记录,可以删除指定的学员记录

 

二、项目界面展示

1、主界面:通过openDB按钮打开指定的数据库文件,将数据库表中的学员信息显示在主界面中tableView中。

  

  其它的按钮依次是 插入按钮、编辑当前记录和删除指定记录

  点击upSalary按钮,所有学员的基本工资增加1000

 qt项目:员工信息管理系统

 

2、编辑学员信息的界面

   单击编辑按钮或者双击某一个记录,则弹出如下编辑窗口

  编辑窗口通过相关控件显示指定的记录各个字段的数据,包括照片信息

  修改相关数据之后,点击ok按钮之后,则新数据更新到数据库中

  通过setPhoto和clearPhoto两个按钮可以设置和修改照片

  qt项目:员工信息管理系统

3、增加学员信息的界面

  通过界面相关控件录入员工的信息,包括设置照片。

 点击ok按钮,将学员信息插入到数据库表中。

 qt项目:员工信息管理系统

三、项目代码实现:

1、项目中主要为两个类文件,描述界面的类文件mainWidget

    和描述插入和编辑界面的类文件wDialogData

2、mainWidget类文件代码如下:

mainWidget.h

#ifndef MAINWINDOW_H

#define MAINWINDOW_H

 

#include <QMainWindow>

#include <QSqlDatabase>

#include <QSqlQueryModel>

#include <QItemSelectionModel>

 

namespace Ui {

class MainWindow;

}

 

class MainWindow : public QMainWindow

{

    Q_OBJECT

 

public:

    explicit MainWindow(QWidget *parent = 0);

    ~MainWindow();

 

private slots:

    void on_actOpenDB_triggered();

 

    void on_actInsert_triggered();

 

    void on_actEdit_triggered();

 

    void on_tableView_doubleClicked(const QModelIndex &index);

 

    void on_actDelete_triggered();

 

    void on_actUpsalary_triggered();

 

private:

    Ui::MainWindow *ui;

    QSqlDatabase db;

    QSqlQueryModel *theModel;

    QItemSelectionModel *theSelection;

 

    void openTable();

    void updateRecord(int recNo);

};

 

#endif // MAINWINDOW_H

 

mainWidget.cpp

#include "mainwindow.h"

#include "ui_mainwindow.h"

#include "wdialogdata.h"

#include <QFileDialog>

#include <QMessageBox>

#include <QDebug>

#include <QSqlError>

#include <QSqlRecord>

#include <QSqlQuery>

 

MainWindow::MainWindow(QWidget *parent) :

    QMainWindow(parent),

    ui(new Ui::MainWindow)

{

    ui->setupUi(this);

}

 

MainWindow::~MainWindow()

{

    delete ui;

}

 

void MainWindow::openTable()

{

    theModel = new QSqlQueryModel(this);

    theSelection = new QItemSelectionModel(theModel);

    theModel->setQuery("select id,name,age,sex,birthday,salary,photo from stu");

    if(theModel->lastError().isValid())

    {

        QMessageBox::information(this,"error","select error\n" + theModel->lastError().text(),QMessageBox::Ok,QMessageBox::NoButton);

        return;

    }

 

    theModel->setHeaderData(0,Qt::Horizontal,"工号");

    theModel->setHeaderData(1,Qt::Horizontal,"姓名");

    theModel->setHeaderData(2,Qt::Horizontal,"年龄");

    theModel->setHeaderData(3,Qt::Horizontal,"性别");

    theModel->setHeaderData(4,Qt::Horizontal,"生日");

    theModel->setHeaderData(5,Qt::Horizontal,"工资");

    ui->tableView->setModel(theModel);

    ui->tableView->setColumnHidden(6,true);

    ui->tableView->setSelectionModel(theSelection);

    ui->actOpenDB->setEnabled(false);

    ui->actInsert->setEnabled(true);

    ui->actDelete->setEnabled(true);

    ui->actEdit->setEnabled(true);

 

}

 

void MainWindow::updateRecord(int recNo)

{

    QSqlRecord curRec = theModel->record(recNo);

    //int empNo = curRec.value("id").toInt();

    QSqlQuery query;

 

    WDialogData *dataDialog  = new WDialogData(this);

    dataDialog->setUPdateRecord(curRec);

    int ret = dataDialog->exec();

    if(ret == QDialog::Accepted)

    {

        QSqlRecord recData = dataDialog->getRecord();

        query.prepare("update stu set name = :name,age = :age ,sex = :sex,birthday = :birthday,salary = :salary,photo = :photo where id = :id");

        query.bindValue(":id",recData.value("id"));

        query.bindValue(":name",recData.value("name"));

        query.bindValue(":age",recData.value("age"));

        query.bindValue(":sex",recData.value("sex"));

        query.bindValue(":birthday",recData.value("birthday"));

        query.bindValue(":salary",recData.value("salary"));

        query.bindValue(":photo",recData.value("photo"));

 

        if(!query.exec())

            QMessageBox::critical(this,"error","update error\n" + query.lastError().text(),QMessageBox::Ok,QMessageBox::NoButton);

        else

            theModel->query().exec();

    }

 

    delete dataDialog;

}

 

void MainWindow::on_actOpenDB_triggered()

{

    QString aFile = QFileDialog::getOpenFileName(this,"select database file","","sqlite(*.db *.db3)");

    if(aFile.isEmpty())

    {

        return;

    }

 

    db = QSqlDatabase::addDatabase("QSQLITE");

    db.setDatabaseName(aFile);

    qDebug()<<aFile;

 

    if(!db.open())

    {

        QMessageBox::warning(this,"error","open database error",QMessageBox::Ok,QMessageBox::NoButton);

        return;

    }

 

    openTable();

 

}

 

void MainWindow::on_actInsert_triggered()

{

    QSqlQuery query;

    query.exec("select * from stu where id = -1");

    QSqlRecord curRec = query.record();

    curRec.setValue("id",theModel->rowCount() + 2000);

 

    qDebug()<<curRec.value("id").toInt();

 

    WDialogData *dataDialog = new WDialogData(this);

    dataDialog->setInsertRecord(curRec);

 

    int ret = dataDialog->exec();

    if(ret == QDialog::Accepted)

    {

        QSqlRecord recData = dataDialog->getRecord();

        query.prepare("insert into stu(id,name,age,sex,birthday,salary,photo) values(:id,:name,:age,:sex,:birthday,:salary,:photo)");

        query.bindValue(":id",recData.value("id"));

        query.bindValue(":name",recData.value("name"));

        query.bindValue(":age",recData.value("age"));

        query.bindValue(":sex",recData.value("sex"));

        query.bindValue(":salary",recData.value("salary"));

        query.bindValue(":photo",recData.value("photo"));

 

        query.bindValue(":birthday",recData.value("birthday"));

        if(!query.exec())

        {

            QMessageBox::critical(this,"error","insert error\n" + query.lastError().text(),QMessageBox::Ok,QMessageBox::NoButton);

        }else

             theModel->setQuery("select id,name,age,sex,birthday,salary,photo from stu");

    }

 

    delete dataDialog;

 

 

}

 

void MainWindow::on_actEdit_triggered()

{

    int curRecNo = theSelection->currentIndex().row();

    updateRecord(curRecNo);

}

 

void MainWindow::on_tableView_doubleClicked(const QModelIndex &index)

{

    int curRecNo = index.row();

    updateRecord(curRecNo);

}

 

void MainWindow::on_actDelete_triggered()

{

    int curRecord = theSelection->currentIndex().row();

    QSqlRecord curRec = theModel->record(curRecord);

    if(curRec.isEmpty())

        return;

    int empNo = curRec.value("id").toInt();

    QSqlQuery query;

    query.prepare("delete from stu where id = :id");

    query.bindValue(":id",empNo);

 

    if(!query.exec())

        QMessageBox::critical(this,"error","delete error\n" + query.lastError().text(),QMessageBox::Ok,QMessageBox::NoButton);

    else

        theModel->query().exec();

 

}

 

void MainWindow::on_actUpsalary_triggered()

{

    QSqlQuery qryEmpList;

    qryEmpList.exec("select id,salary from stu order by id");

    qryEmpList.first();

 

    QSqlQuery qryUpdate;

    qryUpdate.prepare("update stu set salary = :salary where id = :id");

    while (qryEmpList.isValid()) {

       int empId = qryEmpList.value("id").toInt();

       float salary = qryEmpList.value("salary").toFloat();

       salary = salary + 1000;

       qryUpdate.bindValue(":id",empId);

       qryUpdate.bindValue(":salary",salary);

 

       qryUpdate.exec();

       if(!qryEmpList.next())

           break;

    }

    theModel->query().exec();

    QMessageBox::information(this,"notice","update salary finish",QMessageBox::Ok,QMessageBox::NoButton);

}

 

 

3、wDialogData类文件代码如下:

wDialogData.h

   #ifndef WDIALOGDATA_H

#define WDIALOGDATA_H

 

#include <QDialog>

#include <QSqlRecord>

 

namespace Ui {

class WDialogData;

}

 

class WDialogData : public QDialog

{

    Q_OBJECT

 

public:

    explicit WDialogData(QWidget *parent = 0);

    ~WDialogData();

    void setUPdateRecord(QSqlRecord &recData);

    void setInsertRecord(QSqlRecord &recData);

    QSqlRecord getRecord();

private:

    QSqlRecord mRecord;

private:

    Ui::WDialogData *ui;

private slots:

 

    void on_setPhoto_clicked();

    void on_clearPhoto_clicked();

};

 

#endif // WDIALOGDATA_H

 

wDialogData.cpp

 

#include "wdialogdata.h"
#include "ui_wdialogdata.h"
#include <QDebug>
#include <QDate>
#include <QFileDialog>
 
WDialogData::WDialogData(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::WDialogData)
{
    ui->setupUi(this);
 
}
 
WDialogData::~WDialogData()
{
    delete ui;
}
 
void WDialogData::setUPdateRecord(QSqlRecord &recData)
{
    mRecord = recData;
    ui->spinEmpNo->setEnabled(false);
    setWindowTitle("update");
    ui->spinEmpNo->setValue(recData.value("id").toInt());
    ui->editName->setText(recData.value("name").toString());
    ui->spinAge->setValue(recData.value("age").toInt());
 
    QString strDate =  recData.value("birthday").toString();
    ui->EditBirth->setDate(QDate::fromString(strDate,"yyyy-MM-dd"));
 
    ui->comboSex->setCurrentText(recData.value("sex").toString());
    ui->spinSalary->setValue(recData.value("salary").toInt());
 
    QVariant va = recData.value("photo");
 
    if(!va.isValid())
        ui->labPhoto->clear();
    else
    {
        QByteArray data = va.toByteArray();
        QPixmap pic;
        pic.loadFromData(data);
        ui->labPhoto->setPixmap(pic.scaledToWidth(ui->labPhoto->size().width()));
    }
}
 
void WDialogData::setInsertRecord(QSqlRecord &recData)
{
    mRecord = recData;
 
    qDebug()<<recData.value("id").toInt();
 
    ui->spinEmpNo->setEnabled(true);
    setWindowTitle("insert a new record");
    ui->spinEmpNo->setValue(recData.value("id").toInt());
}
 
QSqlRecord WDialogData::getRecord()
{
    mRecord.setValue("id",ui->spinEmpNo->value());
    mRecord.setValue("name",ui->editName->text());
    mRecord.setValue("sex",ui->comboSex->currentText());
    mRecord.setValue("age",ui->spinAge->value());
    mRecord.setValue("birthday",ui->EditBirth->date());
    mRecord.setValue("salary",ui->spinSalary->value());
 
    return mRecord;
 
}
 
void WDialogData::on_setPhoto_clicked()
{
    QString aFile = QFileDialog::getOpenFileName(this,"select photo","","photo(*.jpg)");
    if(aFile.isEmpty())
        return;
    QByteArray data;
    QFile *file = new QFile(aFile);
    file->open(QIODevice::ReadOnly);
    data = file->readAll();
    file->close();
    mRecord.setValue("photo",data);
    QPixmap pic;
    pic.loadFromData(data);
    ui->labPhoto->setPixmap(pic.scaledToWidth(ui->labPhoto->size().width()));
 
}
 
void WDialogData::on_clearPhoto_clicked()
{
    ui->labPhoto->clear();
    mRecord.setNull("photo");
}