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

學無先后,達者為師

網站首頁 編程語言 正文

Qt編寫顯示密碼強度的控件_C 語言

作者:友善啊,朋友 ? 更新時間: 2022-08-06 編程語言

本文實例為大家分享了Qt編寫顯示密碼強度控件的具體代碼,供大家參考,具體內容如下

代碼:

#ifndef WIDGET_H
#define WIDGET_H
?
#include <QWidget>
#include <QRegularExpression>
#include <QTimer>
?
class PasswordStrengthCheck : public QWidget
{
? ? Q_OBJECT
?
public:
? ? PasswordStrengthCheck(QWidget *parent = nullptr);
? ? ~PasswordStrengthCheck();
? ? virtual QSize minimumSizeHint() const override;
? ? void onLineEditTextChanged(const QString &text);
?
protected:
? ? void paintEvent(QPaintEvent *event)override;
?
private:
? ? void onTimer();
? ? QRegularExpression lowRegularExpression;
? ? QRegularExpression mediumRegularExpression;
? ? QRegularExpression highRegularExpression;
? ? double targetRatio{0};
? ? double nowRatio{0};
? ? QTimer timer;
};
#endif // WIDGET_H
#include "widget.h"
#include <QPainter>
#include <QPaintEvent>
#include <QPainterPath>
?
PasswordStrengthCheck::PasswordStrengthCheck(QWidget *parent)
? ? : QWidget(parent)
{
? ? lowRegularExpression = QRegularExpression("[A-Z][a-z][A-Za-z0-9_]{4,6}");
? ? mediumRegularExpression = QRegularExpression("[A-Z][a-z][A-Za-z0-9_]{7,9}");
? ? highRegularExpression = QRegularExpression("[A-Z][a-z][A-Za-z0-9_]{10,12}");
?
? ? connect(&timer,&QTimer::timeout,this,&PasswordStrengthCheck::onTimer);
? ? timer.setInterval(40);
}
?
PasswordStrengthCheck::~PasswordStrengthCheck()
{
}
?
QSize PasswordStrengthCheck::minimumSizeHint() const
{
? ? return QSize(100,30);
}
?
void PasswordStrengthCheck::onLineEditTextChanged(const QString & text)
{
? ? if(highRegularExpression.match(text).hasMatch())
? ? {
? ? ? ? targetRatio = 1;
? ? }
? ? else if(mediumRegularExpression.match(text).hasMatch())
? ? {
? ? ? ? targetRatio = 0.66;
? ? }
? ? else if(lowRegularExpression.match(text).hasMatch())
? ? {
? ? ? ? targetRatio = 0.33;
? ? }
? ? else
? ? {
? ? ? ? targetRatio = 0;
? ? }
? ? timer.start();
}
?
void PasswordStrengthCheck::paintEvent(QPaintEvent *event)
{
? ? QPainter painter(this);
? ? painter.setRenderHint(QPainter::Antialiasing,true);
? ? const auto rect = event->rect();
?
? ? auto width = rect.width();
? ? auto height = rect.height();
? ? painter.setBrush(Qt::white);
? ? painter.setPen(QPen(QBrush("#128bf1"),3));
?
? ? int radiu = 3;
? ? QRect borderRect = QRect(width*0.05,0,width*0.9,height).adjusted(radiu,radiu,-radiu,-radiu);
? ? painter.drawRoundedRect(borderRect,radiu,radiu);
?
? ? QPainterPath path;
? ? path.addRoundedRect(borderRect.adjusted(radiu,radiu,-radiu,-radiu),radiu,radiu);
? ? QPainterPath path2;
? ? path2.addRect(QRect(QPoint(borderRect.x() + borderRect.width() * 0.3,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?borderRect.y()),borderRect.bottomRight()));
?
? ? QPainterPath path_left = path - path2;
?
? ? path2.clear();
? ? path2.addRect(QRect(borderRect.topLeft(),
? ? ? ? ? ? ? ? ? ? ? ? QPoint(borderRect.x() + borderRect.width() * 0.7,borderRect.bottom())));
? ? QPainterPath path_right = path - path2;
?
? ? QRect mediumRect = QRect(QPoint(borderRect.x() + borderRect.width() * 0.35,borderRect.top()),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?QPoint(borderRect.bottomRight() - QPoint(borderRect.width() * 0.35,0))).adjusted(0,radiu,0,-radiu);
?
? ? QPixmap greyPixmap(rect.size());
? ? {
? ? ? ? greyPixmap.fill(Qt::transparent);
? ? ? ? QPainter painter(&greyPixmap);
? ? ? ? QBrush brush("#CDCDCD");
? ? ? ? painter.setRenderHint(QPainter::Antialiasing,true);
? ? ? ? painter.fillPath(path_left,brush);
? ? ? ? painter.fillPath(path_right,brush);
? ? ? ? painter.fillRect(mediumRect,brush);
? ? }
? ? painter.drawPixmap(rect,greyPixmap);
?
? ? if(nowRatio > 0)
? ? {
? ? ? ? QPixmap colorPixmap(QSize(width * nowRatio,height));
? ? ? ? {
? ? ? ? ? ? colorPixmap.fill(Qt::transparent);
? ? ? ? ? ? QPainter painter(&colorPixmap);
? ? ? ? ? ? painter.setRenderHint(QPainter::Antialiasing,true);
? ? ? ? ? ? painter.fillPath(path_left,QBrush("#EC3700"));
? ? ? ? ? ? painter.fillPath(path_right,QBrush("#F78115"));
? ? ? ? ? ? painter.fillRect(mediumRect,QBrush("#6AA000"));
? ? ? ? }
? ? ? ? painter.drawPixmap(QPoint(0,0),colorPixmap);
? ? }
}
?
void PasswordStrengthCheck::onTimer()
{
? ? static double e=0.0002;
?
? ? if(fabs(targetRatio - nowRatio) < e)
? ? {
? ? ? ? timer.stop();
? ? ? ? return;
? ? }
?
? ? if(nowRatio < targetRatio)
? ? {
? ? ? ? nowRatio += 0.02;
? ? }
? ? else
? ? {
? ? ? ? nowRatio -= 0.02;
? ? }
? ? update();
}

使用:

#include "widget.h"
#include <QApplication>
#include <QLineEdit>
#include <QFormLayout>
?
int main(int argc, char *argv[])
{
? ? QApplication a(argc, argv);
?
? ? QWidget w;
? ? QLineEdit * lineEdit = new QLineEdit;
? ? PasswordStrengthCheck * c = new PasswordStrengthCheck;
? ? QObject::connect(lineEdit,&QLineEdit::textChanged,c,&PasswordStrengthCheck::onLineEditTextChanged);
? ? QFormLayout * layout = new QFormLayout(&w);
? ? layout->addRow("密碼:",lineEdit);
? ? layout->addRow("",c);
? ? w.show();
? ? return a.exec();
}

效果:

原文鏈接:https://blog.csdn.net/kenfan1647/article/details/123618175

欄目分類
最近更新