QT生成随机验证码的方法

 更新时间:2022年06月14日 14:31:49   作者:成长途中永远是独孤的  
这篇文章主要为大家详细介绍了QT生成随机验证码的方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了QT生成随机验证码的具体代码,供大家参考,具体内容如下

一、先创建一个QT应用程序,在ui中添加一个QFrame控件,后期将这个控件提升为下面自己实现验证码的类就可以显示出来了。

示例代码如下:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include <QMainWindow>
#include "verification.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT
public:
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();
    
private:
    Ui::MainWindow *ui;

    Verification *verification;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"


MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    verification = ui->frame;  //提升类控件名
}

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

void MainWindow::on_pushButton_clicked()    //点击跟新验证码
{
     verification->Timer_Timeout();
}

主函数:

main.cpp

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

mainwindow.ui

二、右击新添加一个Qt设计师类,在里面实现验证码的随机生成。

代码如下:

verification.h

#ifndef VERIFICATION_H
#define VERIFICATION_H

#include <QPainter>
#include <QTimer>
#include <QFrame>
#include <QChar>
#include <QColor>

class Verification : public QFrame
{
    Q_OBJECT
public:
    Verification(QWidget *parent = Q_NULLPTR);
    ~Verification();

public:
    QString getVerificationCodes() const;   返回一个字符串(默认全为小写)(验证码)
    QChar produceRandomLetters() const;     //随机产生一个字符
    void produceVerificationCodes() const;  //这是一个用来生成验证码的函数
    void produceRandomColors() const;       //产生随机的颜色
    void paintEvent(QPaintEvent *event);    //重写绘制事件,以此来生成验证码
    Qt::GlobalColor* getColor();            //返回设置验证码的颜色
    void Timer_Timeout();
    QString getCaptcha();

private:
    const int letter_number = 4;    //产生字符的数量
    Qt::GlobalColor* m_color;
    QString m_captcha;
    QTimer m_timer;

    enum {                          //枚举分类,也可自己定义
           NUMBER_FLAG,
           UPLETTER_FLAG,
           LOWLETTER_FLAG
       };
    QChar *verificationCode;
    QColor *colorArray;
};

#endif // VERIFICATION_H

verification.cpp

#include "verification.h"
#include <QTime>
#include <QPaintEvent>

Verification::Verification(QWidget *parent)
    :QFrame(parent)
{
    //生成随机种子
    qsrand(QTime::currentTime().second() * 1000 + QTime::currentTime().msec());

//    m_captcha = getVerificationCode();
//    m_color = getColor();
    //    m_timer.start(200);

    colorArray = new QColor[letter_number];
    verificationCode = new QChar[letter_number];
    m_captcha = getVerificationCodes();
}

Verification::~Verification()
{

}

ification::getVerificationCodes() const
{


    QString s ="";
    QChar cTemp;
    for (int i = 0; i < letter_number; ++i)
    {
        cTemp = verificationCode[i];
        s += cTemp>97?cTemp.toUpper():cTemp;
    }
    return s;
}

QChar Verification::produceRandomLetters() const
{
    QChar c;
    int flag = qrand() % letter_number;
    switch (flag)
    {
    case NUMBER_FLAG:c='0' + qrand() % 10; break;
    case UPLETTER_FLAG:c='A' + qrand() % 26; break;
    case LOWLETTER_FLAG:c='a' + qrand() % 26; break;
    default:c=qrand() % 2 ? 'W' : 'D';
    }
    return c;
}

void Verification::produceVerificationCodes() const
{
    for (int i = 0; i < letter_number; ++i)
         verificationCode[i] = produceRandomLetters();
}

void Verification::produceRandomColors() const
{
    for (int i = 0; i < letter_number; ++i)
        colorArray[i] = QColor(qrand() % 255, qrand() % 255, qrand() % 255);
}


void Verification::Timer_Timeout()
{
//    m_captcha = getVerificationCode();
    m_captcha = getVerificationCodes();
//    this->repaint();
    this->update();
}

QString Verification::getCaptcha()
{
    return getVerificationCodes();
}

void Verification::paintEvent(QPaintEvent *event)
{
    painter(this);
    QPoint p;
    //背景设为白色
    painter.fillRect(this->rect(), Qt::white);
    //产生4个不同的字符
    produceVerificationCodes();
    //产生4个不同的颜色
    produceRandomColors();
    //绘制验证码
    for (int i = 0; i < letter_number; ++i)
    {
        p.setX(i*(this->width() / letter_number)+this->width()/14);
        p.setY(this->height() / 1.5);
        painter.setPen(colorArray[i]);
        painter.drawText(p, QString(verificationCode[i]));
    }
    return;
}

三、在主函数里面添加如下代码:

**.h

 Verification *verification;

**.cpp

void VLoginDlg::on_btnClick_clicked()    //点击更新验证码
{
    verification->Timer_Timeout();
}

运行效果图

当点击最右端按钮时,验证码会自动刷新

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • 基于C语言实现图书管理信息系统设计

    基于C语言实现图书管理信息系统设计

    这篇文章主要为大家详细介绍了基于C语言实现图书管理信息系统设计与实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • C++11实现简易定时器的示例代码

    C++11实现简易定时器的示例代码

    这篇文章主要介绍了C++11实现简易定时器的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • c/c++中的左值右值详解

    c/c++中的左值右值详解

    这篇文章主要介绍了c/c++中的左值右值,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2025-04-04
  • C语言非递归算法解决快速排序与归并排序产生的栈溢出

    C语言非递归算法解决快速排序与归并排序产生的栈溢出

    上期我们讲完了排序算法下,不知道小伙伴们有没有发现一个问题,快速排序和归并排序我们都是用递归来实现的,可能有小伙伴会问,如果说数据量很多话,栈区空间会不会不够用呢?这期我们就来解决使用递归实现的排序导致栈溢出如何解决
    2022-04-04
  • C++链接器工作原理详解

    C++链接器工作原理详解

    当文件见过编译后就需要进行一个链接的操作接下来我们就说说什么是链接,本文给大家介绍了C++链接器是如何工作的,文章通过代码示例和图文介绍的非常详细,需要的朋友可以参考下
    2024-02-02
  • C++ Boost Algorithm算法超详细精讲

    C++ Boost Algorithm算法超详细精讲

    Boost.Algorithm 提供了补充标准库算法的算法。与 Boost.Range 不同,Boost.Algorithm 没有引入新概念。 Boost.Algorithm 定义的算法类似于标准库中的算法
    2022-10-10
  • C++中sprintf()函数的使用详解

    C++中sprintf()函数的使用详解

    本篇文章是对C++中sprintf()函数的使用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • 详解C++ 的STL迭代器原理和实现

    详解C++ 的STL迭代器原理和实现

    这篇文章主要为大家介绍了C++的STL迭代器原理和实现,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-01-01
  • C/C++实现7bit与8bit编码互相转换

    C/C++实现7bit与8bit编码互相转换

    这篇文章主要为大家详细介绍了如何使用C/C++实现7bit与8bit编码互相转换功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2024-10-10
  • C++容器std::vector的swap()函数使用方式

    C++容器std::vector的swap()函数使用方式

    这篇文章主要介绍了C++容器std::vector的swap()函数使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-08-08

最新评论