欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2022年06月14日 14:08:13   作者:友善啊,朋友  
這篇文章主要為大家詳細介紹了Qt編寫顯示密碼強度的控件,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

代碼:

#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();
}

效果:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • C語言數(shù)據(jù)結(jié)構(gòu)二叉樹簡單應(yīng)用

    C語言數(shù)據(jù)結(jié)構(gòu)二叉樹簡單應(yīng)用

    這篇文章主要介紹了C語言數(shù)據(jù)結(jié)構(gòu)二叉樹簡單應(yīng)用的相關(guān)資料,需要的朋友可以參考下
    2017-05-05
  • 利用C++單例模式實現(xiàn)高性能配置管理器

    利用C++單例模式實現(xiàn)高性能配置管理器

    這篇文章主要為大家詳細介紹了如何利用C++單例模式實現(xiàn)高性能配置管理器,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-04-04
  • C++超詳細分析順序表

    C++超詳細分析順序表

    程序中經(jīng)常需要將一組數(shù)據(jù)元素作為整體管理和使用,需要創(chuàng)建這種元素組,用變量記錄它們,傳進傳出函數(shù)等。一組數(shù)據(jù)中包含的元素個數(shù)可能發(fā)生變化,順序表則是將元素順序地存放在一塊連續(xù)的存儲區(qū)里,元素間的順序關(guān)系由它們的存儲順序自然表示
    2022-03-03
  • C++超詳細介紹模板

    C++超詳細介紹模板

    人們需要編寫多個形式和功能都相似的函數(shù),因此有了函數(shù)模板來減少重復(fù)勞動;人們也需要編寫多個形式和功能都相似的類,于是 C++ 引人了類模板的概念,編譯器從類模板可以自動生成多個類,避免了程序員的重復(fù)勞動
    2022-07-07
  • MFC繪制不規(guī)則窗體的方法

    MFC繪制不規(guī)則窗體的方法

    這篇文章主要介紹了MFC繪制不規(guī)則窗體的方法,涉及MFC窗體操作的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • 淺談C++內(nèi)存分配及變長數(shù)組的動態(tài)分配

    淺談C++內(nèi)存分配及變長數(shù)組的動態(tài)分配

    下面小編就為大家?guī)硪黄獪\談C++內(nèi)存分配及變長數(shù)組的動態(tài)分配。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-09-09
  • C++超集C++/CLI模塊的基本語法

    C++超集C++/CLI模塊的基本語法

    這篇文章介紹了C++超集C++/CLI模塊的基本語法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • C++實現(xiàn)打印虛函數(shù)表的地址

    C++實現(xiàn)打印虛函數(shù)表的地址

    對于存在虛函數(shù)的類,如何打印虛函數(shù)表的地址,并利用這個虛函數(shù)表的地址來執(zhí)行該類中的虛函數(shù)呢,下面小編就來和大家一起簡單聊聊吧
    2023-07-07
  • VC實現(xiàn)A進程窗口嵌入到B進程窗口中顯示的方法

    VC實現(xiàn)A進程窗口嵌入到B進程窗口中顯示的方法

    這篇文章主要介紹了VC實現(xiàn)A進程窗口嵌入到B進程窗口中顯示的方法,對于理解windows程序運行原理的進程問題有一定的幫助,需要的朋友可以參考下
    2014-07-07
  • Windows進程崩潰問題的定位方法

    Windows進程崩潰問題的定位方法

    這篇文章主要介紹了Windows進程崩潰問題的定位方法,本文使用nstd工具進行進程崩潰時內(nèi)存和堆棧轉(zhuǎn)儲的方法查明問題所在,需要的朋友可以參考下
    2015-04-04

最新評論