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++实现LeetCode(134.加油站问题)

    C++实现LeetCode(134.加油站问题)

    这篇文章主要介绍了C++实现LeetCode(134.加油站问题),本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-07-07
  • 判断指定的进程或程序是否存在方法小结(vc等)

    判断指定的进程或程序是否存在方法小结(vc等)

    VC判断进程是否存在?比如我想知道记事本是否运行,要用到哪些函数等实例,需要的朋友可以参考下
    2013-01-01
  • c++中string类成员函数c_str()的用法

    c++中string类成员函数c_str()的用法

    c_str()函数返回一个指向正规c字符串的指针,内容和string类的本身对象是一样的,通过string类的c_str()函数能够把string对象转换成c中的字符串的样式
    2013-09-09
  • 详解C++ Qt中堆叠窗体的使用案例

    详解C++ Qt中堆叠窗体的使用案例

    这篇文章主要为大家详细介绍了C++ Qt中堆叠窗体的使用案例,文中的示例代码讲解详细,对我们学习QT有一定的帮助,感兴趣的小伙伴可以了解一下
    2023-08-08
  • C/C++实现遍历文件夹最全方法总结

    C/C++实现遍历文件夹最全方法总结

    这篇文章主要为大家介绍了C/C++实现遍历文件夹功能的最全方法总结,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下
    2022-09-09
  • 使用C语言实现12种排序方法

    使用C语言实现12种排序方法

    这篇文章主要介绍了用C语言完整实现12种排序方法,本文给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-12-12
  • c语言根据用户输入的出生年份并计算出当前年龄

    c语言根据用户输入的出生年份并计算出当前年龄

    这篇文章主要介绍了c语言根据用户输入的出生年份并计算出当前年龄,需要的朋友可以参考下
    2023-03-03
  • C++ 反汇编之关于Switch语句的优化措施

    C++ 反汇编之关于Switch语句的优化措施

    这篇文章主要介绍了C++ 反汇编之关于Switch语句的优化措施,利用三种优化来降低树高度,谁的效率高就优先使用谁,三种优化都无法匹配才会使用判定树,具体内容详情跟随小编一起看看吧
    2022-01-01
  • 用C语言实现简单的三子棋

    用C语言实现简单的三子棋

    这篇文章主要为大家详细介绍了用C语言实现三子棋,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • linux c模拟ls命令详解

    linux c模拟ls命令详解

    本篇文章是对linux中基于c模拟ls命令的实现方法进行了详细的分析介绍,需要的朋友参考下
    2013-06-06

最新评论