首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >密码保护QstandardItem pyqt5

密码保护QstandardItem pyqt5
EN

Stack Overflow用户
提问于 2022-04-15 20:57:21
回答 1查看 47关注 0票数 1

我有一个包含密码的QstandardItem,即使在编辑该列时,我也希望将这个QstandardItem的值隐藏为密码或一堆******

代码语言:javascript
复制
username = QStandardItem("%s" % ("username"))
password = QStandardItem("%s" % ("password"))

self.tbViewModel.appendRow([username, password])

我希望用户在选择密码列和CTRL+C时能够复制实际密码。

有密码保护QStandardItem值的方法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-15 22:00:44

您可以使用一个角色来指示它是一个密码和一个委托,它使用该信息来修改QLineEdit中的内容:

代码语言:javascript
复制
PasswordRole = Qt.UserRole + 1
代码语言:javascript
复制
password_item = QStandardItem("password")
password_item.setData(True, PasswordRole)
代码语言:javascript
复制
class PasswordDelegate(QStyledItemDelegate):
    def initStyleOption(self, option, index):
        super().initStyleOption(option, index)
        if index.data(PasswordRole):
            style = option.widget.style() or QApplication.style()
            hint = style.styleHint(QStyle.SH_LineEdit_PasswordCharacter)
            option.text = chr(hint) * len(option.text)

    def createEditor(self, parent, option, index):
        editor = super().createEditor(parent, option, index)
        if index.data(PasswordRole) and isinstance(editor, QLineEdit):
            editor.setEchoMode(QLineEdit.Password)
        return editor
代码语言:javascript
复制
password_delegate = PasswordDelegate(your_view)
your_view.setItemDelegate(password_delegate)
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71888883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档