Qt编写显示密码强度的控件

 更新时间:2022年06月14日 14:08:13   作者:友善啊,朋友  
这篇文章主要为大家详细介绍了Qt编写显示密码强度的控件,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

效果:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C++中构造函数的参数缺省的详解

    C++中构造函数的参数缺省的详解

    这篇文章主要介绍了C++中构造函数的参数缺省的详解的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-10-10
  • Opencv学习教程之漫水填充算法实例详解

    Opencv学习教程之漫水填充算法实例详解

    这篇文章主要给大家介绍了Opencv学习教程之漫水填充算法的相关资料,文中给出了详细的示例代码供大家参考学习,对大家具有一定的参考价值,需要的朋友们下面跟着小编一起来学习学习吧。
    2017-06-06
  • 介绍C语言中tolower函数的实例

    介绍C语言中tolower函数的实例

    这篇文章主要介绍了介绍C语言中tolower函数的实例,本文列出了该函数的头文件,功能说明等,以及如何使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • 利用C语言实现任务调度的示例代码

    利用C语言实现任务调度的示例代码

    这篇文章主要为大家详细介绍了如何利用纯C语言实现任务调度(可用于STM32、C51等单片机),文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2023-04-04
  • C语言实现考试报名管理系统

    C语言实现考试报名管理系统

    这篇文章主要为大家详细介绍了C语言实现考试报名管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • WIN32程序获取父进程ID的方法

    WIN32程序获取父进程ID的方法

    这篇文章主要介绍了WIN32程序获取父进程ID的方法,在进行windows程序开发的时候有一定的实用价值,需要的朋友可以参考下
    2014-08-08
  • C语言中使用lex统计文本文件字符数

    C语言中使用lex统计文本文件字符数

    这篇文章主要介绍了C语言中使用lex统计文本文件字符数,本文直接给出实现代码,需要的朋友可以参考下
    2015-04-04
  • C语言数据的存储超详细讲解上篇

    C语言数据的存储超详细讲解上篇

    使用编程语言进行编程时,需要用到各种变量来存储各种信息。变量保留的是它所存储的值的内存位置。这意味着,当您创建一个变量时,就会在内存中保留一些空间。您可能需要存储各种数据类型的信息,操作系统会根据变量的数据类型,来分配内存和决定在保留内存中存储什么
    2022-04-04
  • 一篇文章带你了解C语言文件操作中的几个函数

    一篇文章带你了解C语言文件操作中的几个函数

    这篇文章主要介绍了使用C语言操作文件的基本函数整理,包括创建和打开以及关闭文件的操作方法,需要的朋友可以参考下,希望能够给你带来帮助
    2021-09-09
  • C++结合QT实现带有优先级的计算器功能

    C++结合QT实现带有优先级的计算器功能

    这篇文章主要介绍了C++结合QT实现带有优先级的计算器,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01

最新评论