網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2022-06-08 Python?全局空間和局部空間_python
- 2021-11-08 C++繼承模式詳解_C 語言
- 2022-08-26 Python?Decorator裝飾器的創建方法及常用場景分析_python
- 2022-10-03 使用useImperativeHandle時父組件第一次沒拿到子組件的問題_React
- 2022-04-17 python中random隨機函數詳解_python
- 2023-04-20 el-table多選+搜索
- 2022-04-28 Python字符串的創建和駐留機制詳解_python
- 2022-10-02 Go語言快速入門指針Map使用示例教程_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支