欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

QT生成隨機(jī)驗(yàn)證碼的方法

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

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

一、先創(chuàng)建一個QT應(yīng)用程序,在ui中添加一個QFrame控件,后期將這個控件提升為下面自己實(shí)現(xiàn)驗(yà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() ? ?//點(diǎn)擊跟新驗(yàn)證碼
{
? ? ?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è)計(jì)師類,在里面實(shí)現(xiàn)驗(yàn)證碼的隨機(jī)生成。

代碼如下:

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; ? 返回一個字符串(默認(rèn)全為小寫)(驗(yàn)證碼)
? ? QChar produceRandomLetters() const; ? ? //隨機(jī)產(chǎn)生一個字符
? ? void produceVerificationCodes() const; ?//這是一個用來生成驗(yàn)證碼的函數(shù)
? ? void produceRandomColors() const; ? ? ? //產(chǎn)生隨機(jī)的顏色
? ? void paintEvent(QPaintEvent *event); ? ?//重寫繪制事件,以此來生成驗(yàn)證碼
? ? Qt::GlobalColor* getColor(); ? ? ? ? ? ?//返回設(shè)置驗(yàn)證碼的顏色
? ? 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)
{
? ? //生成隨機(jī)種子
? ? 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();
? ? //繪制驗(yàn)證碼
? ? 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()?? ?//點(diǎn)擊更新驗(yàn)證碼
{
? ? verification->Timer_Timeout();
}

運(yùn)行效果圖

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

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

相關(guān)文章

  • C++基本算法思想之遞推算法思想

    C++基本算法思想之遞推算法思想

    遞推算法需要用戶知道答案和問題之間的邏輯關(guān)系。在許多數(shù)學(xué)問題中,都有明確的計(jì)算公式可以遵循,因此可以采用遞推算法來實(shí)現(xiàn)
    2013-10-10
  • 使用OpenGL創(chuàng)建窗口的示例詳解

    使用OpenGL創(chuàng)建窗口的示例詳解

    OpenGL,也就是Open?Graphics?Library。其主要就是用于我們?nèi)ヤ秩?D、3D矢量圖形的一種跨語言、跨平臺的應(yīng)用程序編程接口,這篇文章主要介紹了使用OpenGL創(chuàng)建窗口,需要的朋友可以參考下
    2022-04-04
  • floyd算法實(shí)現(xiàn)思路及實(shí)例代碼

    floyd算法實(shí)現(xiàn)思路及實(shí)例代碼

    這篇文章主要介紹了floyd算法實(shí)現(xiàn)思路及實(shí)例代碼,有需要的朋友可以參考一下
    2014-01-01
  • C++?select模型簡單聊天室的實(shí)現(xiàn)示例

    C++?select模型簡單聊天室的實(shí)現(xiàn)示例

    本文主要介紹了C++?select模型簡單聊天室的實(shí)現(xiàn)示例,使用CMake項(xiàng)目進(jìn)行開發(fā),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-05-05
  • ubuntu修改gcc版本的操作方法

    ubuntu修改gcc版本的操作方法

    今天小編就為大家分享一篇ubuntu修改gcc版本的操作方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12
  • Qt編寫地圖綜合應(yīng)用之繪制雨量分布

    Qt編寫地圖綜合應(yīng)用之繪制雨量分布

    雨量分布圖是在區(qū)域地圖基礎(chǔ)上,針對區(qū)域中的每個最小單位區(qū)域比如縣城點(diǎn)位不同顏色顯示。本文將詳細(xì)為大家介紹如何通過QT編寫繪制雨量分布,感興趣的小伙伴可以了解一下
    2021-12-12
  • C語言do關(guān)鍵字的具體使用

    C語言do關(guān)鍵字的具體使用

    本篇文章主要介紹了C語言do關(guān)鍵字的具體使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • C++使用TinyXML2實(shí)現(xiàn)解析和生成XML數(shù)據(jù)

    C++使用TinyXML2實(shí)現(xiàn)解析和生成XML數(shù)據(jù)

    TinyXML2是一個輕量級的、開源的C++庫,專門用于解析和生成XML文檔,本文主要為大家介紹了如何使用TinyXML2實(shí)現(xiàn)解析和生成XML數(shù)據(jù),需要的可以參考下
    2024-04-04
  • 用C語言實(shí)現(xiàn)井字棋游戲代碼

    用C語言實(shí)現(xiàn)井字棋游戲代碼

    大家好,本篇文章主要講的是用C語言實(shí)現(xiàn)井字棋游戲代碼,感興趣的同學(xué)趕快來看一看吧,對你有幫助的話記得收藏一下,方便下次瀏覽
    2022-01-01
  • C++語法詳解之封裝、構(gòu)造函數(shù)、析構(gòu)函數(shù)

    C++語法詳解之封裝、構(gòu)造函數(shù)、析構(gòu)函數(shù)

    這篇文章主要介紹了C++語法詳解之封裝、構(gòu)造函數(shù)、析構(gòu)函數(shù)的相關(guān)知識,通過實(shí)例代碼給大家詳細(xì)介紹,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-03-03

最新評論