• 【QT学习】扫描二维码获取登录验证码(完整源码)


    • 📢欢迎各位读者:点赞 👍 收藏 ⭐留言 📝
    • 📢博客主页:https://blog.csdn.net/qq_59134387😀
    • 📢原创不易,转载请标明出处;如有错误,敬请指正批评!💦
    • 📢我不去想是否能够成功,既然选择了远方,便只顾风雨兼程!✨


    前言

       目前,二维码的使用已经十分便捷和广泛。本篇,作者将分享如何生成四位随机验证码,并将随机验证码信息生成二维码,通过微信或其他方式扫描二维码,即可以获取验证码信息。


    一、实现效果

    • 通过扫描二维码,可以获取到二维码内的验证码信息,若验证失败,则自动刷新二维码和验证码。

    在这里插入图片描述

    二、核心步骤

    1.生成四位随机验证码

    void qr_create::generateRandNum()
    {
    	//清空二维码字符串
        this->code.clear();
        //循环生成四位数字/字母
        for(int i = 0;i < 4;++i)
        {
        	//通过随机数决定生成的字符类型:数字,大写字母或小写字母
            int num = qrand()%3;
         	//根据字符类型,生成随机验证码
            if(num == 0)
            {
                //数字
                this->code += QString::number(qrand()%10);
            }
            else if(num == 1)
            {
                //大写字母
                int temp = 'A';
                this->code += static_cast<QChar>(temp + qrand()%26);
            }
            else if(num == 2)
            {
                //小写字母
                int temp = 'a';
                this->code += static_cast<QChar>(temp + qrand()%26);
            }
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    2.生成保存字符二维码

    (1)下载二维码库文件,可留言私信作者获取。

    在这里插入图片描述
    (2)将下载好的二维码库文件,放到工程文件目录下

    在这里插入图片描述

    (3)在工程文件中,选择添加现有文件,将二维码库文件添加到工程中

    在这里插入图片描述

    在这里插入图片描述

    (4)引用库文件,根据验证码字符生成二维码图像,最后通过标签显示在窗口中

    • 核心代码如下:
    void qr_create::Show_QRcode_Picture(QString message)
    {
        //创建数据向量容器,存储要生成的二维码内容
        std::vector<QrSegment> segs = QrSegment::makeSegments(message.toUtf8());
    
        //创建二维码对象,并根据数据向量容器初始化对象信息
        QrCode qr1 = QrCode::encodeSegments(segs, QrCode::Ecc::HIGH, 3, 3, 2, false);
    
        //创建二维码画布
        QImage QrCode_Image=QImage(qr1.getSize(),qr1.getSize(),QImage::Format_RGB888);
    
        for (int y = 0; y < qr1.getSize(); y++)
        {
            for (int x = 0; x < qr1.getSize(); x++)
            {
                if(qr1.getModule(x, y)==0)
                {
                    QrCode_Image.setPixel(x,y,qRgb(255,255,255));
                }
                else
                {
                    QrCode_Image.setPixel(x,y,qRgb(0,0,0));
                }
            }
        }
    
        //图像大小转换为适当的大小
        QrCode_Image=QrCode_Image.scaled(this->QrLabel->width(),this->QrLabel->height(),Qt::KeepAspectRatio);
    
        //转换为QPixmap在Label中显示
        this->QrLabel->setPixmap(QPixmap::fromImage(QrCode_Image));
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    三、完整源码

    1.main.cpp文件

    #include "qr_create.h"
    #include 
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
    
        qr_create w;
        w.show();
    
        return a.exec();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.qr_create.h文件

    #ifndef QR_CREATE_H
    #define QR_CREATE_H
    
    #include 
    #include 
    #include 
    #include 
    #include 
    
    
    class qr_create : public QWidget
    {
        Q_OBJECT
    public:
        explicit qr_create(QWidget *parent = 0);
        void generateRandNum();
        void Show_QRcode_Picture(QString message);
        QString code;
        QLabel *QrLabel,*textLabel;
        QLineEdit *codeEdit;
        QPushButton *flushBtn,*verifyBtn;
    signals:
    
    public slots:
        void QrFlush();
        void QrVerify();
    };
    
    #endif // QR_CREATE_H
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    3.qr_create.cpp文件

    #include 
    #include 
    #include 
    #include 
    #include 
    #include "QrCode.h"
    #include "qr_create.h"
    
    using namespace qrcodegen;
    
    qr_create::qr_create(QWidget *parent) : QWidget(parent)
    {
        this->setFixedSize(400,440);
    
        //创建标签,并初始化
        this->QrLabel = new QLabel(this);
        this->QrLabel->setGeometry(50,50,300,300);
    
        QFont font;
        font.setPointSize(12);
        this->textLabel = new QLabel("验证码",this);
        this->textLabel->setGeometry(50,380,60,30);
        this->textLabel->setFont(font);
    
        //创建输入框,并初始化
        this->codeEdit = new QLineEdit(this);
        this->codeEdit->setGeometry(125,380,100,30);
        this->codeEdit->setMaxLength(4);
        this->codeEdit->setValidator(new QRegExpValidator(QRegExp("[a-zA-Z0-9]+$")));
    
        //创建按钮,并初始化
        this->verifyBtn = new QPushButton("验证",this);
        this->verifyBtn->setGeometry(240,380,50,30);
        connect(this->verifyBtn,SIGNAL(clicked(bool)),this,SLOT(QrVerify()));
    
        this->flushBtn= new QPushButton("刷新",this);
        this->flushBtn->setGeometry(300,380,50,30);
        connect(this->flushBtn,SIGNAL(clicked(bool)),this,SLOT(QrFlush()));
    
        //随机数种子初始化
        qsrand(QTime(0,0,0).secsTo(QTime::currentTime()));
    
        //生成随机数
        this->generateRandNum();
    
        //根据验证码生成二维码
        this->Show_QRcode_Picture("验证码:"+code);
    }
    
    void qr_create::generateRandNum()
    {
        this->code.clear();
        for(int i = 0;i < 4;++i)
        {
            int num = qrand()%3;
            if(num == 0)
            {
                //数字
                this->code += QString::number(qrand()%10);
            }
            else if(num == 1)
            {
                //大写字母
                int temp = 'A';
                this->code += static_cast<QChar>(temp + qrand()%26);
            }
            else if(num == 2)
            {
                //小写字母
                int temp = 'a';
                this->code += static_cast<QChar>(temp + qrand()%26);
            }
        }
    }
    
    void qr_create::Show_QRcode_Picture(QString message)
    {
        //创建数据向量容器,存储要生成的二维码内容
        vector<QrSegment> segs = QrSegment::makeSegments(message.toUtf8());
        
        //创建二维码对象,并根据数据向量容器初始化对象信息
        QrCode qr1 = QrCode::encodeSegments(segs, QrCode::Ecc::HIGH, 3, 3, 2, false);
    
        //创建二维码画布
        QImage QrCode_Image=QImage(qr1.getSize(),qr1.getSize(),QImage::Format_RGB888);
    
        for (int y = 0; y < qr1.getSize(); y++)
        {
            for (int x = 0; x < qr1.getSize(); x++)
            {
                if(qr1.getModule(x, y)==0)
                {
                    QrCode_Image.setPixel(x,y,qRgb(255,255,255));
                }
                else
                {
                    QrCode_Image.setPixel(x,y,qRgb(0,0,0));
                }
            }
        }
    
        //图像大小转换为适当的大小
        QrCode_Image=QrCode_Image.scaled(this->QrLabel->width(),this->QrLabel->height(),Qt::KeepAspectRatio);
    
        //转换为QPixmap在Label中显示
        this->QrLabel->setPixmap(QPixmap::fromImage(QrCode_Image));
    }
    
    void qr_create::QrFlush()
    {
        //生成随机数
        this->generateRandNum();
        //根据验证码生成二维码
        this->Show_QRcode_Picture("验证码:"+code);
    }
    
    void qr_create::QrVerify()
    {
        if(this->codeEdit->text() == code)
        {
            //清空编辑框
            this->codeEdit->clear();
            //弹出提示窗
            QMessageBox::information(nullptr,"温馨提示","验证成功!");
        }
        else
        {
            //清空编辑框
            this->codeEdit->clear();
            //弹出提示窗
            QMessageBox::information(nullptr,"温馨提示","验证失败,请重新扫描二维码!");
            //生成随机数
            this->generateRandNum();
            //根据验证码生成二维码
            this->Show_QRcode_Picture("验证码:"+code);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137

    总结

       以上就是【QT学习】扫描二维码获取登录验证码(完整源码)的所有内容,希望大家阅读后都能有所收获!原创不易,转载请标明出处,若文章出现有误之处,欢迎读者留言指正批评!

    在这里插入图片描述

  • 相关阅读:
    【入门】二分查找右侧边界
    Unity之Hololens如何升级MRTK内置shader支持URP
    红警快捷键总结
    JAVA多线程
    腾讯云的ubuntu系统,远程登录不上,解决方案
    mac查看端口占用程序
    文件路径操作
    kibana配置文件中明文密码加密
    如何正确使用电流探头
    YB4618 具有充电前端过电压和过温保护功能,低压差充电前端 OVP 保护开关IC
  • 原文地址:https://blog.csdn.net/qq_59134387/article/details/127263998