如何在 QTreeWidget 中制作可编辑的值
How to make editable value in QTreeWidget
我有一个项目。我处理 json 文件。我在 python 中得到了排序。这是 TreeView,我想对 TreeView 进行更改。我如何编辑它的每个单元格。
import json
from PyQt5.QtWidgets import *
class ViewTree(QTreeWidget):
def __init__(self, value):
super().__init__()
def fill_item(item, value):
def new_item(parent, text, val=None):
child = QTreeWidgetItem([text])
fill_item(child, val)
parent.addChild(child)
child.setExpanded(True)
if value is None: return
elif isinstance(value, dict):
for key, val in sorted(value.items()):
new_item(item, str(key), val)
elif isinstance(value, (list, tuple)):
for val in value:
text = (str(val) if not isinstance(val, (dict, list, tuple))
else '[%s]' % type(val).__name__)
new_item(item, text, val)
else:
new_item(item, str(value))
fill_item(self.invisibleRootItem(), value)
if __name__ == '__main__':
app = QApplication([])
fname = QFileDialog.getOpenFileName()
json_file=open(fname[0],"r")
file=json.load(json_file)
window = ViewTree(file)
window.setGeometry(300, 100, 900, 600)
window.show()
app.exec_()
您必须启用 Qt::ItemIsEditable
标志:
from PyQt5.QtCore import Qt
# ...
child = QTreeWidgetItem([text])
child.setFlags(child.flags() | Qt.ItemIsEditable)
我有一个项目。我处理 json 文件。我在 python 中得到了排序。这是 TreeView,我想对 TreeView 进行更改。我如何编辑它的每个单元格。
import json
from PyQt5.QtWidgets import *
class ViewTree(QTreeWidget):
def __init__(self, value):
super().__init__()
def fill_item(item, value):
def new_item(parent, text, val=None):
child = QTreeWidgetItem([text])
fill_item(child, val)
parent.addChild(child)
child.setExpanded(True)
if value is None: return
elif isinstance(value, dict):
for key, val in sorted(value.items()):
new_item(item, str(key), val)
elif isinstance(value, (list, tuple)):
for val in value:
text = (str(val) if not isinstance(val, (dict, list, tuple))
else '[%s]' % type(val).__name__)
new_item(item, text, val)
else:
new_item(item, str(value))
fill_item(self.invisibleRootItem(), value)
if __name__ == '__main__':
app = QApplication([])
fname = QFileDialog.getOpenFileName()
json_file=open(fname[0],"r")
file=json.load(json_file)
window = ViewTree(file)
window.setGeometry(300, 100, 900, 600)
window.show()
app.exec_()
您必须启用 Qt::ItemIsEditable
标志:
from PyQt5.QtCore import Qt
# ...
child = QTreeWidgetItem([text])
child.setFlags(child.flags() | Qt.ItemIsEditable)