Qt实现密码显示按钮

 更新时间:2022年06月14日 16:45:53   作者:虾球xz  
这篇文章主要为大家详细介绍了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();
}

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

相关文章

  • 一文掌握C++ const与constexpr及区别

    一文掌握C++ const与constexpr及区别

    C++ 11标准中,const 用于为修饰的变量添加“只读”属性而 constexpr关键字则用于指明其后是一个常量,编译器在编译程序时可以顺带将其结果计算出来,而无需等到程序运行阶段,这样的优化极大地提高了程序的执行效率,本文重点介绍C++ const与constexpr区别介绍,一起看看吧
    2024-02-02
  • OpenCV实现人脸识别简单程序

    OpenCV实现人脸识别简单程序

    这篇文章主要为大家详细介绍了OpenCV实现人脸识别简单程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • C++ const与constexpr区别小结

    C++ const与constexpr区别小结

    C++11标准中,const用于为修饰的变量添加只读属性,而constexpr关键字则用于指明其后是一个常量,本文主要介绍了C++ const与constexpr区别小结,感兴趣的可以了解一下
    2024-03-03
  • C++单例模式应用实例

    C++单例模式应用实例

    这篇文章主要介绍了C++单例模式应用实例,详细讲述了单例模式的原理与结构,及相关的打印机应用实例,需要的朋友可以参考下
    2014-10-10
  • C++动态规划中关于背包问题讲解

    C++动态规划中关于背包问题讲解

    可能有些读者有接触过动态规划,可能也有一些读者以前完全不知道动态规划这个东西,别担心,我这篇文章会为读者做一个入门,好让读者掌握这个重要的知识点
    2023-03-03
  • 详细分析C++ 多态和虚函数

    详细分析C++ 多态和虚函数

    这篇文章主要介绍了C++ 多态和虚函数的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-07-07
  • 基于C语言实现的aes256加密算法示例

    基于C语言实现的aes256加密算法示例

    这篇文章主要介绍了基于C语言实现的aes256加密算法,结合具体实例形式详细分析了C语言实现的aes256加密算法实现步骤与使用技巧,需要的朋友可以参考下
    2017-02-02
  • 由static_cast和dynamic_cast到C++对象占用内存的全面分析

    由static_cast和dynamic_cast到C++对象占用内存的全面分析

    下面小编就为大家带来一篇由static_cast和dynamic_cast到C++对象占用内存的全面分析。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-01-01
  • vscode+platformIO开发stm32f4的实现

    vscode+platformIO开发stm32f4的实现

    这篇文章主要介绍了vscode+platformIO开发stm32f4的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-05-05
  • C++构造函数一些常见的坑

    C++构造函数一些常见的坑

    这篇文章主要给大家分享的是C++构造函数一些常见的坑,文章围绕C++构造函数的相关资料展开关于C++构造函数坑的内容,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-01-01

最新评论