首页 > 代码库 > Qt---实现在QLineEdit中输入密码时先显示明文,然后显示*号
Qt---实现在QLineEdit中输入密码时先显示明文,然后显示*号
如题:
//==>QPasswordLineEdit.h #ifndef QPASSWORDLINEEDIT_H#define QPASSWORDLINEEDIT_H #include <QLineEdit> class QPasswordLineEdit : public QLineEdit{Q_OBJECT public:QPasswordLineEdit(QWidget *parent,int timeout = 300);~QPasswordLineEdit(); private slots:void DisplayPasswordAfterEditSlot(int,int);void DisplayPasswordSlot();void GetRealTextSlot(const QString&); public:QString GetPassword() const;void SetTimeout(int msec);int GetTimeout() const; private:QString GetMaskString(); private:int m_Timeout;QString m_LineEditText;int m_LastCharCount;}; #endif // QPASSWORDLINEEDIT_H /// ===> QPasswordLineEdit.cpp #include "qpasswordlineedit.h"#include <QTimer> QPasswordLineEdit::QPasswordLineEdit(QWidget *parent,int timeout) : QLineEdit(parent){m_Timeout = timeout;m_LineEditText = "";m_LastCharCount = 0; connect(this,SIGNAL(cursorPositionChanged(int,int)),this,SLOT(DisplayPasswordAfterEditSlot(int,int)));connect(this,SIGNAL(textEdited(const QString&)),this,SLOT(GetRealTextSlot(const QString&)));} QPasswordLineEdit::~QPasswordLineEdit(){ } void QPasswordLineEdit::DisplayPasswordAfterEditSlot(int oldPos,int newPos){if(oldPos>=0 && newPos>=0 ){if(newPos>oldPos)QTimer::singleShot(m_Timeout,this,SLOT(DisplayPasswordSlot()));elsethis->setCursorPosition(oldPos);} }void QPasswordLineEdit::DisplayPasswordSlot(){this->setText(GetMaskString());}void QPasswordLineEdit::GetRealTextSlot(const QString& text){if(text.count()>m_LastCharCount)m_LineEditText.append(text.right(1));else if(text.count()<m_LastCharCount)m_LineEditText.remove(m_LineEditText.count()-1,1);m_LastCharCount = m_LineEditText.count();} QString QPasswordLineEdit::GetPassword() const{return m_LineEditText;}void QPasswordLineEdit::SetTimeout(int msec){m_Timeout = msec;}int QPasswordLineEdit::GetTimeout() const{return m_Timeout;} QString QPasswordLineEdit::GetMaskString(){QString mask = "";int count = this->text().length();if(count>0){for(int i=0;i<count;i++)mask += "*";}return mask;}
通过GetPassword()可以获取当前的密码明文;
转自:http://zhouzhenren163.blog.163.com/blog/static/65499281201432604737176/
Qt---实现在QLineEdit中输入密码时先显示明文,然后显示*号
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。