Qt如何实现密码显示按钮(qt,开发技术)

时间:2024-05-04 00:40:37 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

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

Qt如何实现密码显示按钮

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:Qt如何实现密码显示按钮的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:JavaScript判断是否为数字的方式有哪些下一篇:

18 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18