Qt学习-明文密文的切换+验证码随机生成

目录

一:结果展示

二:代码实现

登录窗口类定义如下

具体方法实现如下:

构造:

界面窗口设置

界面控件布局

按钮-信号槽

本节核心-验证码干扰函数

本节核心-生成验证码方法实现

本节核心-重写鼠标点击事件-每次点击一次就生成新的随机验证码

本节核心-明文密文显示函数


一:结果展示

验证码随机生成

每点击一次验证码图标,就可以刷新出一个新的验证码

 

输入密码,最初是密文显示,但是通过密码编辑框旁边的按钮控件可进行明文密文的切换

最初密文

切换明文

 

二:代码实现

以登录窗口的展示为例

登录窗口类定义如下

#ifndef LOGINWIDGET_H
#define LOGINWIDGET_H

#include "setcontrol.h"
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include <QLineEdit>
#include <QTime>
#include <QDialog>
#include <QPainter>
#include <QIcon>
#include <QMessageBox>
#include<QDebug>
#include <QPaintEvent>
#include <QCryptographicHash>
#include "usercontrol.h"
#include "registerwidget.h"

class loginWidget : public QWidget
{
    Q_OBJECT
public:
    explicit loginWidget(QWidget *parent = 0);
    void paintEvent(QPaintEvent *event);//验证码干扰元素函数
    void init_UI();//界面窗口
    void init_control();//控件布局
    void init_connect();//按钮--信号槽
    QString getcode();//获取随机验证码
    void mousePressEvent(QMouseEvent *event);//鼠标点击事件--重写鼠标点击事件,没点击一次就随机生成新的验证码
    ~loginWidget();//析构
    int mark;//标志位-明文密文
signals:

public slots:
    void login();//点击登录按钮时候执行的槽函数
    void toreg();//跳转到注册
    void backlog();//返回到登录
    void PlainAndCipher();//明文密文显示函数--明文密文切换
    QString GetMd5(const QString &value);//加密

private:
    QLabel *user_label,*password_label,*code_label;//标签
    QPushButton *login_btn,*qsc_btn,*register_btn,*eye_btn;//按钮
    QLineEdit *user_edit,*password_edit,*Vericode_edit;//编辑框
    QString code;//验证码
    RegisterWidget *regWin;//注册窗
};

#endif // LOGINWIDGET_H

具体方法实现如下:

构造:

//构造
loginWidget::loginWidget(QWidget *parent) : QWidget(parent)
{
    this->mark=0;//标志位-明文密文
    init_UI();//窗口界面
    init_control();//控件布局
    init_connect();//按钮-信号槽
    this->code = this->getcode();//获取随机验证码
}

界面窗口设置

//界面窗口设置
void loginWidget::init_UI()
{
    this->setWindowTitle("LoginWin");
    this->setFixedSize(500,400);
    this->regWin = new RegisterWidget();
}

界面控件布局

//界面控件设置
void loginWidget::init_control()
{
    this->user_label = new QLabel("用户名:",this);
    this->user_label->setGeometry(130,60,80,30);

    this->password_label = new QLabel("密码:",this);
    this->password_label->setGeometry(145,110,80,30);

    this->code_label = new QLabel("验证码:",this);
    this->code_label->setGeometry(130,160,80,30);

    this->user_edit = new QLineEdit(this);
    this->user_edit->setGeometry(190,60,150,30);
    this->user_edit->setPlaceholderText(tr("请输入用户名!"));
    this->user_edit->setMaxLength(12);
    this->user_edit->installEventFilter(this);

    this->password_edit = new QLineEdit(this);
    this->password_edit->setGeometry(190,110,150,30);
    this->password_edit->setPlaceholderText(tr("请输入密码!"));
    this->password_edit->setMaxLength(8);
    this->password_edit->setEchoMode(QLineEdit::Password);

    this->eye_btn = new QPushButton("..",this);
    this->eye_btn->setGeometry(350,110,30,30);

    this->Vericode_edit = new QLineEdit(this);
    this->Vericode_edit->setGeometry(190,160,80,30);
    this->Vericode_edit->setPlaceholderText(tr("验证码"));

    this->login_btn = new QPushButton("登录",this);
    this->login_btn->setGeometry(100,240,80,30);

    this->register_btn = new QPushButton("注册",this);
    this->register_btn->setGeometry(200,240,80,30);

    this->qsc_btn = new QPushButton("退出",this);
    this->qsc_btn->setGeometry(300,240,80,30);
}

按钮-信号槽

//按钮--信号槽连接
void loginWidget::init_connect()
{
    //到注册界面
    connect(this->register_btn,SIGNAL(clicked()),this,SLOT(toreg()));
    //登录验证方法实现
    connect(this->login_btn,SIGNAL(clicked()),this,SLOT(login()));
    //返回到登录界面
    connect(this->regWin,SIGNAL(back()),this,SLOT(backlog()));
    //明密文切换实现
    connect(this->eye_btn,SIGNAL(clicked()),this,SLOT(PlainAndCipher()));
}

本节核心-验证码干扰函数

//验证码干扰元素函数
void loginWidget::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPen pen;
    painter.drawPixmap(this->rect(),QPixmap(":image/pt.jpg"));

    //画点
    for(int i = 0;i < 100;++i)
    {
        pen = QPen(QColor(qrand()%256,qrand()%256,qrand()%256));
        painter.setPen(pen);
        painter.drawPoint(300+qrand()%150,160+qrand()%50);
    }

    //画线
    for(int i = 0;i < 10;++i)
    {

        painter.drawLine(300+qrand()%150,160+qrand()%50,
                         300+qrand()%150,160+qrand()%50);
    }
    pen = QPen(QColor(255,0,0,100));
    QFont font("楷体",25,QFont::Bold,true);
    painter.setFont(font);
    painter.setPen(pen);
    //绘画字
    for(int i = 0;i < 4;++i)
    {
        painter.drawText(300+30*i,160,30,40,Qt::AlignCenter,QString(code[i]));
    }
}

本节核心-生成验证码方法实现

//生成验证码方法实现
QString loginWidget::getcode()
{
    update();
    //code为QString类型变量
    QString getcode;
    //随机数字
    for(int i = 0;i < 4;++i)
    {
        int num = qrand()%3;
        if(num == 0)
        {
            //数字
            getcode += QString::number(qrand()%10);
        }
        else if(num == 1)
        {
            //大写字母
            int temp = 'A';
            getcode += static_cast<QChar>(temp + qrand()%26);
        } else if(num == 2)
        {
            //小写字母
            int temp = 'a';
            getcode += static_cast<QChar>(temp + qrand()%26);
        }
    }
    return getcode;
}

本节核心-重写鼠标点击事件-每次点击一次就生成新的随机验证码

//重写鼠标点击事件,没点击一次就随机生成新的验证码
void loginWidget::mousePressEvent(QMouseEvent *event)
{
    //验证码区域做重写鼠标点击事件
    int x = event->x();
    int y = event->y();
    if(x>300 && x<450 && y>150 && y<200)
    {

        this->code = this->getcode();
    }
}

本节核心-明文密文显示函数

//明文密文显示函数
void loginWidget::PlainAndCipher()
{
    //0是密文 1是明文
    if(this->mark==0)
    {
        this->password_edit->setEchoMode(QLineEdit::Normal);
        this->mark=1;
    }
    else if(this->mark==1)
    {
        this->password_edit->setEchoMode(QLineEdit::Password);
        this->mark=0;
    }
}

猜你喜欢

转载自blog.csdn.net/m0_56051805/article/details/125174286