QTreeWidgetItems 和 QLineEdit 回声模式
QTreeWidgetItems and QLineEdit echomodes
重点:如何让 QTreeWidgetItem 遵守 QLineEdits setEchoMode(QLineEdit.Password)?
最后一天我一直在用头撞墙:
我有一个 QTreeWidgetItem 的子 class(它只是向 class 添加了一个额外的字段)
我创建了它的一个实例,将它添加到我的 TreeWidget 中:
twi = DIMTreeWidgetItem.DIMTreeWidgetItem(uuid.uuid4(), [field_name, '<Empty>'])
...
self.ui_instance.main_window.treeWidget.addTopLevelItem(twi)
我根据双击该项目编辑了一个实例:
self.ui_instance.main_window.treeWidget.editItem(项目,列)
这很好用。
我有一个代表附加到该列,它很简单:
def __init__(self, parent=None, *args):
QStyledItemDelegate.__init__(self, parent, *args)
def createEditor(self, parent, option, index):
le = QLineEdit('', parent)
le.setEchoMode(QLineEdit.PasswordEchoOnEdit)
return le
但这似乎只影响编辑期间的项目。我在编辑后隐藏treewidgetitem内容的正确方法是什么?
即使有人可以阐明如何在 C++ 中执行此操作,我也确信我可以将其转换为 pyside2
您必须将委托的 displayText 方法覆盖为 return 文本长度的 ●
字符:
from PySide2 import QtCore, QtWidgets
class PasswordDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QtWidgets.QLineEdit(parent)
editor.setEchoMode(QtWidgets.QLineEdit.PasswordEchoOnEdit)
return editor
def displayText(self, value, locale):
character = "●" # u"\u25CF"
v = character * len(value)
return super(PasswordDelegate, self).displayText(v, locale)
class EditableItem(QtWidgets.QTreeWidgetItem):
def __init__(self, *args, **kwargs):
super(EditableItem, self).__init__(*args, **kwargs)
self.setFlags(self.flags() | QtCore.Qt.ItemIsEditable)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
tree_widget = QtWidgets.QTreeWidget()
self.setCentralWidget(tree_widget)
for i in range(5):
parent_item = EditableItem(tree_widget, ["{}".format(i)])
for j in range(5):
child_item = EditableItem(["{}-{}".format(i, j)])
parent_item.addChild(child_item)
tree_widget.expandAll()
delegate = PasswordDelegate(tree_widget)
tree_widget.setItemDelegate(delegate)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
如果您想区分在哪个元素中应用,您可以覆盖 initStyleOption 方法,因为它具有与该项目关联的 QModelIndex 信息,在以下示例中,它将仅应用于具有父元素的元素。
class PasswordDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QtWidgets.QLineEdit(parent)
editor.setEchoMode(QtWidgets.QLineEdit.PasswordEchoOnEdit)
return editor
def initStyleOption(self, option, index):
super(PasswordDelegate, self).initStyleOption(option, index)
if index.parent().isValid():
character = "●" # u"\u25CF"
option.text = character * len(option.text)
重点:如何让 QTreeWidgetItem 遵守 QLineEdits setEchoMode(QLineEdit.Password)?
最后一天我一直在用头撞墙: 我有一个 QTreeWidgetItem 的子 class(它只是向 class 添加了一个额外的字段)
我创建了它的一个实例,将它添加到我的 TreeWidget 中:
twi = DIMTreeWidgetItem.DIMTreeWidgetItem(uuid.uuid4(), [field_name, '<Empty>'])
...
self.ui_instance.main_window.treeWidget.addTopLevelItem(twi)
我根据双击该项目编辑了一个实例:
self.ui_instance.main_window.treeWidget.editItem(项目,列) 这很好用。
我有一个代表附加到该列,它很简单:
def __init__(self, parent=None, *args):
QStyledItemDelegate.__init__(self, parent, *args)
def createEditor(self, parent, option, index):
le = QLineEdit('', parent)
le.setEchoMode(QLineEdit.PasswordEchoOnEdit)
return le
但这似乎只影响编辑期间的项目。我在编辑后隐藏treewidgetitem内容的正确方法是什么?
即使有人可以阐明如何在 C++ 中执行此操作,我也确信我可以将其转换为 pyside2
您必须将委托的 displayText 方法覆盖为 return 文本长度的 ●
字符:
from PySide2 import QtCore, QtWidgets
class PasswordDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QtWidgets.QLineEdit(parent)
editor.setEchoMode(QtWidgets.QLineEdit.PasswordEchoOnEdit)
return editor
def displayText(self, value, locale):
character = "●" # u"\u25CF"
v = character * len(value)
return super(PasswordDelegate, self).displayText(v, locale)
class EditableItem(QtWidgets.QTreeWidgetItem):
def __init__(self, *args, **kwargs):
super(EditableItem, self).__init__(*args, **kwargs)
self.setFlags(self.flags() | QtCore.Qt.ItemIsEditable)
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
tree_widget = QtWidgets.QTreeWidget()
self.setCentralWidget(tree_widget)
for i in range(5):
parent_item = EditableItem(tree_widget, ["{}".format(i)])
for j in range(5):
child_item = EditableItem(["{}-{}".format(i, j)])
parent_item.addChild(child_item)
tree_widget.expandAll()
delegate = PasswordDelegate(tree_widget)
tree_widget.setItemDelegate(delegate)
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.resize(640, 480)
w.show()
sys.exit(app.exec_())
如果您想区分在哪个元素中应用,您可以覆盖 initStyleOption 方法,因为它具有与该项目关联的 QModelIndex 信息,在以下示例中,它将仅应用于具有父元素的元素。
class PasswordDelegate(QtWidgets.QStyledItemDelegate):
def createEditor(self, parent, option, index):
editor = QtWidgets.QLineEdit(parent)
editor.setEchoMode(QtWidgets.QLineEdit.PasswordEchoOnEdit)
return editor
def initStyleOption(self, option, index):
super(PasswordDelegate, self).initStyleOption(option, index)
if index.parent().isValid():
character = "●" # u"\u25CF"
option.text = character * len(option.text)