QT homework student information management system

There are ten thousand wine fights in gold cups and ten thousand money in jade plates.
I can't eat when I stop the cup, and I'm at a loss when I draw my sword.
If you want to cross the Yellow River, you will climb the Taihang snow covered mountain.
When I came here to fish in Bixi, I suddenly dreamed of the sun side by boat.
It's hard to travel! It's hard to travel! How wrong is it? Where are you today?
I will mount a long wind some day and break the heavy waves, and set my cloudy sail straight and bridge the deep, deep sea.
- Tang Libai

1, General analysis

Through the analysis of the overall function of the system in the demand stage, we get the overall function structure of the student information management system, and its main functions are shown in the functional structure chart above. It should include four basic function modules.
(1) Information adding module: by adding user information, administrator information and class information, the whole student information management system can obtain data input, which provides necessary conditions for the operation of the whole student information management system.
(2) Information deletion module: by deleting user information, administrator information and class information, the useless data in the student information management system can be cleared in time, so as to better manage the effective data.
(3) Information modification module: through the modification of user information, administrator information, class information, the information in the student information management system can be updated in time to ensure the real-time system information.
(4) Information viewing module: for administrators, information can be viewed before other operations can be performed. For students, it is the only way to view their own data and change their password.

2, Realization effect picture

(1) Screenshot of main functions of administrator






(2) Screenshot of main functions of students

3, Software code analysis

This student information management system mainly uses QT layout and design to add, delete, modify and check the database, so the main code is actually the code of the database. Although QT does not need to install the driver to connect to the Sqiite database like mysql, it needs to add sql to the file as shown in the figure below.

(1) The first is the code to create the database and three tables, which I write in the main function. Some data is inserted when creating the table. As an example, the administrator's account and password are inserted to initialize the administrator's login. Otherwise, without an administrator and no students, the system cannot be logged on.

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QSqlDatabase db= QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("StudentInformationManagementSystem.db");
    if ( !db.open()){
           qDebug()<<"Not open!";
       }
    QSqlQuery query;//The QSqlQuery class provides methods for executing and operating SQL statements
  //Create database admin table
    query.exec("CREATE TABLE admin ("
                       "admin_account VARCHAR(40) PRIMARY KEY NOT NULL,"
                       "admin_password VARCHAR(40) NOT NULL)");
    //Create database student table
        query.exec("CREATE TABLE student ("
                       "id VARCHAR(50) PRIMARY KEY NOT NULL, "
                       "name VARCHAR(30) NOT NULL, "
                       "sex VARCHAR(10) NOT NULL, "
                       "age INTEGER NOT NULL,"
                       "address VARCHAR(50) NOT NULL,"
                       "phone_number VARCHAR(30) NOT NULL,"
                       "password VARCHAR(30) NOT NULL,"
                       "dormitory VARCHAR(30) NOT NULL,"
                       "className VARCHAR(30) NOT NULL)");
    query.exec("CREATE TABLE class ("
                       "name VARCHAR(40) PRIMARY KEY NOT NULL, "
                       "department VARCHAR(50) NOT NULL, "
                       "instructor VARCHAR(40) NOT NULL, "
                       "instructor_number VARCHAR(40) NOT NULL,"
                        "people_number INTEGER NOT NULL)");
    Login w;
    w.show();

    return a.exec();
}

(2) Next is the login administrator and the student's account password verification. According to whether the selected radioButton is an administrator or a student, check the database respectively. The administrator compares the account password in the database one by one (the student user compares the student number and password one by one) with the account password in the input box. Find the data to compare with. If it is correct, the login succeeds. If it fails, the account or password error box pops up. The code is as follows.

//Click login
void Login::on_loginBtn_clicked()
{
    QString username = ui->zhanghao->text();
    QString password = ui->password->text();
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QSQLITE");//Specify SQLite
    db.setDatabaseName("StudentInformationManagementSystem.db");//data base
    if (!db.open())
            qDebug() << "connection failed";
    //If the administrator is selected
    if( ui->adminRadio->isChecked()){
        QSqlQuery query(db);
        db.exec("SET NAMES 'GBK'");//Prevent confusion code
        query.exec("select admin_account,admin_password from admin");
        bool T1=false;
        while(query.next())
                   {
                       QString user = query.value(0).toString();
                       QString pass = query.value(1).toString();
                       if(username.compare(user)==0&&password.compare(pass)==0){
                           T1=true;
                           login_zhanghao=user;//Record the login's account, and you will know who is logged in.
                           this->hide();
                           admin=new adminFace;
                           admin->show();
                       }
                  }
        if(T1==false)
         QMessageBox::information(this, "warning", "Wrong user name or password");
 }

    if( ui->studentRadio->isChecked()){
        QSqlQuery query(db);
        db.exec("SET NAMES 'GBK'");//Prevent confusion code
        query.exec("select id,password from student");
        bool zhanghaoAndPassword=false;
        while(query.next())
               {
                   QString user = query.value(0).toString();
                   QString pass = query.value(1).toString();
                   if(username.compare(user)==0&&password.compare(pass)==0){
                       zhanghaoAndPassword=true;
                       login_zhanghao=user;//Record student's account number
                       login_password=pass;//The student's password is recorded here for modification
                       //this->hide();
                       this->close();
                       student=new studentFace;
                       student->setModal(true);
                       student->show();
                       break;
                   }
               }

    if(zhanghaoAndPassword==false)
     QMessageBox::information(this, "warning", "Wrong user name or password");
    }
}

(3) Then, we will find that, as shown in the above code, the login window will be hidden and the administrator interface will be created, which means the login is successful. For student users, after successful login, they will see their own information and can change the password, which is all operation. For administrators, they can operate all kinds of data after logging in. The operation of the three tables is to add, delete, modify and query, so the administrator is used to manage the student information table as an example. After clicking on the student information table, first let the existing student information be displayed in the form of table view. The code is written in the construction method as follows:

 QSqlQuery query;
    QSqlQueryModel *model = new QSqlQueryModel(this);
       model->setQuery("select id,password,name,sex,age,address,phone_number,dormitory,className from student");
       //Here are the names of the columns shown
       model->setHeaderData(0,Qt::Horizontal,tr("Student ID"));
       model->setHeaderData(1,Qt::Horizontal,tr("Password"));
       model->setHeaderData(2,Qt::Horizontal,tr("Full name"));
       model->setHeaderData(3,Qt::Horizontal,tr("Gender"));
       model->setHeaderData(4,Qt::Horizontal,tr("Age"));
       model->setHeaderData(5,Qt::Horizontal,tr("Native place"));
       model->setHeaderData(6,Qt::Horizontal,tr("Contact number"));
       model->setHeaderData(7,Qt::Horizontal,tr("Dorm"));
       model->setHeaderData(8,Qt::Horizontal,tr("Class name"));
       ui->tableView->setModel(model);

(4) Finally, the core code is to add, delete, modify and query the database, also taking the student information table as an example.
1. Add student information

//Determine submission data
void admin_student_add::on_pushButton_clicked()
{
    QString addId=ui->idEdit->text();
    QString addPassword=ui->passwordEdit->text();
    QString addName=ui->nameEdit->text();
    QString addSex=ui->sexEdit->text();
    int addage=ui->ageEdit->text().toInt();
    QString address=ui->addressEdit->text();
    QString addPhone_number=ui->phone_numberEdit->text();
    QString addDormitory=ui->dormitoryEdit->text();
    QString addClassName=ui->classNameEdit->text();
    if(addId.isEmpty()||addName.isEmpty()||addPassword.isEmpty()||addSex.isEmpty()||addage<=0||address.isEmpty()||addPhone_number.isEmpty()||addDormitory.isEmpty()||addClassName.isEmpty()){
        QMessageBox::information(this, "warning", "Incomplete or wrong filling");
    }else{
        QSqlQuery query;
        //Judge whether the input student number exists in the database
        query.exec("select id from student");
        bool isExist=false;//Does it exist?
        while(query.next())
                   {
                       QString id=query.value(0).toString();
                       if(id.compare(addId)==0){
                           QMessageBox::information(this, "warning", "This account already exists!");
                           isExist=false;
                           //this->close();
                           break;
                       }
                       isExist=true;
                  }
     //If there is no data at the beginning, query.next() will not execute, so there will be no response. Increase! query.next(), so that the first increased data can be increased successfully
        if(!query.next()){
            isExist=true;
        }
        if(isExist){
                     QString sql=QString("insert into student(id,password,name,sex,age,address,phone_number,dormitory,className)values('%1','%2','%3','%4','%5','%6','%7','%8','%9')").arg(addId).arg(addPassword).arg(addName).arg(addSex).arg(addage).arg(address).arg(addPhone_number).arg(addDormitory).arg(addClassName);
                     query.exec(sql);
                     QMessageBox::information(this, "Reminder", "Congratulations, student ID:"+addId+"Add success");
                     qDebug()<<addId+"Add success";
                     this->close();
                     admin_student_show *newAdminStudentShow=new admin_student_show;
                     newAdminStudentShow->show();
        }

}
}

2. Delete student information

//Delete student information
void admin_student_show::on_pushButton_3_clicked()
{
    QString wantDeleteStuentId=ui->wantDeleteEdit->text();
    if(wantDeleteStuentId==""){QMessageBox::information(this, "warning", "You have not entered the student ID you want to delete");return;};
    //Determine whether the class you want to delete exists in the database
    QSqlQuery query;
    query.exec("select id from student");
    bool isExist=false;//Does it exist?
    while(query.next())
               {
                   QString tempStudentId=query.value(0).toString();
                   if(tempStudentId.compare(wantDeleteStuentId)==0){
                       QString sql=QString("delete from student where id='%1'").arg(wantDeleteStuentId);
                       query.exec(sql);
                        QMessageBox::information(this, "Reminder", "Congratulations, student ID:"+wantDeleteStuentId+"Delete successful");
                       qDebug()<<wantDeleteStuentId+"Delete successful";
                       isExist=false;
                       //Auto refresh table
                       this->close();
                       admin_student_show *newAdminStudentShow=new admin_student_show;
                       newAdminStudentShow->show();
                       break;
                   }
                   isExist=true;
              }
    if(isExist){
                  QMessageBox::information(this, "warning", "Sorry, no student ID:"+wantDeleteStuentId);
                 qDebug()<<wantDeleteStuentId+"Delete failed";
                 //this->close();
                 ui->wantDeleteEdit->setText("");
    }
}

3. To modify the student information, you need to find the student information first and then modify it

//Update modification data
void admin_student_updata::on_pushButton_clicked()
{
    if(ifNotFindIdDo){ QMessageBox::information(this, "warning", "Please input the student number first");return;}
    QString updateId=ui->updateIdEdit->text();
    QString updatePassword=ui->passwordEdit->text();
    QString updateName=ui->nameEdit->text();
    QString updateSex=ui->sexEdit->text();
    int updateage=ui->ageEdit->text().toInt();
    QString updatedress=ui->addressEdit->text();
    QString updatePhone_number=ui->phone_numberEdit->text();
    QString updateDormitory=ui->dormitoryEdit->text();
    QString updateClassName=ui->classNameEdit->text();
    if(updateId.isEmpty()||updateName.isEmpty()||updatePassword.isEmpty()||updateSex.isEmpty()||updateage<=0||updatedress.isEmpty()||updatePhone_number.isEmpty()||updateDormitory.isEmpty()||updateClassName.isEmpty()){
        QMessageBox::information(this, "warning", "Incomplete or wrong filling");
    }else{
        QSqlQuery query;
        QString wantUpdateStudentId=ui->idEdit->text();
        QString sql=QString("update student set id='%1',password='%2',name='%3',sex='%4',age='%5',address='%6',phone_number='%7',dormitory='%8',className='%9' where id='%10'").arg(updateId).arg( updatePassword).arg( updateName).arg( updateSex).arg( updateage).arg( updatedress).arg( updatePhone_number).arg( updateDormitory).arg( updateClassName).arg(wantUpdateStudentId);
        query.exec(sql);
        QMessageBox::information(this, "Reminder", "Congratulations! Information modified successfully!");
        qDebug()<<"Modified success";
        this->close();
        admin_student_show *newAdminStudentShow=new admin_student_show;
        newAdminStudentShow->show();
}
}
//Student ID to be changed
void admin_student_updata::on_pushButton_3_clicked()
{
       QString wantUpdateStudentId=ui->idEdit->text();
       QSqlQuery query;
       //Judge whether the input student number exists in the database
       QString sql=QString("select id from student where id='%1'").arg(wantUpdateStudentId);
       query.exec(sql);
       bool isExist=false;//Does it exist?
       while(query.next())
                  {
                      QString id=query.value(0).toString();
                      if(id.compare(wantUpdateStudentId)==0){
                          ifNotFindIdDo=false;
                          isExist=true;
                          //this->close();
                          break;
                      }
                      isExist=false;
                 }
       if(isExist){
           QSqlQuery query;
           QString sql=QString("select id,password,name,sex,age,address,phone_number,dormitory,className from student where id='%1'").arg(wantUpdateStudentId);
           query.exec(sql);
           while (query.next()) {
               QString Update_id=query.value(0).toString();
               QString Update_password=query.value(1).toString();
               QString Update_name=query.value(2).toString();
               QString Update_sex=query.value(3).toString();
               QString Update_age=query.value(4).toString();
               QString Update_address=query.value(5).toString();
               QString Update_phone_number=query.value(6).toString();
               QString Update_dormitory=query.value(7).toString();
               QString Update_className=query.value(8).toString();
               ui->updateIdEdit->setText(Update_id);
               ui->passwordEdit->setText(Update_password);
               ui->nameEdit->setText(Update_name);
               ui->sexEdit->setText(Update_sex);
               ui->ageEdit->setText(Update_age);
               ui->addressEdit->setText(Update_address);
               ui->phone_numberEdit->setText(Update_phone_number);
               ui->dormitoryEdit->setText(Update_dormitory);
               ui->classNameEdit->setText(Update_className);
           }

       }

}

4. There is no need to view the student information, because the tableview of the student information interface can view the student information.

5. Finally, find the information. You can find the student information according to the student number or name. If it does not exist, it will be prompted, and if it exists, it will be displayed. The specific implementation code is as follows:

void admin_find_show::on_findBtn_clicked()
{

    QString findId= ui->id_find->text();
    QString findName=ui->name_find->text();
    //select table1.abc from table1 inner join table2 on table1.xxx=table2.xxx;
        QSqlQuery query1;//Execute the first sql statement
        QSqlQuery query2;//Execute the second sql statement
    if(!findId.isEmpty()){
        QString sqlById1=QString("select student.id,student.name,student.sex,student.age,student.address,student.phone_number,student.dormitory,class.name,class.department,class.instructor,class.instructor_number,class.people_number from student inner join class on student.className=class.name and student.id='%1'").arg(findId);
        query1.exec(sqlById1);
        bool Notfond=true;
        bool InfoNotAll=true;
        while (query1.next()) {
            QString id=query1.value(0).toString();
            QString name=query1.value(1).toString();
            QString sex=query1.value(2).toString();
            QString age=query1.value(3).toString();
            QString address=query1.value(4).toString();
            QString phoneNumber=query1.value(5).toString();
            QString dormitory=query1.value(6).toString();
            QString className=query1.value(7).toString();
            QString department=query1.value(8).toString();
            QString instructor=query1.value(9).toString();
            QString instructorPhone=query1.value(10).toString();
        //Here is the information found
            ui->IdLineEdit->setText(id);
            ui->NameLineEdit->setText(name);
            ui->SexLineEdit->setText(sex);
            ui->AgeLineEdit->setText(age);
            ui->AddressLineEdit->setText(address);
            ui->PhoneNumberLineEdit->setText(phoneNumber);
            ui->DormitoryLineEdit->setText(dormitory);
            ui->classLineEdit->setText(className);
            ui->DepartmentLineEdit->setText(department);
            ui->InstructorLineEdit->setText(instructor);
            ui->InstructorPhoneNumberLineEdit->setText(instructorPhone);
            Notfond=false;
            InfoNotAll=false;
        }
        if(InfoNotAll){
        QString sqlById2=QString("select student.id,student.name,student.sex,student.age,student.address,student.phone_number,student.dormitory from student where student.id='%1'").arg(findId);
        query2.exec(sqlById2);
            while (query2.next()) {
                QString id=query2.value(0).toString();
                QString name=query2.value(1).toString();
                QString sex=query2.value(2).toString();
                QString age=query2.value(3).toString();
                QString address=query2.value(4).toString();
                QString phoneNumber=query2.value(5).toString();
                QString dormitory=query2.value(6).toString();
            //Here is the information found
                ui->IdLineEdit->setText(id);
                ui->NameLineEdit->setText(name);
                ui->SexLineEdit->setText(sex);
                ui->AgeLineEdit->setText(age);
                ui->AddressLineEdit->setText(address);
                ui->PhoneNumberLineEdit->setText(phoneNumber);
                ui->DormitoryLineEdit->setText(dormitory);
                ui->classLineEdit->setText("null");
                ui->DepartmentLineEdit->setText("null");
                ui->InstructorLineEdit->setText("null");
                ui->InstructorPhoneNumberLineEdit->setText("null");
                Notfond=false;
            }
        }
        if(Notfond){
            QMessageBox::information(this, "warning", "No information found");
            this->close();
            admin_find_show *adminFindShow=new admin_find_show;
            adminFindShow->show();
        }

    }
    else{

        QString sqlByName1=QString("select student.id,student.name,student.sex,student.age,student.address,student.phone_number,student.dormitory,class.name,class.department,class.instructor,class.instructor_number,class.people_number from student inner join class on student.className=class.name and student.name='%1'").arg(findName);
        query1.exec(sqlByName1);
        bool Notfond=true;
        bool InfoNotAll=true;
        while (query1.next()) {
            QString id=query1.value(0).toString();
            QString name=query1.value(1).toString();
            QString sex=query1.value(2).toString();
            QString age=query1.value(3).toString();
            QString address=query1.value(4).toString();
            QString phoneNumber=query1.value(5).toString();
            QString dormitory=query1.value(6).toString();
            QString className=query1.value(7).toString();
            QString department=query1.value(8).toString();
            QString instructor=query1.value(9).toString();
            QString instructorPhone=query1.value(10).toString();
        //Here is the information found
            ui->IdLineEdit->setText(id);
            ui->NameLineEdit->setText(name);
            ui->SexLineEdit->setText(sex);
            ui->AgeLineEdit->setText(age);
            ui->AddressLineEdit->setText(address);
            ui->PhoneNumberLineEdit->setText(phoneNumber);
            ui->DormitoryLineEdit->setText(dormitory);
            ui->classLineEdit->setText(className);
            ui->DepartmentLineEdit->setText(department);
            ui->InstructorLineEdit->setText(instructor);
            ui->InstructorPhoneNumberLineEdit->setText(instructorPhone);
            Notfond=false;
            InfoNotAll=false;
        }
        if(InfoNotAll){
            QString sqlByName2=QString("select student.id,student.name,student.sex,student.age,student.address,student.phone_number,student.dormitory from student where student.name='%1'").arg(findName);
            query2.exec(sqlByName2);
                while (query2.next()) {
                    QString id=query2.value(0).toString();
                    QString name=query2.value(1).toString();
                    QString sex=query2.value(2).toString();
                    QString age=query2.value(3).toString();
                    QString address=query2.value(4).toString();
                    QString phoneNumber=query2.value(5).toString();
                    QString dormitory=query2.value(6).toString();
                //Here is the information found
                    ui->IdLineEdit->setText(id);
                    ui->NameLineEdit->setText(name);
                    ui->SexLineEdit->setText(sex);
                    ui->AgeLineEdit->setText(age);
                    ui->AddressLineEdit->setText(address);
                    ui->PhoneNumberLineEdit->setText(phoneNumber);
                    ui->DormitoryLineEdit->setText(dormitory);
                    ui->classLineEdit->setText("null");
                    ui->DepartmentLineEdit->setText("null");
                    ui->InstructorLineEdit->setText("null");
                    ui->InstructorPhoneNumberLineEdit->setText("null");
                    Notfond=false;
                }
        }
        if(Notfond){
            QMessageBox::information(this, "warning", "No information found");
            this->close();
            admin_find_show *adminFindShow=new admin_find_show;
            adminFindShow->show();
        }
    }


}

6. In addition, inspired by the scientific calculator before, the function of copying tableview information has been added. For example, to delete a student number, click the corresponding student number, and the copied prompt will pop up. You can paste it in the delete student number box or search box. This is a way to improve the user experience. The specific implementation code is as follows:

//Select table view to get the selected content
void admin_student_show::on_tableView_clicked(const QModelIndex &index)
{
    QString tableViewValue=index.data().toString();
    QClipboard *board=QApplication::clipboard();
       board->setText(tableViewValue);
       QMessageBox::information(this, "Reminder", "information"+tableViewValue+"Copied to pasteboard");
}

Four. Conclusion

For the whole student information management system, the principle is relatively simple and easy to implement, but because Qt is connected to sqlite database without learning, so I looked up a lot of tutorials and spent a lot of time sorting out ideas. The whole program gives me the feeling of satisfying practicality.

Published 15 original articles, won praise 15, visited 1527
Private letter follow

Tags: SQL Database Qt SQLite

Posted on Sun, 12 Jan 2020 03:36:09 -0500 by Kano