我有一个包含密码的QstandardItem,即使在编辑该列时,我也希望将这个QstandardItem的值隐藏为密码或一堆******。
username = QStandardItem("%s" % ("username"))
password = QStandardItem("%s" % ("password"))
self.tbViewModel.appendRow([username, password])我希望用户在选择密码列和CTRL+C时能够复制实际密码。
有密码保护QStandardItem值的方法吗?
发布于 2022-04-15 22:00:44
您可以使用一个角色来指示它是一个密码和一个委托,它使用该信息来修改QLineEdit中的内容:
PasswordRole = Qt.UserRole + 1password_item = QStandardItem("password")
password_item.setData(True, PasswordRole)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 editorpassword_delegate = PasswordDelegate(your_view)
your_view.setItemDelegate(password_delegate)https://stackoverflow.com/questions/71888883
复制相似问题