管理系统的源码

本文介绍了如何构建一个管理系统的源码,包括成员基类Person,以及学生和教师子类,还有不同操作页面如添加、删除、修改和查询。通过主函数初始化数据库,使用LoginPage进行登录选择,进入对应的StudentPage或TeacherPage执行操作。源码可在百度云下载获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

基本思路如下:

1.成员基类Person,学生类Students与教师类Teacher继承Person;

2.操作基类OperationPage,添加、删除、修改、查询类 继承自OperationPage;

3.主函数里初始化数据库信息,实例化LoginPage类,loginPage选择登录教师还是学生,相应进入到studentPage与teacherPage进行操作。

main.cpp

#include <QApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QPalette>
#include <QPixmap>
#include <QIcon>
#include "loginpage.h"
#include "warning.h"
#include "teacher.h"
#include "students.h"

bool Init()         //连接数据库
{
    QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");   //数据库驱动类型为SQL Server
    db.setHostName("127.0.0.1");                            //选择本地主机,127.0.1.1
    db.setDatabaseName("SchoolManagementSystem");           //设置数据源名称
    db.setUserName("sa");                                   //登录用户
    db.setPassword("123456");                               //密码
    return db.open();
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    if(Init())
    {
        LoginPage *loginPage = new LoginPage();
        loginPage->setWindowTitle("SchoolManagementSystem");
        loginPage->setFixedSize(600,500);
        loginPage->setAutoFillBackground(true);
        QPalette palette;
        QPixmap pixmap("F:/C++ Qt/SchoolManagementSystem/background/background.jpg");
        palette.setBrush(QPalette::Window, QBrush(pixmap));
        loginPage->setPalette(palette);
        loginPage->setWindowIcon(QIcon("F:/C++ Qt/SchoolManagementSystem/background/loginPageIcon.jpg"));
        loginPage->setWindowTitle("Login");
        loginPage->show();
        QString teacherListName = "Teacher";
        QString studentListName = "Student";
        //默认第一位老师信息
        QString DF_TeacherName = "df_name";
        QString DF_TeacherPwd = "123456";
        Teacher *DF_teacher = new Teacher(DF_TeacherName, DF_TeacherPwd);

        //初始化管理者--创建管理者表,并插入第一位管理者信息
        QString initTeacher = QString("IF object_id('%1','u') is null "
                                      "CREATE TABLE %1 "
                                      "(TeacherId VARCHAR(20) PRIMARY KEY ,"
                                      "TeacherPwd VARCHAR(20) NOT NULL ,"
                                      "TeacherName VARCHAR(20) NOT NULL, "
                                      "TeacherSex VARCHAR(20) NOT NULL DEFAULT 'Male', )")
                                      .arg(teacherListName)
                                      .arg(teacherListName);

        QString creat_DF_manager = QString("IF NOT EXISTS "
                                     "( SELECT * FROM %1 "
                                     "WHERE teacherId = '%2') "
                                     "INSERT INTO %3 ( teacherName,teacherId,TeacherPwd) "
                                     "VALUES ('%4','%5','%6');")
                                     .arg(teacherListName)
                                     .arg("0")
                                     .arg(teacherListName)
                                     .arg(DF_teacher->getName())
                                     .arg(DF_teacher->getEmployeeId())
                                     .arg(DF_teacher->getPassword());

        QString initStudent = QString("IF object_id('%1','u') is null "
                                   "CREATE TABLE %1 "
                                   "(StudentId VARCHAR(20) PRIMARY KEY NOT NULL ,"
                                   "StudentPwd VARCHAR(20) NOT NULL DEFAULT '123456' ,"
                                   "StudentName VARCHAR(20) NOT NULL ,"
                                   "StudentSex VARCHAR(20) NOT NULL DEFAULT 'Male', "
                                   "StudentChineseGrades float ,"
                                   "StudentMathGrades float ,"
                                   "StudentEnglishGrades float)")
                                   .arg(studentListName)
                                   .arg(studentListName);

        QSqlQuery *query = new QSqlQuery;
        query->exec(initTeacher);
        query->exec(initStudent);
        query->exec(creat_DF_manager);


    }
    else
    {
        Warning *warning = new Warning();
        warning->setContents("fail to connect Database");
        warning->setWindowTitle("Warning");
        warning->show();
    }
    return a.exec();

}

loginPage.cpp

#include "loginpage.h"
#include "ui_loginpage.h"
#include "warning.h"
#include "teacherpage.h"
#include "studentpage.h"
#include <QCheckBox>
#include <QLineEdit>
#include <QLabel>
#include <QPushButton>
#include <QGridLayout>
#include <QButtonGroup>
#include <QSqlTableModel>
#include <QHBoxLayout>


LoginPage::LoginPage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::LoginPage)
{
    Init();
    ui->setupUi(this);

}

LoginPage::~LoginPage()
{
    delete ui;
}

void LoginPage::Init()
{
    this->choiceStudent = new QCheckBox();
    this->choiceTeacher = new QCheckBox();
    this->choicesGroup = new QButtonGroup(this);
    this->choiceStudent->setText("Student");
    this->choiceTeacher->setText("Teacher");
    choicesGroup->addButton(choiceStudent,1);
    choicesGroup->addButton(choiceTeacher,2);
    this->choiceTeacher->setChecked(true);
    this->teacherOrStudent = true;
    QObject::connect(this->choiceStudent,SIGNAL(clicked(bool)),SLOT(ChoiceStudent(bool)));
    QObject::connect(this->choiceTeacher,SIGNAL(clicked(bool)),SLOT(ChoiceTeacher(bool)));

    this->input_Id = new QLineEdit();
    this->input_pwd = new QLineEdit();
    this->input_pwd->setEchoMode(QLineEdit::Password);

    this->Id = new QLabel(tr("Student/Employee ID: "));
    this->password = new QLabel(tr("Password:"));

    this->bnt_login = new QPushButton();
    this->bnt_quit = new QPushButton();
    this->bnt_login->setText("login");
    this->bnt_quit->setText("quit");
    QObject::connect(this->bnt_login,SIGNAL(clicked(bool)),this,SLOT(BntLoginClick(bool)));
    QObject::connect(this->bnt_quit,SIGNAL(clicked(bool)),this,SLOT(BntQuitClick(bool)));

    this->mainLayout = new QGridLayout(this);

    mainLayout->addWidget(choiceStudent, 1, 0);
    mainLayout->addWidget(choiceTeacher, 0, 0);

    mainLayout->addWidget(Id, 2, 0);
    mainLayout->addWidget(input_Id, 2, 1);

    mainLayout->addWidget(password, 3, 0);
    mainLayout->addWidget(input_pwd, 3, 1);

    QHBoxLayout *hLayout = new QHBoxLayout;
    hLayout->addWidget(bnt_login);
    hLayout->addWidget(bnt_quit);
    mainLayout->addLayout(hLayout,4,0,1,2);
}

void LoginPage::BntLoginClick(bool)
{
    QString inputId = input_Id->text();
    QString inputPwd = input_pwd->text();

    if(inputId == "" || inputPwd == "")
    {
        Warning *warning = new Warning();
        warning->setContents("Please input the complete content");
        warning->setWindowTitle("Warning");
        warning->show();
        return;
    }

    if(this->choiceStudent->isChecked())
        this->teacherOrStudent = false;

    if(LoginCheck(teacherOrStudent, inputId, inputPwd))     //登录成功
    {
        if(teacherOrStudent)
        {
            TeacherPage *teacherPage = new TeacherPage;
            teacherPage->setFixedSize(650,500);
            teacherPage->setWindowTitle("Teacher");
            QObject::connect(teacherPage,SIGNAL(reshow()),this,SLOT(Reshow()));
            teacherPage->setWindowIcon(QIcon("F:/C++ Qt/SchoolManagementSystem/background/teacherIcon.jpg"));
            teacherPage->show();
        }
        else
        {
            StudentPage *studentPage = new StudentPage;
            studentPage->setFixedSize(650,500);
            studentPage->setWindowTitle("Student");
            studentPage->setStudentId(inputId);
            QObject::connect(studentPage,SIGNAL(reshow()),this,SLOT(Reshow()));
            studentPage->setWindowIcon(QIcon("F:/C++ Qt/SchoolManagementSystem/background/studentIcon.jpg"));
            studentPage->show();
        }
        this->close();
    }
    else
    {
        Warning *warning = new Warning();
        warning->setContents("fail to login");
        warning->show();
        return;
    }

}

void LoginPage::BntQuitClick(bool)
{
    this->~LoginPage();
}

bool LoginPage::LoginCheck(bool aTeacherOrStudent, QString aId, QString aPwd)
{
    QString tableName;
    if(aTeacherOrStudent)            //Teacher
    {
        tableName = "Teacher";
    }
    else                             //Student
    {
        tableName = "Student";
    }

    QSqlTableModel model;
    model.setTable(tableName);                                  //相当于SQL语句 SELECT * FROM Teacher
    QString query = QString(" %1 = '%2' and %3 = '%4'")         //用%1代替字符串时不需要加'',加''是SQL “=” 的语法
            .arg(tableName + "Id")
            .arg(aId)
            .arg(tableName + "Pwd")
            .arg(aPwd);
    model.setFilter(query);
    model.select();                                             //执行
   if(model.rowCount() == 1)                                    //表中存在一栏,表示Id与密码匹配,登录成功
       return true;
   else
       return false;
}

void LoginPage::Reshow()
{
    this->input_Id->clear();
    this->input_pwd->clear();
    this->show();
}

void LoginPage::ChoiceStudent(bool)
{
    this->teacherOrStudent = false;
}

void LoginPage::ChoiceTeacher(bool)
{
    this->teacherOrStudent = true;
}

teacherPage.cpp

#include "teacherpage.h"
#include "students.h"
#include "teacher.h"
#include "queryinfo.h"
#include "addinfo.h"
#include "deleteinfo.h"
#include "alterinfo.h"
#include <qdebug.h>
#include <QPushButton>
#include <QHBoxLayout>
#include <QTableView>
#include <QSqlTableModel>

TeacherPage::TeacherPage()
{
    this->bnt_add = new QPushButton();
    this->bnt_delete = new QPushButton();
    bnt_add->setText("Add");
    bnt_delete->setText("Delete");
    QObject::connect(this->bnt_add,SIGNAL(clicked(bool)),this,SLOT(BntAddClick(bool)));
    QObject::connect(this->bnt_delete,SIGNAL(clicked(bool)),this,SLOT(BntDeleteClick(bool)));

    this->HLayout->removeWidget(this->bnt_quit);
    this->HLayout->addWidget(bnt_add);
    this->HLayout->addWidget(bnt_delete);
    this->HLayout->addWidget(this->bnt_quit);
}

void TeacherPage::BntAddClick(bool)
{

    AddInfo *addInfo = new AddInfo;
    addInfo->setWindowIcon(QIcon("F:/C++ Qt/SchoolManagementSystem/background/teacherIcon.jpg"));
    addInfo->setWindowTitle("AddPage");
    addInfo->show();
}

void TeacherPage::BntAlterClick(bool)
{
    AlterInfo *alterInfo = new AlterInfo;
    alterInfo->setWindowTitle("AlterPage");
    alterInfo->setWindowIcon(QIcon("F:/C++ Qt/SchoolManagementSystem/background/teacherIcon.jpg"));
    alterInfo->show();
}

void TeacherPage::BntDeleteClick(bool)
{
    DeleteInfo *deleteInfo = new DeleteInfo;
    deleteInfo->setWindowTitle("DeletePage");
    deleteInfo->setWindowIcon(QIcon("F:/C++ Qt/SchoolManagementSystem/background/teacherIcon.jpg"));
    deleteInfo->show();
}

void TeacherPage::BntQueryClick(bool)
{
    QueryInfo *queryInfo = new QueryInfo;
    queryInfo->setWindowTitle("QueryPage");
    queryInfo->setWindowIcon(QIcon("F:/C++ Qt/SchoolManagementSystem/background/teacherIcon.jpg"));
    QObject::connect(queryInfo,SIGNAL(showInfo(QString,QString)),this,SLOT(ShowInfo(QString,QString)));
    queryInfo->show();
}

void TeacherPage::BntQuitClick(bool)
{
    emit reshow();
    this->close();
}

void TeacherPage::ShowInfo(QString aTeacherOrStudent, QString aId)
{
    this->model = new QSqlTableModel;
    model->setTable(aTeacherOrStudent);
    if(aId != "")
        model->setFilter(QString(" %1 = '%2'").arg(aTeacherOrStudent + "Id").arg(aId));
    model->select();
    this->view->showGrid();
    this->view->setModel(model);
}

addInfo.cpp

#include "addinfo.h"
#include "warning.h"
#include "students.h"
#include "teacher.h"
#include <qdebug.h>
#include <QComboBox>
#include <QLineEdit>
#include <QLabel>
#include <QGridLayout>
#include <QHBoxLayout>
#include <QComboBox>
#include <QSqlTableModel>
#include <QSqlQuery>
#include <QIntValidator>
AddInfo::AddInfo()
{
 
 
    this->model = new QSqlTableModel;
    this->query = new QSqlQuery;
    this->input_name = new QLineEdit;
    this->input_pwd = new QLineEdit;
    this->input_ChineseGrades = new QLineEdit;
    this->input_MathGrades = new QLineEdit;
    this->input_EnglishGrades = new QLineEdit;
    QValidator *validator=new QIntValidator(0,100,this);
    this->input_ChineseGrades->setValidator(validator);
    this->input_MathGrades->setValidator(validator);
    this->input_EnglishGrades->setValidator(validator);
    this->choiceSex =new QComboBox;
    choiceSex->insertItem(1,"Male");
    choiceSex->insertItem(2,"Female");
 
    this->Name = new QLabel(tr("name"));
    this->Pwd = new QLabel(tr("password"));
    this->ChineseGrades = new QLabel(tr("ChineseGrades"));
    this->MathGrades = new QLabel(tr("MathGrades"));
    this->EnglishGrades = new QLabel(tr("EnglishGrades"));
    this->Sex = new QLabel(tr("Sex"));
 
 
    this->mainLayout->removeItem(this->HLayout);
    this->mainLayout->addWidget(Pwd,3,0);
    this->mainLayout->addWidget(input_pwd,3,1);
    this->mainLayout->addWidget(Name,4,0);
    this->mainLayout->addWidget(input_name,4,1);
    this->mainLayout->addWidget(Sex,5,0);
    this->mainLayout->addWidget(choiceSex,5,1);
 
    this->mainLayout->addWidget(ChineseGrades,6,0);
    this->mainLayout->addWidget(input_ChineseGrades,6,1);
    this->mainLayout->addWidget(MathGrades,7,0);
    this->mainLayout->addWidget(input_MathGrades,7,1);
    this->mainLayout->addWidget(EnglishGrades,8,0);
    this->mainLayout->addWidget(input_EnglishGrades,8,1);
 
    this->mainLayout->addLayout(this->HLayout,9,0,1,2);     //占1列宽度,2列的高度
 
    this->input_ChineseGrades->setEnabled(false);

                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值