服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - QT生成随机验证码的方法

QT生成随机验证码的方法

2022-12-24 15:40成长途中永远是独孤的 C/C++

这篇文章主要为大家详细介绍了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生成随机验证码的方法

二、右击新添加一个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();
}

运行效果图

QT生成随机验证码的方法

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

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

原文链接:https://blog.csdn.net/DYDlove/article/details/109240609

延伸 · 阅读

精彩推荐
  • C/C++C++实现LeetCode(113.二叉树路径之和之二)

    C++实现LeetCode(113.二叉树路径之和之二)

    这篇文章主要介绍了C++实现LeetCode(113.二叉树路径之和之二),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考...

    Grandyang6372021-11-26
  • C/C++C++对象模型之RTTI的实现原理

    C++对象模型之RTTI的实现原理

    RTTI是Runtime Type Identification的缩写,意思是运行时类型识别。C++引入这个机制是为了让程序在运行时能根据基类的指针或引用来获得该指针或引用所指的对象...

    今日头条1762020-10-23
  • C/C++深入理解c++常成员函数和常对象

    深入理解c++常成员函数和常对象

    下面小编就为大家带来一篇深入理解c++常成员函数和常对象。小编觉得挺不错的,现在分享给大家,也给大家做个参考,一起跟随小编过来看看吧...

    C++教程网6942021-04-02
  • C/C++VisualStudio2019配置OpenCV的详细过程

    VisualStudio2019配置OpenCV的详细过程

    这篇文章主要介绍了VisualStudio2019配置OpenCV,配置系统环境找到高级系统设置等一系列操作,本文分步骤通过图文并茂的形式给大家介绍的非常详细,需要的...

    go0dStudy9202022-08-13
  • C/C++C++实现LeetCode(141.单链表中的环)

    C++实现LeetCode(141.单链表中的环)

    这篇文章主要介绍了C++实现LeetCode(141.单链表中的环),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下...

    Grandyang4432021-12-02
  • C/C++深入sizeof的使用详解

    深入sizeof的使用详解

    本篇文章是对sizeof的使用进行了详细的分析介绍,需要的朋友参考下...

    C语言教程网2192020-12-08
  • C/C++C语言实现自定义扫雷游戏(递归版)

    C语言实现自定义扫雷游戏(递归版)

    这篇文章主要为大家详细介绍了C语言实现自定义扫雷游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    caco95279582022-11-01
  • C/C++C/C++字符串函数之复制函数详解

    C/C++字符串函数之复制函数详解

    下面小编就为大家带来一篇C/C++字符串函数之复制函数详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C++教程网5342021-04-15