Qt如何实现密码显示按钮
导读:本文共1381.5字符,通常情况下阅读需要5分钟。同时您也可以点击右侧朗读,来听本文内容。按键盘←(左) →(右) 方向键可以翻页。
摘要: PasswordLineEdit.h#ifndefPASSWORDLINEEDIT_H#definePASSWORDLINEEDIT_H#include<QAction>#include<QLineEdit>#include<QToolButton>classPasswordLineEdit:publicQLin... ...
音频解说
目录
(为您整理了一些要点),点击可以直达。PasswordLineEdit.h
#ifndefPASSWORDLINEEDIT_H#definePASSWORDLINEEDIT_H#include<QAction>#include<QLineEdit>#include<QToolButton>classPasswordLineEdit:publicQLineEdit{public:PasswordLineEdit(QWidget*parent=nullptr);privateslots:voidonPressed();voidonReleased();protected:voidenterEvent(QEvent*event);voidleaveEvent(QEvent*event);voidfocusInEvent(QFocusEvent*event);voidfocusOutEvent(QFocusEvent*event);private:QToolButton*button;};#endif//PASSWORDLINEEDIT_H
PasswordLineEdit.cpp
#include"passwordlineedit.h"PasswordLineEdit::PasswordLineEdit(QWidget*parent):QLineEdit(parent){setEchoMode(QLineEdit::Password);QAction*action=addAction(QIcon(":/eyeOff"),QLineEdit::TrailingPosition);button=qobject_cast<QToolButton*>(action->associatedWidgets().last());button->hide();button->setCursor(QCursor(Qt::PointingHandCursor));connect(button,&QToolButton::pressed,this,&PasswordLineEdit::onPressed);connect(button,&QToolButton::released,this,&PasswordLineEdit::onReleased);}voidPasswordLineEdit::onPressed(){QToolButton*button=qobject_cast<QToolButton*>(sender());button->setIcon(QIcon(":/eyeOn"));setEchoMode(QLineEdit::Normal);}voidPasswordLineEdit::onReleased(){QToolButton*button=qobject_cast<QToolButton*>(sender());button->setIcon(QIcon(":/eyeOff"));setEchoMode(QLineEdit::Password);}voidPasswordLineEdit::enterEvent(QEvent*event){button->show();QLineEdit::enterEvent(event);}voidPasswordLineEdit::leaveEvent(QEvent*event){button->hide();QLineEdit::leaveEvent(event);}voidPasswordLineEdit::focusInEvent(QFocusEvent*event){button->show();QLineEdit::focusInEvent(event);}voidPasswordLineEdit::focusOutEvent(QFocusEvent*event){button->hide();QLineEdit::focusOutEvent(event);}
main.cpp
#include"passwordlineedit.h"#include<QApplication>#include<QFormLayout>intmain(intargc,char*argv[]){QApplicationa(argc,argv);QWidgetw;PasswordLineEdit*w1=newPasswordLineEdit;QLineEdit*w2=newQLineEdit;QFormLayout*lay=newQFormLayout(&w);lay->addRow("PasswordLineEdit:",w1);lay->addRow("QLineEdit:",w2);w.show();returna.exec();}
</div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:
Qt如何实现密码显示按钮的详细内容,希望对您有所帮助,信息来源于网络。