QLineEdit实现可显示密码可隐藏密码

20 篇文章 0 订阅

一、效果图

 

 

password_hide.png

 password_hide.png

password_show.png

二、关键逻辑

QLineEditPassword.h 

#ifndef QLINEEDITPASSWORD_H
#define QLINEEDITPASSWORD_H

#include <QLineEdit>

class QLineEditPassword : public QLineEdit
{
    Q_OBJECT
public:
    explicit QLineEditPassword(QWidget *parent = nullptr);

signals:

public slots:
};

#endif // QLINEEDITPASSWORD_H

 QLineEditPassword.cpp

#include "QLineEditPassword.h"
#include <QFile>
#include <QHBoxLayout>
#include <QPushButton>

// 加载文件内容
inline bool LoadStyleFile(QString strFilePath, QString& strContent)
{
    QFile qssFile(strFilePath);
    qssFile.open(QFile::ReadOnly);
    if(!qssFile.isOpen())
    {
        return false;
    }

    strContent = qssFile.readAll();
    qssFile.close();

    return true;
}


QLineEditPassword::QLineEditPassword(QWidget *parent) : QLineEdit(parent)
{
    setEchoMode(QLineEdit::Password);

    QPushButton* button = new QPushButton();
    button->setCursor(Qt::PointingHandCursor);
    button->setCheckable(true);
    connect(button, &QPushButton::toggled, [this](bool checked) {
            if (checked)
            {
                setEchoMode(QLineEdit::Normal);
            }
            else
            {
                setEchoMode(QLineEdit::Password);
            }
        });

    QHBoxLayout* layout = new QHBoxLayout();
    layout->addStretch();
    layout->addWidget(button);
    layout->setContentsMargins(0, 0, 0, 0);
    setLayout(layout);

    QString strStyle;
    if (LoadStyleFile(":/QLineEditPasswordStyle.qss", strStyle))
    {
        setStyleSheet(strStyle);
    }
}

QLineEditPasswordStyle.qss

QLineEdit {
    font-family: "Microsoft YaHei";
    font-size: 12px;
    color: rgb(50, 50, 50);
    background-color: #f2f2f2;
    border: 1px solid #e5e5e5;
    border-radius: 4px;
    padding: 2px 2px;
    min-height: 18px;   /* 对应高度大概是24px,这里不知为何不能等价 */
    padding-right: 18px;
}

QLineEdit:hover{
    border: 1px solid #014099;
}

QLineEdit:focus{
    border: 1px solid #014099;
}

QLineEdit:hover{
    border: 1px solid #014099;
}

QLineEdit QPushButton {
    width:  16px;
    height: 16px;
    qproperty-flat: true;
    margin-right: 4px;
    border: none;
    border-width: 0;
    border-image: url(:/password_hide.png) 0 0 0 0 stretch stretch;
    background: transparent;
}

QLineEdit QPushButton::checked {
    border-image: url(:/password_show.png) 0 0 0 0 stretch stretch;
}



 

PyQt6 是 PyQt 的最新版本,它是一个用于创建图形用户界面 (GUI) 应用程序的强大工具包。PyQt6 兼容 Python 和 Qt 库,并提供了一个丰富的库供开发者构建复杂的应用程序。 为了在一个 PyQt6 的输入里设置密码显示隐藏功能,可以使用 `QLineEdit` 类,并结合自定义函数来改变文本显示模式。下面是一种实现这个功能的方法: 1. **导入必要的模块**: ```python from PyQt6.QtWidgets import QApplication, QLineEdit, QPushButton, QVBoxLayout, QWidget import sys ``` 2. **创建一个窗口并添加组件**: ```python class PasswordInputExample(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): layout = QVBoxLayout() # 创建一个密码输入 self.password_input = QLineEdit() self.password_input.setEchoMode(QLineEdit.EchoMode.Password) self.password_input.textChanged.connect(self.onTextChanged) # 创建一个按钮控制显示/隐藏密码的功能 show_button = QPushButton('显示') show_button.clicked.connect(self.toggle_password_visibility) layout.addWidget(self.password_input) layout.addWidget(show_button) self.setLayout(layout) def onTextChanged(self, text): """ 这个函数会在输入的内容变化时触发 """ pass def toggle_password_visibility(self): """ 切换密码输入显示模式 """ if self.password_input.echoMode() == QLineEdit.EchoMode.Password: self.password_input.setEchoMode(QLineEdit.EchoMode.Normal) else: self.password_input.setEchoMode(QLineEdit.EchoMode.Password) ``` 3. **运行应用程序**: ```python if __name__ == '__main__': app = QApplication(sys.argv) ex = PasswordInputExample() ex.show() sys.exit(app.exec()) ``` 在这个例子中,我们首先创建了一个 `QLineEdit` 对象,并将它的 `echoMode()` 设置为 `Password` 模式,这意味着默认状态下输入的内容将以星号表示。当按下 "显示" 按钮时,`toggle_password_visibility` 函数会检查当前的 `echoMode()` 并相应地更改其值。如果当前显示模式是 `Password`,则更改为 `Normal`,反之亦然,实现密码显示隐藏的功能。 ---
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值