mirror of
https://github.com/zhuzichu520/FluentUI.git
synced 2024-11-26 21:37:04 +08:00
update
This commit is contained in:
parent
2ead64e9b5
commit
c0efd71d31
@ -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:{
|
||||
|
54
example/qml/page/T_Captcha.qml
Normal file
54
example/qml/page/T_Captcha.qml
Normal 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
73
src/FluCaptcha.cpp
Normal 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
24
src/FluCaptcha.h
Normal 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
|
Loading…
Reference in New Issue
Block a user