Qt C++中生成验证码

verificationcode.h
#ifndef VERIFICATIONCODE_H
#define VERIFICATIONCODE_H
#include <QLabel>
#include <QPainter>
#include <QMouseEvent>
#include <QTime>
class VerificationCode : public QLabel
{
Q_OBJECT
public:
explicit VerificationCode(QWidget *parent = nullptr);
// 获取当前验证码
QString getCode() const;
// 验证用户输入
bool validate(const QString& input) const;
// 公共方法:刷新验证码
void refreshCode();
signals:
void clicked();
protected:
void paintEvent(QPaintEvent *event) override;
void mousePressEvent(QMouseEvent *event) override;
private:
void generateCode();
void drawCode(QPainter &painter);
QString m_code; // 存储当前验证码
const int m_codeLength = 4; // 验证码长度
};
#endif // VERIFICATIONCODE_Hverificationcode.cpp
#include "verificationcode.h"
#include <QTime>
#include <QtMath>
#include <QDebug>
#include <QRandomGenerator>
VerificationCode::VerificationCode(QWidget *parent) : QLabel(parent)
{
// 设置初始大小
setFixedSize(120, 40);
// 设置样式
setStyleSheet("background-color: white; border: 1px solid gray;");
// 生成初始验证码
generateCode();
// 启用鼠标跟踪
setMouseTracking(true);
// 设置光标形状为手型,提示可点击
setCursor(Qt::PointingHandCursor);
}
QString VerificationCode::getCode() const
{
return m_code;
}
bool VerificationCode::validate(const QString &input) const
{
return input.toUpper() == m_code.toUpper(); // 不区分大小写比较
}
void VerificationCode::refreshCode()
{
generateCode();
update(); // 触发重绘
}
void VerificationCode::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
// 绘制背景
painter.fillRect(rect(), Qt::white);
// 绘制验证码
drawCode(painter);
}
void VerificationCode::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
refreshCode();
emit clicked();
}
QLabel::mousePressEvent(event);
}
void VerificationCode::generateCode()
{
const QString charSet = "23456789ABCDEFGHJKLMNPQRSTUVWXYZ";
m_code.clear();
QRandomGenerator::global()->bounded(QTime::currentTime().msec()); // 设置随机种子
for (int i = 0; i < m_codeLength; ++i) {
int index = QRandomGenerator::global()->bounded(0,100) % charSet.length();
m_code.append(charSet.at(index));
}
}
void VerificationCode::drawCode(QPainter &painter)
{
// 绘制干扰点
for (int i = 0; i < 100; ++i) {
int x = QRandomGenerator::global()->bounded(0,100) % width();
int y = QRandomGenerator::global()->bounded(0,100) % height();
painter.setPen(QColor(QRandomGenerator::global()->bounded(0,100) % 256, QRandomGenerator::global()->bounded(0,100) % 256, QRandomGenerator::global()->bounded(0,100) % 256, 100));
painter.drawPoint(x, y);
}
// 绘制干扰线
for (int i = 0; i < 5; ++i) {
painter.setPen(QColor(QRandomGenerator::global()->bounded(0,256), QRandomGenerator::global()->bounded(0,256), QRandomGenerator::global()->bounded(0,256), 100));
painter.drawLine(QRandomGenerator::global()->bounded(0,100) % width(), QRandomGenerator::global()->bounded(0,100) % height(), QRandomGenerator::global()->bounded(0,100) % width(), QRandomGenerator::global()->bounded(0,100) % height());
}
// 绘制验证码文本
for (int i = 0; i < m_code.length(); ++i) {
// 设置随机颜色
painter.setPen(QColor(QRandomGenerator::global()->bounded(0,128) + 64, QRandomGenerator::global()->bounded(0,128) + 64, QRandomGenerator::global()->bounded(0,128) + 64));
// 设置随机字体大小
QFont font;
font.setPointSize(18 + QRandomGenerator::global()->bounded(0,5));
font.setBold(QRandomGenerator::global()->bounded(0,5));
painter.setFont(font);
// 计算字符位置(稍微随机偏移)
int x = 10 + i * (width() - 20) / m_codeLength;
int y = height() / 2 + 10 - QRandomGenerator::global()->bounded(0,8);
// 添加随机旋转
painter.save();
painter.translate(x, y);
painter.rotate(QRandomGenerator::global()->bounded(-15,15)); // -15到15度随机旋转
painter.drawText(0, 0, QString(m_code[i]));
painter.restore();
}
}目录 返回
首页