Title:
//==>qpasswordlineedit.h#ifndef Qpasswordlineedit_h#defineQpasswordlineedit_h#include<QLineEdit>classQpasswordlineedit: PublicQlineedit{q_object Public: Qpasswordlineedit (Qwidget*parent,intTimeout = -);~Qpasswordlineedit ();PrivateSlots:voidDisplaypasswordaftereditslot (int,int);voidDisplaypasswordslot ();voidGetrealtextslot (Constqstring&); Public: QString GetPassword ()Const;voidSetTimeout (intmsec);intGetTimeout ()Const; Private: QString getmaskstring ();Private:intm_timeout; QString M_lineedittext;intM_lastcharcount;}; #endif //Qpasswordlineedit_h ///===> QPasswordLineEdit.cpp#include"qpasswordlineedit.h"#include<QTimer>Qpasswordlineedit::qpasswordlineedit (Qwidget*parent,inttimeout): 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 (Constqstring&)), This, SLOT (Getrealtextslot (Constqstring&)));} Qpasswordlineedit::~Qpasswordlineedit () {}voidQpasswordlineedit::D Isplaypasswordaftereditslot (intOldpos,intNewpos) {if(oldpos>=0&& newpos>=0 ){if(newpos>Oldpos) Qtimer::singleshot (M_timeout, This, SLOT (Displaypasswordslot ()));Else This-setcursorposition (oldpos);} }voidQpasswordlineedit::D isplaypasswordslot () { This-SetText (getmaskstring ());}voidQpasswordlineedit::getrealtextslot (Constqstring&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{returnM_lineedittext;}voidQpasswordlineedit::settimeout (intmsec) {M_timeout=msec;}intQpasswordlineedit::gettimeout ()Const{returnM_timeout;} QString qpasswordlineedit::getmaskstring () {QString mask="";intCount = This-text (). length ();if(count>0){ for(intI=0; i<count;i++) Mask+="*";}returnMask;}
The current password plaintext can be obtained by GetPassword (); Transferred from: http://zhouzhenren163.blog.163.com/blog/static/65499281201432604737176/
Qt---Display clear text first when you enter a password in Qlineedit, and then display the * number