天天看點

Qt 使用者登入界面的設計

登入界面沒有作嚴格的正規表達式驗證,也沒有連接配接資料庫,本以為很簡單,實際做了才發現,為了解決輸入容錯耗費了大量的時間,今天把我的代碼分享出來,供各位同仁參考讨論,未解決的問題也希望大家能幫忙解決。為了節約篇幅,省略了所有的頭檔案包含指令。

        一、LoginForm類

           頭檔案forgetpassword.h清單:

class LoginForm : public QWidget
{    Q_OBJECT
public:
    explicit LoginForm(QWidget *parent = nullptr);
     ~LoginForm();
private:
    QLabel* lbl_name;    
    QLabel* lbl_pwd;    
    QLineEdit* edt_name;
    QLineEdit* edt_pwd;
    QPushButton* btn_login;
    QPushButton* btn_cancel;
    bool  b_nameSel;//标記edt_name編輯框是否被單擊過
    bool b_pwdSel;//标記edt_pwd編輯框是否被單擊過
    QCommandLinkButton* linkBtn_forget;
    QPushButton* btn_register;
    AddUser *userReg;
    ForgetPassword *forgetpwd;
public slots:
    void on_btn_login();
    void  on_btn_cancel();
    void  on_radForget_clicked();//忘記密碼按鈕的槽函數
    void  on_radRegister_clicked(); //注冊按鈕的槽函數    
    void on_editingFinished();//編輯框輸入完成的槽函數
    void on_textEdited(const QString&);//編輯框單擊信号槽函數
signals:
    void  SendLoginVerify(QString userName,QString userPassword);//登陸驗證信号
};
#endif // LOGINFORM_H
           

      實作檔案forgetpassword.cpp清單:

LoginForm::LoginForm(QWidget *parent) :    QWidget(parent)
{
    this->setWindowTitle("使用者登入");
    //取消最大化
this->setWindowFlags(~windowFlags() & Qt::WindowMaximized & Qt::WindowStaysOnTopHint);
  //設定當單擊關閉按鈕時,釋放建立的視窗的記憶體空間
   this->setAttribute(Qt::WA_DeleteOnClose,true);    setFixedSize(400,300);

    QFont nullFont;    nullFont.setPixelSize(16);

    lbl_name=new QLabel("使用者名:");
    lbl_name->setGeometry(30,30,60,30);    lbl_name->setFont(nullFont);

    lbl_pwd=new QLabel("密碼:");
    lbl_pwd->setGeometry(30,60,60,30);    lbl_pwd->setFont(nullFont);

    edt_name=new QLineEdit("");
    edt_name->setObjectName("edt_name");//設定編輯框對象的name屬性
    edt_name->setGeometry(100,30,120,30);    edt_name->setFont(nullFont);

    edt_pwd=new QLineEdit("");
    edt_pwd->setObjectName("edt_pwd");
    edt_pwd->setGeometry(100,60,120,30);    edt_pwd->setFont(nullFont);
    edt_pwd->setEchoMode(QLineEdit::Password);//輸入的密碼以圓點顯示

    linkBtn_forget=new QCommandLinkButton("忘記密碼");
    linkBtn_forget->setGeometry(10,10,40,30);    linkBtn_forget->setFont(nullFont);
    btn_register=new QPushButton("注冊");
    btn_register->setGeometry(200,100,100,30);    btn_register->setFont(nullFont);
    btn_login=new QPushButton("登陸");
    btn_login->setGeometry(200,200,100,30);    btn_login->setFont(nullFont);
    btn_cancel=new QPushButton("關閉");
    btn_cancel->setGeometry(200,300,100,30);    btn_cancel->setFont(nullFont);

    userReg=nullptr;    forgetpwd=nullptr;

    //初始化編輯框是否被選擇
    b_nameSel=false;    b_pwdSel=false;

    setMouseTracking(true);//設定滑鼠追蹤,便于在editingFinished信号槽函數中擷取滑鼠位置

    //表格布局
    QGridLayout* layout=new QGridLayout(this);
    layout->setSpacing(6);
    layout->setMargin(20);
    //布局設定
    layout->addWidget(lbl_name,0,0);
    layout->addWidget(edt_name,0,1);
    layout->addWidget(lbl_pwd,1,0);

    layout->addWidget(edt_pwd,1,1);
    layout->addWidget(linkBtn_forget,1,2);
   
    layout->addWidget(btn_register,2,0);    
    layout->addWidget(btn_login,2,1);   
    layout->addWidget(btn_cancel,2,2);

    connect(btn_login,SIGNAL(clicked()),this,SLOT(on_btn_login()));
    connect(btn_cancel,SIGNAL(clicked()),this,SLOT(on_btn_cancel()));
    connect(linkBtn_forget,SIGNAL(clicked()),this,SLOT(on_radForget_clicked()));
    connect(btn_register,SIGNAL(clicked()),this,SLOT(on_radRegister_clicked()));

    //編輯框輸入完成,輸入框資料合法性驗證
    connect(edt_name,SIGNAL(textEdited(const QString&)),this,SLOT(on_textEdited(const QString&)));
    connect(edt_pwd,SIGNAL(textEdited(const QString&)),this,SLOT(on_textEdited(const QString&)));
    connect(edt_name,SIGNAL(editingFinished()),this,SLOT(on_editingFinished()));
    connect(edt_pwd,SIGNAL(editingFinished()),this,SLOT(on_editingFinished()));
}
           
void LoginForm::on_textEdited(const QString& text)
{
    QLineEdit* obj=qobject_cast<QLineEdit*>(sender());
    qDebug()<<"對象name屬性:"<<obj->objectName();
    if(obj->objectName()=="edt_name")
    {        b_nameSel=true;        b_pwdSel=false;    }
    else if(obj->objectName()=="edt_pwd")
    {        b_nameSel=false;        b_pwdSel=true;    }
}
           
void LoginForm::on_radForget_clicked()
{    forgetpwd=new ForgetPassword;
    forgetpwd->show();
    QString pwd=forgetpwd->getPassword();
    edt_pwd->setText(pwd);
    update();
}

void LoginForm::on_radRegister_clicked()
{    userReg=new AddUser;    userReg->show();}
           
void LoginForm::on_btn_login()
{
    QString name=edt_name->text();
    QString pwd=edt_pwd->text();
    if(name.isEmpty())
    {        QMessageBox msg(this);        msg.setWindowTitle("輸入錯誤");
            msg.setText("使用者名為空!");        msg.exec();        return;
    }
    if(pwd.isEmpty())
    {        QMessageBox msg(this);        msg.setWindowTitle("輸入錯誤");
             msg.setText("密碼為空!");        msg.exec();        return;
    }
    if(name.isEmpty()&&pwd.isEmpty())
    {        QMessageBox msg(this);        msg.setWindowTitle("輸入錯誤");
             msg.setText("使用者名和密碼為空!");        msg.exec();        return;
    }
    emit SendLoginVerify(name,pwd);
    qDebug()<<"denglu";
    close();
}
           
void LoginForm::on_editingFinished()
{
    QLineEdit* obj=qobject_cast<QLineEdit*>(sender());
    qDebug()<<"對象name屬性:"<<obj->objectName();
    qDebug()<<this->mapFromGlobal(QCursor::pos()).x()<<this->mapFromGlobal(QCursor::pos()).y();
    qDebug()<<this->btn_cancel->geometry();
    qDebug()<<this->btn_cancel->geometry().contains(this->mapFromGlobal(QCursor::pos()));
    //判斷滑鼠位置是否在取消按鈕上,如果在就不校驗編輯框的資料
    if(this->btn_cancel->geometry().contains(this->mapFromGlobal(QCursor::pos())))
    {        return;    }
    //判斷滑鼠位置是否在忘記密碼單選鈕上,如果在就不校驗編輯框的資料
    if(this->linkBtn_forget->geometry().contains(this->mapFromGlobal(QCursor::pos())))
    {        return;    }
    //判斷滑鼠位置是否在注冊按鈕上,如果在就不校驗編輯框的資料
    if(this->btn_register->geometry().contains(this->mapFromGlobal(QCursor::pos())))
    {        return;    }

    if(b_nameSel && obj->objectName()=="edt_name")
    {
        QString name=obj->text();
        if(name.size() <3|| name.size()>8)
        {
            qDebug()<<name;
            QMessageBox msg(this);
            msg.setWindowTitle("輸入錯誤");
            msg.setText("使用者名不合法");
            msg.exec();
        }
    }
    else if(b_pwdSel && obj->objectName()=="edt_pwd")
    {
        QString pwd=obj->text();
        if((pwd.size()<6 || pwd.size()>8))
        {
            qDebug()<<pwd;
            QMessageBox msg(this);
            msg.setWindowTitle("輸入錯誤");
            msg.setText("密碼輸入不合法");
            msg.exec();
        }
    }
}
           
void LoginForm::on_btn_cancel()
{    close();}

LoginForm::~LoginForm()
{
    if(lbl_pwd!=nullptr)//1.
    {        delete lbl_pwd;        lbl_pwd=nullptr;    }
    if(lbl_name!=nullptr)//2.
    {        delete lbl_name;        lbl_name=nullptr;    }
    if(edt_pwd!=nullptr)//3.
    {        delete edt_pwd;        edt_pwd=nullptr;    }
    if(edt_name!=nullptr)//4.
    {        delete edt_name;        edt_name=nullptr;    }
    if(btn_login!=nullptr)//5.
    {        delete btn_login;        btn_login=nullptr;    }
    if(btn_cancel!=nullptr)//6.
    {        delete btn_cancel;        btn_cancel=nullptr;    }
    if(linkBtn_forget!=nullptr)//7.
    {        delete linkBtn_forget;        linkBtn_forget=nullptr;    }
    if(forgetpwd!=nullptr)
    {       delete forgetpwd;        forgetpwd=nullptr;    }
    if(btn_register!=nullptr)//8.
    {        delete btn_register;        btn_register=nullptr;    }
    if(userReg!=nullptr)//9.
    {        delete userReg;        userReg=nullptr;    }
}
           

由于兼顧了注冊和忘記密碼,是以又多了兩個類忘記密碼和注冊添加使用者

二、ForgetPassword類

忘記密碼使用了UI,界面:

Qt 使用者登入界面的設計

頭檔案清單 

#ifndef FORGETPASSWORD_H
#define FORGETPASSWORD_H
namespace Ui
 {
class ForgetPassword;
}

class ForgetPassword : public QWidget
{
    Q_OBJECT
public:
    explicit ForgetPassword(QWidget *parent = nullptr);
    ~ForgetPassword();
    QString getPassword();
private:
    QString password;
    bool b_name;
private slots:
    void on_btn_close_clicked();
    void on_btn_find_clicked();
    void on_editingFinished();
    void on_textEdited(const QString&);//編輯框單擊信号槽函數
signals:
    void  SendFindPassword(QString userName);//登陸驗證信号
private:   
      Ui::ForgetPassword *ui;
};
#endif // FORGETPASSWORD_H
           
忘記密碼實作代碼清單:
ForgetPassword::ForgetPassword(QWidget *parent) :    QWidget(parent), ui(new Ui::ForgetPassword)
{   ui->setupUi(this);
    this->setWindowTitle("忘記密碼");
    this->setWindowFlags(~windowFlags() & Qt::WindowMaximized & Qt::WindowStaysOnTopHint);//取消最大化
//設定當單擊關閉按鈕時,釋放建立的視窗的記憶體空間
     this->setAttribute(Qt::WA_DeleteOnClose,true);
    ui->lbl_password->setText("");
    connect(ui->edt_user,SIGNAL(editingFinished()),this,SLOT(on_editingFinished()));
    b_name=false;
    connect(ui->edt_user,SIGNAL(textEdited(const   QString&)),this,SLOT(on_textEdited(const QString&)));
}
           
void ForgetPassword::on_textEdited(const QString& text)
{
    QLineEdit* obj=qobject_cast<QLineEdit*>(sender());
    qDebug()<<"對象name屬性:"<<obj->objectName();
    if(obj->objectName()=="edit_name")
    {        b_name=true;    }
}
void ForgetPassword::on_editingFinished()
{
    QLineEdit* obj=qobject_cast<QLineEdit*>(sender());
    qDebug()<<"obj"<<obj->objectName();
    //判斷滑鼠位置是否在取消按鈕上,如果在就不校驗編輯框的資料
    if(b_name && (obj->objectName()=="edt_user"))
    {
        QString name=obj->text();
        if(name.isEmpty())
        {     
            qDebug()<<"戶用名為空或不合法";
            QMessageBox msg(this);
            msg.setWindowTitle("輸入錯誤");
            msg.setText("戶用名為空,請重新輸入");
            msg.exec();
            ui->edt_user->setFocus(); 
       }  
  }
}       
           
void ForgetPassword::on_btn_close_clicked()
{    close();}

QString ForgetPassword::getPassword()
{    return password;}

void ForgetPassword::on_btn_find_clicked()
{
   QString user=ui->edt_user->text();
   if(ui->edt_user->text().isEmpty())
   {
       QMessageBox msg(this);
       msg.setWindowTitle("輸入錯誤");
       msg.setText("戶用名為空,請重新輸入");
       msg.exec();
       ui->edt_user->setFocus();
   }
   emit SendFindPassword(user);
   password="123";//需要與伺服器通信,得到伺服器回報的結果,替換“123”
   qDebug()<<"on_btn_find_clicked.";
   ui->lbl_password->setText(password); 
}

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

三、AddUser類

添加使用者也用到ui,界面如下:

Qt 使用者登入界面的設計

添加使用者頭檔案AddUser.h代碼清單: 

namespace Ui {
class AddUser;
}
class AddUser : public QWidget
{
    Q_OBJECT
public:
    explicit AddUser(QWidget *parent = nullptr);
    ~AddUser();
private:
    bool b_nameSel;//标記edt_name編輯框是否被單擊過
    bool b_pwdSel;//标記edt_pwd編輯框是否被單擊過
    bool b_ensurePwd;
signals:
    void SendInsertUserInfo(QString userName,QString userPassword);public slots:
    void on_btn_ok();
    void on_btn_cancel();
    void on_editingFinished();
    void on_textEdited(const QString&);//編輯框單擊信号槽函數
private:
    Ui::AddUser *ui;
};
           

添加使用者實作檔案AddUser.cpp代碼清單:

AddUser::AddUser(QWidget *parent) :    QWidget(parent),    ui(new Ui::AddUser)
{
    ui->setupUi(this);
    this->setWindowTitle("使用者添加");
    this->setWindowFlags(~windowFlags() & Qt::WindowMaximized & Qt::WindowStaysOnTopHint);
    //設定當單擊關閉按鈕時,釋放建立的視窗的記憶體空間
   this->setAttribute(Qt::WA_DeleteOnClose,true);
   //初始化編輯框是否被選擇
    b_nameSel=false;    b_pwdSel=false;    b_ensurePwd=false;
     //設定滑鼠追蹤,便于在editingFinished信号槽函數中擷取滑鼠位置
    setMouseTracking(true); 

ui->edit_pwd->setEchoMode(QLineEdit::Password);//輸入的密碼以圓點顯示
    ui->edit_surePwd->setEchoMode(QLineEdit::Password);//輸入的密碼以圓點顯示

    connect(ui-edit_name,SIGNAL(editingFinished()),this,SLOT(on_editingFinished()));    connect(ui->edit_pwd,SIGNAL(editingFinished()),this,SLOT(on_editingFinished()));    connect(ui->edit_surePwd,SIGNAL(editingFinished()),this,SLOT(on_editingFinished()));
    connect(ui->edit_name,SIGNAL(textEdited(const QString&)),this,SLOT(on_textEdited(const QString&)));
    connect(ui->edit_pwd,SIGNAL(textEdited(const QString&)),this,SLOT(on_textEdited(const QString&)));
    connect(ui->edit_surePwd,SIGNAL(textEdited(const QString&)),this,SLOT(on_textEdited(const QString&)));
    //ui->edit_name->setFocus();
    connect(ui->btn_ok,SIGNAL(clicked()),this,SLOT(on_btn_ok()));
    connect(ui->btn_cancel,SIGNAL(clicked()),this,SLOT(on_btn_cancel()));
}
           
void AddUser::on_textEdited(const QString& text)
{
    QLineEdit* obj=qobject_cast<QLineEdit*>(sender());
    qDebug()<<"對象name屬性:"<<obj->objectName();
    if(obj->objectName()=="edit_name")
    {        b_nameSel=true;        b_pwdSel=false;        b_ensurePwd=false;  }
    else if(obj->objectName()=="edit_pwd")
    {        b_nameSel=false;        b_pwdSel=true;        b_ensurePwd=false;    }
    else if(obj->objectName()=="edit_surePwd")
    {        b_nameSel=false;        b_pwdSel=false;        b_ensurePwd=true;    }
}
           
void AddUser::on_editingFinished()
{
    QLineEdit* obj=qobject_cast<QLineEdit*>(sender());
    qDebug()<<"obj"<<obj->objectName()<<obj->hasFocus();
    qDebug()<<this->mapFromGlobal(QCursor::pos()).x()<<this->mapFromGlobal(QCursor::pos()).y();
    qDebug()<<this->ui->btn_cancel->geometry();
    qDebug()<<this->ui->btn_cancel->geometry().contains(this->mapFromGlobal(QCursor::pos()));
    //判斷滑鼠位置是否在取消按鈕上,如果在就不校驗編輯框的資料
    if(this->ui->btn_cancel->geometry().contains(this->mapFromGlobal(QCursor::pos())))
    {        return;    }
    if(b_nameSel && obj->objectName()=="edit_name")
    {
        QString name=obj->text();
        int nameSize= name.size();
        if(nameSize<3 || nameSize>8)
         {
            qDebug()<<"戶用名為空或不合法";
            QMessageBox msg(this);
            msg.setWindowTitle("輸入錯誤");
            msg.setText("使用者名不合法");
            msg.exec();
            ui->edit_name->setFocus();
            //return;
         }
    }

    if(b_pwdSel && obj->objectName()=="edit_pwd" )
    {
        QString pwd=obj->text();
        int pwdSize= pwd.size();
        if(pwdSize<6||pwdSize>8)
        {
            qDebug()<<"密碼為空或不合法";
            QMessageBox msg(this);
            msg.setWindowTitle("輸入錯誤");
            msg.setText("密碼為空或不合法");
            msg.exec();
            ui->edit_pwd->setFocus();
            // return;
        }
    }
if(b_ensurePwd && obj->objectName()=="edit_surePwd")
    {
        QString ensurepwd=obj->text();
        int ensurepwdSize= ensurepwd.size();
        if(ensurepwdSize<6||ensurepwdSize>8)
        {
            QMessageBox msg(this);
            msg.setWindowTitle("輸入錯誤");
            msg.setText("确認密碼為空或不合法");
            msg.exec();
            ui->edit_surePwd->setFocus();
            //return;
        }
        QString pwd=ui->edit_pwd->text();
        if(pwd!=ensurepwd)
        {
            QMessageBox msg(this);
            msg.setWindowTitle("輸入錯誤");
            msg.setText("密碼與确認密碼不一緻!");
            msg.exec();
            //ui->edit_surePwd->setFocus();
              return;
        }
    }
}
           
void AddUser::on_btn_ok()
{
    QString name=ui->edit_name->text();
    QString pwd=ui->edit_pwd->text();
    QString ensurePwd=ui->edit_surePwd->text();

    if(name.isEmpty())
    {   QMessageBox msg(this);
        msg.setWindowTitle("輸入錯誤");
        msg.setText("使用者名為空");
        msg.exec();
        ui->edit_name->setFocus();
        return;
    }

    if(name.size()<3||name.size()>6)
    {
        QMessageBox msg(this);
        msg.setWindowTitle("輸入錯誤");
        msg.setText("使用者名不合法");
        msg.exec();
        ui->edit_name->setFocus();
        return;
    }

   if(pwd.isEmpty())
    {
        QMessageBox msg(this);
        msg.setWindowTitle("輸入錯誤");
        msg.setText("密碼為空");
        msg.exec();
        ui->edit_pwd->setFocus();
        return;
    }
    if(pwd.size()<6||pwd.size()>8)
    {
        qDebug()<<"密碼為空或不合法";
        QMessageBox msg(this);
        msg.setWindowTitle("輸入錯誤");
        msg.setText("密碼為空或不合法");
        msg.exec();
        ui->edit_pwd->setFocus();
          return;
     }
    if(ensurePwd.isEmpty())
    {
        QMessageBox msg(this);
        msg.setWindowTitle("輸入錯誤");
        msg.setText("确認密碼為空");
        msg.exec();
        ui->edit_surePwd->setFocus();
        return;
    }
  if(ensurePwd.size()<6||ensurePwd.size()>8)
    {
        QMessageBox msg(this);
        msg.setWindowTitle("輸入錯誤");
        msg.setText("确認密碼為空或不合法");
        msg.exec();
        ui->edit_surePwd->setFocus();
          return;
    }
    //使用者權限需要處理嗎?
    //向伺服器發送修改的消息
    emit SendInsertUserInfo(name,pwd);
    qDebug()<<name<<pwd;
    close();
}

void AddUser::on_btn_cancel()
{    close();}

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

 四、主函數

主函數測試代碼如下:

int main(int argc, char *argv[])
{    
   QApplication a(argc, argv);
   //    -----------1.登陸測試
   LoginForm *login=new LoginForm; 
   login->show();  
   return a.exec();
}
           

        最後還有一個容錯沒有處理,那就是當使用者輸入框輸入了部分資訊後,單擊右上角的關閉按鈕,還會彈出編輯驗證的消息框,實在解決不了了,希望大家幫幫忙。

繼續閱讀