Qt之登录界面(splash)

发布于:2025-02-10 ⋅ 阅读:(46) ⋅ 点赞:(0)

在上一篇多文档窗口设计(MDI)的基础上增加了一个登录界面(splash).

该模块可以扩展为常规的软件登录界面。

界面展示如下

如果用户名和密码输入正确,则调到MDI界面,如果用户名和密码一共输入三次以上,则程序强制退出。

保存用户名的作用是,将本次输入正确的用户名和密码保存到注册表(注意注册表里不能直接放铭文密码),下次启动该软件会自动填写用户名。

注册表中保存的数据如下,密码是经过MD5加密过后的。

界面设置如下

编辑框密码隐形显示需要设置

程序结果如下:

主程序main

#include "mainwindow.h"

#include <QApplication>
#include"tlogindialog.h"
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    TLoginDialog *dlgLogin=new TLoginDialog();
    dlgLogin->resize(640,480);
    if(dlgLogin->exec()==QDialog::Accepted)
    {
        MainWindow w;
        w.show();
        return a.exec();
    }
    else
        return 0;

}

tlogindialog.h和tlogindialog.cpp如下

#ifndef TLOGINDIALOG_H
#define TLOGINDIALOG_H

#include <QDialog>

namespace Ui {
class TLoginDialog;
}

class TLoginDialog : public QDialog
{
    Q_OBJECT

public:
    explicit TLoginDialog(QWidget *parent = nullptr);
    ~TLoginDialog();

private:
    Ui::TLoginDialog *ui;
    bool m_moving=false;
    QPoint m_lastPos;
    QString m_user="user";
    QString m_pwd="12345";
    int m_tryCount=0;
    const int M_MAXCOUT=3;
    void readSettings();
    void writeSettings();
    QString encrypt(const QString &str);
    // QWidget interface
protected:
    virtual void mousePressEvent(QMouseEvent *event) override;
    virtual void mouseReleaseEvent(QMouseEvent *event) override;
    virtual void mouseMoveEvent(QMouseEvent *event) override;
private slots:
    void on_btnOk_clicked();
};


#endif // TLOGINDIALOG_H
/************************************************.cpp***************************/
#include "tlogindialog.h"
#include "ui_tlogindialog.h"
#include<QMouseEvent>
#include<QCryptographicHash>
#include<QSettings>
#include<QMessageBox>
TLoginDialog::TLoginDialog(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::TLoginDialog)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_DeleteOnClose);
    setWindowFlag(Qt::SplashScreen);
    QApplication::setOrganizationName("Sun Company");
    QApplication::setApplicationName("MDI_SPLASH");
    readSettings();

}

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

void TLoginDialog::readSettings()
{
    QSettings settings;
    bool saved=settings.value("saved",false).toBool();
    m_user=settings.value("UserName","user").toString();
    QString defaultPSWD=encrypt("12345");
    m_pwd=settings.value("PSWD",defaultPSWD).toString();
    if(saved)
    {
        ui->EditUser->setText(m_user);
    }
    ui->checkBox->setChecked(saved);
}

void TLoginDialog::writeSettings()
{
    QSettings settings;
    settings.setValue("UserName",m_user);
    settings.setValue("PSWD",m_pwd);
    settings.setValue("saved",ui->checkBox->isChecked());

}

QString TLoginDialog::encrypt(const QString &str)
{
    QByteArray btArray=str.toLocal8Bit();
    QCryptographicHash hash(QCryptographicHash::Md5);
    hash.addData(btArray);
    QByteArray resultArray=hash.result();
    QString md5=resultArray.toHex();
    return md5;
}
void TLoginDialog::mousePressEvent(QMouseEvent *event)
{
    if(event->button()==Qt::LeftButton)
    {
        m_moving=true;
        m_lastPos=event->globalPosition().toPoint()-this->pos();
    }
    return QDialog::mousePressEvent(event);
}

void TLoginDialog::mouseReleaseEvent(QMouseEvent *event)
{
    m_moving=false;
    event->accept();
}

void TLoginDialog::mouseMoveEvent(QMouseEvent *event)
{
    QPoint eventPos=event->globalPosition().toPoint();
    if(m_moving&&(event->buttons()&Qt::LeftButton)
        && (eventPos-m_lastPos-pos()).manhattanLength()>QApplication::startDragDistance())
    {
        move(eventPos-m_lastPos);
        m_lastPos=eventPos-this->pos();
    }
    return QDialog::mouseMoveEvent(event);
}



void TLoginDialog::on_btnOk_clicked()
{
    QString user=ui->EditUser->text().trimmed();
    QString pwd=ui->EditPwd->text().trimmed();
    QString encrptPSWD=encrypt(pwd);
    if((m_user==user)&&(encrptPSWD==m_pwd))
    {
        writeSettings();
        this->accept();
    }
    else
    {
        m_tryCount++;
        if(m_tryCount>M_MAXCOUT)
        {
            QMessageBox::critical(this,"错误","输入错误次数太多,强行退出");
            this->reject();
        }
        else
            QMessageBox::warning(this,"错误提示","用户名或者密码错误");
    }
}