Qt实现密码显示按钮

本文实例为大家分享了Qt实现密码显示按钮的具体代码,供大家参考,具体内容如下

PasswordLineEdit.h

#ifndef PASSWORDLINEEDIT_H
#define PASSWORDLINEEDIT_H

#include <QAction>
#include <QLineEdit>
#include <QToolButton>

class PasswordLineEdit : public QLineEdit {
public:
  PasswordLineEdit(QWidget *parent = nullptr);
private slots:
  void onPressed();
  void onReleased();

protected:
  void enterEvent(QEvent *event);
  void leaveEvent(QEvent *event);
  void focusInEvent(QFocusEvent *event);
  void focusOutEvent(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);
}

void PasswordLineEdit::onPressed()
{
    QToolButton *button = qobject_cast<QToolButton *>(sender());
    button->setIcon(QIcon(":/eyeOn"));
    setEchoMode(QLineEdit::Normal);
}

void PasswordLineEdit::onReleased()
{
    QToolButton *button = qobject_cast<QToolButton *>(sender());
    button->setIcon(QIcon(":/eyeOff"));
    setEchoMode(QLineEdit::Password);
}

void PasswordLineEdit::enterEvent(QEvent *event)
{
    button->show();
    QLineEdit::enterEvent(event);
}

void PasswordLineEdit::leaveEvent(QEvent *event)
{
    button->hide();
    QLineEdit::leaveEvent(event);
}

void PasswordLineEdit::focusInEvent(QFocusEvent *event)
{
    button->show();
    QLineEdit::focusInEvent(event);
}

void PasswordLineEdit::focusOutEvent(QFocusEvent *event)
{
    button->hide();
    QLineEdit::focusOutEvent(event);
}

main.cpp

#include "passwordlineedit.h"

#include <QApplication>
#include <QFormLayout>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWidget w;
    PasswordLineEdit *w1 = new PasswordLineEdit;
    QLineEdit *w2 = new QLineEdit;
    QFormLayout *lay = new QFormLayout(&w);
    lay->addRow("PasswordLineEdit: ", w1);
    lay->addRow("QLineEdit: ", w2);
    w.show();

    return a.exec();
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

(0)

相关推荐

  • pyqt 实现QlineEdit 输入密码显示成圆点的方法

    使用自带的函数就可以实现: lineEdit.setEchoMode(QLineEdit.Password) import struct from PyQt5.QtWidgets import (QMessageBox, QLineEdit) from PyQt5.QtGui import QPixmap {...} class LoginWindow(QtWidgets.QWidget): {...} def __init__(self): super(LoginWindow, self)._

  • pyqt5 lineEdit设置密码隐藏,删除lineEdit已输入的内容等属性方法

    self.lineEdit.setEchoMode(QLineEdit.Password) 设置密码隐藏 self.lineEdit.setClearButtonEnabled(True) 设置对输入内容的删除提示 self.lineEidt.setFixedSize() 总的设置控件大小 self.lineEdit.setFixedWidth() 设置宽度 self.lineEdit.setFixedHeight() 设置高度 self.lineEidt.setFrame(False) 设置无

  • Qt编写显示密码强度的控件

    本文实例为大家分享了Qt编写显示密码强度控件的具体代码,供大家参考,具体内容如下 代码: #ifndef WIDGET_H #define WIDGET_H   #include <QWidget> #include <QRegularExpression> #include <QTimer>   class PasswordStrengthCheck : public QWidget {     Q_OBJECT   public:     PasswordStren

  • Qt实现密码框

    本文实例为大家分享了Qt实现密码框的具体代码,供大家参考,具体内容如下 密码输入框 支持无可选择,不可复制,粘贴,可查看密码,全清除功能 环境 Qt5.6.2+ Vs2013 效果 代码 QPasswdLineEdit类 PasswdLineEdit.h #ifndef PASSWDLINEEDIT_H #define PASSWDLINEEDIT_H #include <QLineEdit> #include <QPushButton> class QPasswdLineEdit

  • Qt实现密码显示按钮

    本文实例为大家分享了Qt实现密码显示按钮的具体代码,供大家参考,具体内容如下 PasswordLineEdit.h #ifndef PASSWORDLINEEDIT_H #define PASSWORDLINEEDIT_H #include <QAction> #include <QLineEdit> #include <QToolButton> class PasswordLineEdit : public QLineEdit { public:   Password

  • vue实现密码显示与隐藏按钮的自定义组件功能

    思路 实现该组件有两个思路,一个使用v-model进行双向绑定,一个是使用prop的.sync修饰符.父子组件通信. 1.v-model形式 v-model的实现需要在自定义组件中自定义一个inputvalue值,因为vue中的父子组件传递机制问题,在组件中直接修改props注入的key字段时,vue会给出错误.子组件中定义inputvalue字段,用于子组件中input元素的值的双向绑定,子组件中的input数据绑定可以使用v-model,也可以使用实现v-model的原理语法糖,下面两种方式

  • Android 密码 显示与隐藏功能实例

    效果: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android

  • jQuery通过改变input的type属性实现密码显示隐藏切换功能

    一般我们做登录注册的时候都会提供一个让用户选择自己输入的密码是否显示的需求, 这种需求我们肯定会想到只要动态改变input的type属性不就好了(text显示/password隐藏): 于是我用了$(''#id).attr('type', 'password')这个API 然而结果并不是我想的那样,出错了 HTML 代码 Uncaught Error: type property can't be changed 意思大概就是这个属性不能被修改. 于是我就googl一手. 然而我得到的结果是这样

  • Android中实现EditText密码显示隐藏的方法

    在Google发布了support:design:23+以后我们发现有这么一个东西TextInputLayout,先看下效果图: <android.support.design.widget.TextInputLayout android:id="@+id/pwdLayout" android:layout_width="match_parent" android:layout_height="wrap_content" app:passw

  • vue实现密码显示隐藏切换功能

    先给大家分享效果图: 具体实现代码如下所示: html: <group> <span>设置密码</span> <x-input :type="this.registration_data.pwdType" placeholder="请填写密码" @on-change="password"></x-input> <img :src="this.registration_da

  • 微信小程序实现单个卡片左滑显示按钮并防止上下滑动干扰功能

    实现类似ios端微信的左滑显示置顶.删除按钮的功能,首先需将按钮部分设为绝对定位,并且right设为负值溢出屏幕.利用小程序事件处理的api,分别读取触摸开始,触摸移动时,触摸结束的X/Y坐标,根据差值来改变整个卡片的位置. 这里有一个细节,为了防止按钮的点击干扰到卡片主部分的滑动,需要将整个卡片分为两部分. <View style={`${positionStyle[index]}`}> <View onTouchStart={this.moveTaskStart} onTouchMo

  • 原生JS实现顶部导航栏显示按钮+搜索框功能

    根据点击导航栏,搜索框的内容会随之变动 话不多说,先上图 HTML代码段 <div class="bar"> <ul id="menu" class="menu"> <li class="active">搜店</li> <li class="gradient">地址</li> <li class="gradient&qu

  • vue+elementUi 实现密码显示/隐藏+小图标变化功能

    vue+elementUi 实现密码显示/隐藏+小图标变化(js一共三行代码,其中一行为了美观)...,先给大家展示下效果图,感觉不错可以参考实现代码. [效果图] [html] // 前后代码[略] <el-form-item label="密码" prop="pwd"> <el-input v-model="ruleForm.pwd" :type="pwdType" placeholder="请

  • Qt实现可拖动按钮

    本文实例为大家分享了Qt实现可拖动按钮的具体代码,供大家参考,具体内容如下 直接上代码 self-contained.h #ifndef SELFCONTAINED_H #define SELFCONTAINED_H #include <QWidget> #include <QPainter> #include <QTimer> #include <QImage> #include <QMouseEvent> #include <QVect

随机推荐