Qt绘制验证码


前言

参考网上代码写的验证码,原文连接【Qt学习】随机验证码生成 & 鼠标点击事件更新验证码


提示:以下是本篇文章正文内容,下面案例可供参考

1.效果展示

请添加图片描述

2.VerificationCode.h

代码如下(示例):

#ifndef VERIFICATIONCODE_H
#define VERIFICATIONCODE_H

#include <QWidget>
#include <QMouseEvent>
#include <QPainter>
#include <QDebug>

#define POINTCOUNT  150
#define LINECOUNT   10

class VerificationCode : public QWidget
{
    Q_OBJECT
public:
    explicit VerificationCode(QWidget *parent = nullptr);
    enum VerificationCodeType { figure, capitalLetter, lowerCase };
    initData();
    inline QString getCode(){return verificationCode;}
    QList<QColor> getRandColorList();
    QList<QPoint> getRandPointList();
    QList<QLine> getRandLineList();
private:
    QString getVerificationCode();
    void paintEvent(QPaintEvent *event);
    void mousePressEvent(QMouseEvent *event);
private:
    QString         verificationCode;   //验证码
    QList<QColor>   randColorList;      //点颜色列表
    QList<QPoint>   randPointList;      //点列表
    QList<QLine>    randLineList;       //线段列表
};

#endif // VERIFICATIONCODE_H

3.VerificationCode.cpp

代码如下(示例):

#include "verificationcode.h"

VerificationCode::VerificationCode(QWidget *parent) :
    QWidget(parent)
{
    initData();
}

VerificationCode::initData()
{
    randColorList = getRandColorList();
    randPointList = getRandPointList();
    randLineList = getRandLineList();
    verificationCode = getVerificationCode();//获取随机验证码
    qDebug()<< "verificationCode = " << verificationCode;
    this->setMouseTracking(true);
    this->installEventFilter(this);
    this->setAttribute(Qt::WA_Hover, true);
}

QList<QColor> VerificationCode::getRandColorList()
{
    QList<QColor> colorList;
    for(int i = 0;i < POINTCOUNT;++i){
        colorList.append(QColor(qrand()%256,qrand()%256,qrand()%256));
    }
    return colorList;
}

QList<QPoint> VerificationCode::getRandPointList()
{
    QList<QPoint> pointList;
    for(int i = 0;i < POINTCOUNT;++i){
        pointList.append(QPoint(qrand()%150,qrand()%50));
    }
    return pointList;
}

QList<QLine> VerificationCode::getRandLineList()
{
    QList<QLine> lineList;
    for(int i = 0;i < LINECOUNT;++i){
        lineList.append(QLine(qrand()%150,qrand()%50, qrand()%150,qrand()%50));
    }
    return lineList;
}

//绘制验证码
void VerificationCode::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    QPen pen;
    //绘制背景
    painter.setPen(Qt::NoPen);
    painter.setBrush(QColor(170,225,246));
    painter.setRenderHint(QPainter::Antialiasing);
    painter.drawRect(rect());
    //画点
    for(int i = 0; i < POINTCOUNT;++i){
        pen = QPen(randColorList.value(i));
        painter.setPen(pen);
        painter.drawPoint(randPointList.value(i));
    }
    //画线
    for(int i = 0;i < LINECOUNT;++i){
        painter.drawLine(randLineList.value(i));
    }
    pen = QPen(QColor(244,115,120));
    QFont font("华文中宋",25,QFont::Bold,true);
    painter.setFont(font);
    painter.setPen(pen);
    //绘画字
    for(int i = 0;i < 6;++i){
        painter.drawText(25*i,0,30,40,Qt::AlignCenter,QString(verificationCode[i]));
    }
}

//生成验证码方法实现
QString VerificationCode::getVerificationCode()
{
	int temp;
    QString getcode;
    for(int i = 0; i < 6; ++i){
        int type = qrand()%3;
        switch (type) {
        case figure:
            //数字
            getcode += QString::number(qrand()%10);
            break;
        case capitalLetter:
            //大写字母
            temp = 'A';
            getcode += static_cast<QChar>(temp + qrand()%26);
            break;
        case lowerCase:
            //小写字母
            temp = 'a';
            getcode += static_cast<QChar>(temp + qrand()%26);
            break;
        default:
            break;
        }
    }
    update();
    return getcode;
}

//重写鼠标点击事件,每点击一次就随机生成新的验证码
void VerificationCode::mousePressEvent(QMouseEvent *event)
{
    randColorList = getRandColorList();
    randPointList = getRandPointList();
    randLineList = getRandLineList();
    this->verificationCode = this->getVerificationCode();
    qDebug()<< "验证码: " << verificationCode;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值