Qt uses MVC and database singleton mode to realize login function

1, Briefly describe MVC and singleton mode

1. MVC design pattern

  • Function: effectively separate data and user interface.
  • Composition: model layer (representing data), view layer (representing user interface), control layer controller (defining user operations on the interface).

2. MVC frame diagram

3. Use MVC architecture to realize the process of user login

  1. The login interface sends a request to the controller
  2. The controller entrusts the user model to query the user data
  3. The results obtained by querying the user table of the user model
  4. The user model returns the query result to the controller
  5. The controller feeds back the results returned by the user model to the user login interface

There is only one controller and many models, but there is only one user model, which is used for login and registration. At this time, you need to create a user model at the beginning, rather than create it again when you use it. So how can you ensure that it is created only once? Therefore, the singleton mode is involved. (in addition, the singleton mode can also be used in the controller and view interfaces. The controller can be opened only once and can be used directly when it needs to be used. The view uses the singleton mode for window management)

4. Singleton mode

4.1 concept: in an application, a class has only one instance and provides a global access point to access it

4.2 types of singleton mode:

Lazy mode: does not initialize when the class is loaded
Hungry man mode: the initialization is completed when the class is loaded, so the class loading is relatively slow, but the speed of obtaining objects is fast.

4.3 design single case mode:

  • Privatize constructors. If it is not privatized, you can create objects outside the function at will. Implementations can only create objects themselves
  • Define a private static class object pointer. (static: no need to create objects. Direct access outside the class)
  • Define a public access interface to obtain class objects, and the interface function should also be static. Because you can only use static functions to access static objects

4.4 single example code:

 

II. Implementation of login

1. View

class loginWin : public QWidget
{
    Q_OBJECT
public:
//...... Declaration control, etc
public slots:
    void GotoLogin();
private:

    user * uData;
    controller * control;


};

loginWin::loginWin(QWidget *parent) : QWidget(parent)
{

    uData=user::getInstance();
    //Initialize control layer
    this->control=controller::getInstance();
    this->setWindowTitle("Sign in");
    this->setPalette(QPalette(Qt::white));
    //...... Create controls, layouts, etc
    //Slot function
    connect(loginBtn,SIGNAL(clicked(bool)),this,SLOT(GotoLogin()));//Sign in

}


void loginWin::GotoLogin()
{
    QString userID = nameEdit->text();
    QString pwd = pwdEdit->text();
    QString verify_edt = verifyEdit->text();
    QString verify_lab = m_Verify->getVerificationCode();

    int res=control->controlLogin(userID,pwd);
    if(res==8)
    {
        QMessageBox msgBox;
        msgBox.setText("Wrong account or password");
        msgBox.exec();
    }

    else if(res=user::SUCCESS && userID!=NULL && verify_edt==verify_lab)
    {
        this->loginState=1;
        QMessageBox::information(this,"Tips","Login successful");
        qDebug()<<"Login successful";
        loginWin::word_list<<userID;
        indexWinget *indexWin = new indexWinget();
        indexWin->show();
        this->hide();
        return ;

    }
}

2. Controller (login controller)

class controller
{
public:
    static controller * getInstance();
private:
    controller();
    ~controller();
    static controller * control;

public:
    int controlLogin(QString userID,QString userPwd);
    loginmodel    * log;

};

//Controller cpp file:
controller * controller::control=NULL;

controller::controller()
{
    log=loginmodel::getInstance();
}


controller * controller::getInstance()
{
    if(controller::control==NULL)
    {
        controller::control=new controller();
    }
    return controller::control;
}
//Verify login
int controller::controlLogin(QString userID, QString userPwd)
{
    int res;
    res=log->modelLogVerify(userID,userPwd);//Hand it over to the model
    return res;
}

3. Model

class user
{
public:
    //...... Omit custom enumeration types
    static user * getInstance();
    //Get user information
    QList<user> getUserData();
    //Set user information
    void setUserMesg(QString userID,QString userName,QString userPwd,QString isLocked,QString isVIP,QString userType,QString registTime);
    void setUserMesg(QString userID, QString userName, QString userPwd);

    QString getUserID() const;
    void setUserID(const QString value);
    QString getUserName() const;
    void setUserName(const QString &value);
    QString getUserPwd() const;
    void setUserPwd(const QString &value);

private:
    user();
    static user * uData;
    QString userID;
    QString userName;
    QString userPwd;
    mysqlite * u_data;


};

//cpp file code
QList<user> user::getUserData()
{

    QList<user> user_list;   //A linked list that temporarily saves user information
    user tp_user;       //Class that temporarily saves user information
    char ** resID;   //Query the result set returned by user account
    char ** resPwd;     //password
    char ** resName;    //full name

    int index=1;        //Returns the array subscript of the result set
    int row=0,column=0; //Returns the rows and columns of the result set

    //SQL statement
    QString selectUserID=QString("select userID from user");//account number
    QString selectUserName=QString("select userName from user");      //full name
    QString selectUserPwd=QString("select userPwd from user");    //password

    //The obtained data is stored in the result set
    u_data->getData(selectUserID.toStdString().c_str(),resID,row,column);
    u_data->getData(selectUserName.toStdString().c_str(),resName,row,column);
    u_data->getData(selectUserPwd.toStdString().c_str(),resPwd,row,column);
    for (int i = 0; i < row; ++i)
    {

        for (int j = 0; j < column; ++j)
        {
            qDebug()<<"Convert result set to QString type";
            //Convert result set to QString type
            QString strID(resID[index]);
            QString strName(resName[index]);
            QString strPwd(resPwd[index]);
            QString strLocked(resLocked[index]);
            QString strVIP(resVIP[index]);
            QString strType(resType[index]);
            QString strTime(resTime[index]);
            //Store the result set in the user class and insert it into the linked list
            tp_user.setUserMesg(strID,strName,strPwd,strLocked,strVIP,strType,strTime);
            user_list.append(tp_user);
            qDebug()<<"getUserData SUCCESS";
            //
            ++index;
        }
    }
    return user_list;
}



void user::setUserMesg(QString userID, QString userName, QString userPwd)
{
    QString insertUser;

    insertUser=QString(
                "insert into user(userID,userName,userPwd) values('%1','%2','%3')"
                ).arg(userID,userName,userPwd);

    u_data->dbExe(insertUser.toUtf8().data());
    qDebug()<<"setUserMesg SUCCESS";
}

user::user()
{

    u_data=mysqlite::getMyDatabase();
}

user *user::getInstance()
{
    if(user::uData==NULL)
    {
        user::uData=new user();
    }
    return user::uData;
}

4. Model (login model)

class loginmodel
{

public:
    static loginmodel * getInstance();
    int modelLogVerify(QString userID,QString userPwd);
private:
    loginmodel();
    static loginmodel * login;
    user * uData;
};

//cpp file code is as follows
loginmodel * loginmodel::login=NULL;
loginmodel::loginmodel()
{
    uData=user::getInstance();
}

loginmodel * loginmodel::getInstance()
{
    if(loginmodel::login==NULL)
    {
        loginmodel::login=new loginmodel();
    }
    return loginmodel::login;
}

int loginmodel::modelLogVerify(QString userID, QString userPwd)
{
    int res=-1;               //Query result - 1: the account or password is incorrect 1: the verification is successful
    int i;
    QList<user> tp_user;    //Temporarily save user information
    tp_user=uData->getUserData();//Get user information from the data layer
    //Verify that the user and password are correct
    for(i=0;i<tp_user.count();i++)
    {
        if(tp_user[i].getUserID()==userID&&tp_user[i].getUserPwd()==userPwd)
        {
            res=1;
            break;
        }
    }
    if(userID==NULL)
    {
        return user::ACONTNULL;
    }
    else if(userPwd==NULL)
    {
        return user::PWDNULL;
    }
    else if(res==-1)
    {
        return user::ERROR;
    }
    else
    {
       return user::SUCCESS;
    }
}

5. Database single example (refer to the link at the end of the text)

3, Realization effect

 

 

Keywords: C++ Database Qt mvc

Added by rocket on Thu, 27 Jan 2022 12:59:03 +0200