一、新建项目
创建一个"Qt Widget Application"项目,基类选择“QMainWindow”
二、自定义CaptchaLabel类
右击项目名,选择"Add New...”
C++ -> C++Class,点击“Choose”
更改类名CaptchaLabel,添加基类QLabel,点击“下一步”
默认,点击完成
更改captchalabel.h代码如下
#ifndef CAPTCHALABEL_H#define CAPTCHALABEL_H#include <QObject>#include <QLabel>class CaptchaLabel : public QLabel{public:explicit CaptchaLabel(QWidget* parent = nullptr);//获取随机数QString getRandNumber() { return m_randNumber; }void generateRandNumber(int num = 4); //生成随机数protected:void paintEvent(QPaintEvent *event); //绘制void mousePressEvent(QMouseEvent *event); //鼠标按下private:QString m_randNumber; //随机数};#endif // CAPTCHALABEL_H
更改captchalabel.cpp代码如下
#include "captchalabel.h"
#include <QDebug>
#include <QRandomGenerator>
#include <QTimer>
#include <QPainter>
#include <QTime>CaptchaLabel::CaptchaLabel(QWidget *parent) : QLabel(parent)
{this->setMinimumSize(100, 30); //设置最小大小this->setStyleSheet("background-color:white;"); //白色背景//随机数种子初始化qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));//生成随机数generateRandNumber();
}/**
* @brief CaptchaLabel::generateRandNum 生成随机数,默认是四个数
* @param num
*/
void CaptchaLabel::generateRandNumber(int num)
{m_randNumber.clear();for(int i = 0; i < num; i++){int num = qrand() % 3;if(num == 0) //数字{m_randNumber += QString::number(qrand()%10);}else if(num == 1) //大写字母{int temp = 'A';m_randNumber += static_cast<QChar>(temp + qrand()%26);}else if(num == 2) //小写字母{int temp = 'a';m_randNumber += static_cast<QChar>(temp + qrand()%26);}}
}/**
* @brief CaptchaLabel::paintEvent 绘制验证码
*/
void CaptchaLabel::paintEvent(QPaintEvent *)
{QPainter painter(this);int num = m_randNumber.size(); //几个随机数int width = this->width();int height = this->height();//设置字体int size = (width/num) < height ? (width/num) : height;QFont font;font.setPixelSize(size); //设置字体大小font.setItalic(true); //设置字体倾斜font.setFamily(QString("楷体"));painter.setFont(font); //若设置字体加粗,有的随机数会显示不全QPen pen;//绘制随机数for(int i = 0; i < num; i++){pen = QPen(QColor(qrand()%255, qrand()%255, qrand()%255));painter.setPen(pen);int y = (height-5-size) <= 0 ? 0 : (qrand()%(height-5-size));QRectF rect(width/num*i, y, width/num, size);//painter.drawRect(rect); //测试时,查看矩形的位置及大小//在矩形rect中绘制大小为size的随机数painter.drawText(rect, Qt::AlignHCenter|Qt::AlignBottom, m_randNumber.at(i));}//绘制干扰点for(int i = 0; i < (num*25); i++){pen = QPen(QColor(qrand()%256, qrand()%256, qrand()%256));painter.setPen(pen);painter.drawPoint(qrand()%width, qrand()%height);}//绘制干扰线for(int i = 0; i < num; i++){pen = QPen(QColor(qrand()%256, qrand()%256, qrand()%256));painter.setPen(pen);painter.drawLine(qrand()%width, qrand()%height, qrand()%width, qrand()%height);}
}/*** @brief CaptchaLabel::mousePressEvent 鼠标点击时更换随机数* @param event*/
void CaptchaLabel::mousePressEvent(QMouseEvent *event){Q_UNUSED(event);//重新生成验证码generateRandNumber();//实时更新update();
}
三、使用自定义CaptchaLabel类
在项目的界面编辑器中添加一个QLabel,右击,提升为
输入类名,点击“添加”
出现如下红框中的内容,点击“提升”
label的基类被更改为自定义CaptchaLabel类
再在界面上添加一个QLineEdit和QPushButton控件,再进行垂直布局
右击PushButton,选择“转到槽”
选择“clicked()”,点击“OK”
生成如下代码
更改代码如下
四、运行测试
运行项目
验证码大小随着界面大小更改而更改
点击label,验证码会改变
输入正确的验证码(区分大小写),点击“PushButton”,输出“验证码正确”提示信息
输入错误的验证码(区分大小写),点击“PushButton”,输出“验证码错误”提示信息