日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

QT生成隨機驗證碼的方法_C 語言

作者:成長途中永遠是獨孤的 ? 更新時間: 2022-08-06 編程語言

本文實例為大家分享了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();
}

運行效果圖

當點擊最右端按鈕時,驗證碼會自動刷新

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

欄目分類
最近更新