This commit is contained in:
zhuzichu 2023-08-18 11:46:02 +08:00
parent 2ead64e9b5
commit c0efd71d31
4 changed files with 157 additions and 0 deletions

View File

@ -355,6 +355,12 @@ FluObject{
navigationView.push("qrc:/example/qml/page/T_Screenshot.qml")
}
}
FluPaneItem{
title:"Captcha"
onTap:{
navigationView.push("qrc:/example/qml/page/T_Captcha.qml")
}
}
FluPaneItem{
title:"Chart"
onTap:{

View File

@ -0,0 +1,54 @@
import QtQuick
import QtQuick.Layouts
import QtQuick.Window
import QtQuick.Controls
import FluentUI
import "qrc:///example/qml/component"
FluScrollablePage{
title:"Captcha"
FluCaptcha{
id:captcha
Layout.topMargin: 20
MouseArea{
anchors.fill: parent
cursorShape: Qt.PointingHandCursor
onClicked: {
captcha.refresh()
}
}
}
FluButton{
text:"Refresh"
Layout.topMargin: 20
onClicked: {
captcha.refresh()
}
}
RowLayout{
spacing: 10
Layout.topMargin: 10
FluTextBox{
id:text_box
placeholderText: "请输入验证码"
}
FluButton{
text:"verify"
onClicked: {
var success = captcha.verify(text_box.text)
if(success){
showSuccess("验证码正确")
}else{
showError("错误验证,请重新输入")
}
}
}
}
}

73
src/FluCaptcha.cpp Normal file
View File

@ -0,0 +1,73 @@
#include "FluCaptcha.h"
#include <QTime>
#include <QChar>
#include <QPainter>
#include <QRandomGenerator>
#include <qmath.h>
FluCaptcha::FluCaptcha(QQuickItem *parent)
: QQuickPaintedItem(parent)
{
font(QFont("楷体",25,QFont::Bold,true));
setWidth(180);
setHeight(80);
refresh();
}
void FluCaptcha::paint(QPainter* painter)
{
painter->save();
painter->fillRect(boundingRect().toRect(),QColor(255,255,255,255));
QPen pen;
painter->setFont(_font);
for(int i=0;i<100;i++)
{
pen = QPen(QColor(_generaNumber(256),_generaNumber(256),_generaNumber(256)));
painter->setPen(pen);
painter->drawPoint(_generaNumber(180),_generaNumber(80));
}
for(int i=0;i<5;i++)
{
pen = QPen(QColor(_generaNumber(256),_generaNumber(256),_generaNumber(256)));
painter->setPen(pen);
painter->drawLine(_generaNumber(180),_generaNumber(80),_generaNumber(180),_generaNumber(80));
}
for(int i=0;i<4;i++)
{
pen = QPen(QColor(_generaNumber(255),_generaNumber(255),_generaNumber(255)));
painter->setPen(pen);
painter->drawText(15+35*i,10+_generaNumber(15),30,40,Qt::AlignCenter, QString(_code[i]));
}
painter->restore();
}
int FluCaptcha::_generaNumber(int number){
return QRandomGenerator::global()->bounded(0,number);
}
void FluCaptcha::refresh(){
this->_code.clear();
for(int i = 0;i < 4;++i)
{
int num = _generaNumber(3);
if(num == 0)
{
this->_code += QString::number(_generaNumber(10));
}
else if(num == 1)
{
int temp = 'A';
this->_code += static_cast<QChar>(temp + _generaNumber(26));
}
else if(num == 2)
{
int temp = 'a';
this->_code += static_cast<QChar>(temp + _generaNumber(26));
}
}
update();
}
bool FluCaptcha::verify(const QString& code){
return this->_code == code;
}

24
src/FluCaptcha.h Normal file
View File

@ -0,0 +1,24 @@
#ifndef FLUCAPTCHA_H
#define FLUCAPTCHA_H
#include <QQuickItem>
#include <QQuickPaintedItem>
#include <QPainter>
#include "stdafx.h"
class FluCaptcha : public QQuickPaintedItem
{
Q_OBJECT
Q_PROPERTY_AUTO(QFont,font);
QML_NAMED_ELEMENT(FluCaptcha)
private:
int _generaNumber(int number);
QString _code;
public:
explicit FluCaptcha(QQuickItem *parent = nullptr);
void paint(QPainter* painter) override;
Q_INVOKABLE void refresh();
Q_INVOKABLE bool verify(const QString& code);
};
#endif // FLUCAPTCHA_H