最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

QT生成隨機驗證碼的方法

 更新時間:2022年06月14日 14:31:49   作者:成長途中永遠是獨孤的  
這篇文章主要為大家詳細介紹了QT生成隨機驗證碼的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了QT生成隨機驗證碼的具體代碼,供大家參考,具體內(nèi)容如下

一、先創(chuàng)建一個QT應(yīng)用程序,在ui中添加一個QFrame控件,后期將這個控件提升為下面自己實現(xiàn)驗證碼的類就可以顯示出來了。

示例代碼如下:

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();
}

主函數(shù):

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設(shè)計師類,在里面實現(xiàn)驗證碼的隨機生成。

代碼如下:

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; ? ? //隨機產(chǎn)生一個字符
? ? void produceVerificationCodes() const; ?//這是一個用來生成驗證碼的函數(shù)
? ? void produceRandomColors() const; ? ? ? //產(chǎn)生隨機的顏色
? ? void paintEvent(QPaintEvent *event); ? ?//重寫繪制事件,以此來生成驗證碼
? ? Qt::GlobalColor* getColor(); ? ? ? ? ? ?//返回設(shè)置驗證碼的顏色
? ? void Timer_Timeout();
? ? QString getCaptcha();

private:
? ? const int letter_number = 4; ? ?//產(chǎn)生字符的數(shù)量
? ? 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;
? ? //背景設(shè)為白色
? ? painter.fillRect(this->rect(), Qt::white);
? ? //產(chǎn)生4個不同的字符
? ? produceVerificationCodes();
? ? //產(chǎn)生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;
}

三、在主函數(shù)里面添加如下代碼:

**.h

?Verification *verification;

**.cpp

void VLoginDlg::on_btnClick_clicked()?? ?//點擊更新驗證碼
{
? ? verification->Timer_Timeout();
}

運行效果圖

當(dāng)點擊最右端按鈕時,驗證碼會自動刷新

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 基于C語言實現(xiàn)圖書管理信息系統(tǒng)設(shè)計

    基于C語言實現(xiàn)圖書管理信息系統(tǒng)設(shè)計

    這篇文章主要為大家詳細介紹了基于C語言實現(xiàn)圖書管理信息系統(tǒng)設(shè)計與實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • C++11實現(xiàn)簡易定時器的示例代碼

    C++11實現(xiàn)簡易定時器的示例代碼

    這篇文章主要介紹了C++11實現(xiàn)簡易定時器的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • c/c++中的左值右值詳解

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

    這篇文章主要介紹了c/c++中的左值右值,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2025-04-04
  • C語言非遞歸算法解決快速排序與歸并排序產(chǎn)生的棧溢出

    C語言非遞歸算法解決快速排序與歸并排序產(chǎn)生的棧溢出

    上期我們講完了排序算法下,不知道小伙伴們有沒有發(fā)現(xiàn)一個問題,快速排序和歸并排序我們都是用遞歸來實現(xiàn)的,可能有小伙伴會問,如果說數(shù)據(jù)量很多話,棧區(qū)空間會不會不夠用呢?這期我們就來解決使用遞歸實現(xiàn)的排序?qū)е聴R绯鋈绾谓鉀Q
    2022-04-04
  • C++鏈接器工作原理詳解

    C++鏈接器工作原理詳解

    當(dāng)文件見過編譯后就需要進行一個鏈接的操作接下來我們就說說什么是鏈接,本文給大家介紹了C++鏈接器是如何工作的,文章通過代碼示例和圖文介紹的非常詳細,需要的朋友可以參考下
    2024-02-02
  • C++ Boost Algorithm算法超詳細精講

    C++ Boost Algorithm算法超詳細精講

    Boost.Algorithm 提供了補充標準庫算法的算法。與 Boost.Range 不同,Boost.Algorithm 沒有引入新概念。 Boost.Algorithm 定義的算法類似于標準庫中的算法
    2022-10-10
  • C++中sprintf()函數(shù)的使用詳解

    C++中sprintf()函數(shù)的使用詳解

    本篇文章是對C++中sprintf()函數(shù)的使用進行了詳細的分析介紹,需要的朋友參考下
    2013-05-05
  • 詳解C++?的STL迭代器原理和實現(xiàn)

    詳解C++?的STL迭代器原理和實現(xiàn)

    這篇文章主要為大家介紹了C++的STL迭代器原理和實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2022-01-01
  • C/C++實現(xiàn)7bit與8bit編碼互相轉(zhuǎn)換

    C/C++實現(xiàn)7bit與8bit編碼互相轉(zhuǎn)換

    這篇文章主要為大家詳細介紹了如何使用C/C++實現(xiàn)7bit與8bit編碼互相轉(zhuǎn)換功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-10-10
  • C++容器std::vector的swap()函數(shù)使用方式

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

    這篇文章主要介紹了C++容器std::vector的swap()函數(shù)使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08

最新評論

吉安市| 临海市| 长岭县| 台南县| 晋城| 湄潭县| 峡江县| 东兴市| 鄂尔多斯市| 辛集市| 彰武县| 阿拉善右旗| 宁晋县| 石景山区| 石渠县| 永吉县| 体育| 临夏市| 若羌县| 墨玉县| 南溪县| 荥阳市| 谷城县| 秦皇岛市| 神池县| 泽普县| 蓬莱市| 遂川县| 梁河县| 竹北市| 佛山市| 阿瓦提县| 旬邑县| 阳东县| 井研县| 五大连池市| 鹤山市| 西林县| 渭南市| 大港区| 新宁县|