我有一个基本上相当于嵌套字典的数据结构。假设它看起来像这样:
standard_dict = {
'section1': {
'category1': np.array([1, 2, 3, 5]),
'category2': 2
},
'section2': {
'category1': [4, 5, 6],
'category2': {'category2a': {'t': 1}}
}
}
此外,我有一个带有 for 循环的递归函数,可以从此字典中生成嵌套的树视图。现在我试图在 for 循环中为每个值、键对添加一个索引。我使用 QModelindex 进行了尝试,但我没有达到代码执行我想要它的地步。
def add_object(self, v: object, prefix1: object) -> object:
if isinstance(v, dict):
for k, v2 in v.items():
prefix1.addChild(CustomNode(k))
prefix = prefix1.getlastChild()
self.add_object(v2, prefix)
elif isinstance(v, list):
for e in v:
prefix1.addChild(CustomNode(e))
有没有一种简单的方法来实现索引并将索引添加到值、键对?
我想要的结果是只更新 PyQt5 Gui 中更改的单个值,而不是整个 Gui。
如果有人想知道,这是我的整个项目。
""" 导入副本 从输入导入列表,任何 导入系统 从 PyQt5 导入 QtCore、QtWidgets、uic 将 numpy 导入为 np 从 deepdiff 导入 DeepDiff 从 lib.MainWindow 导入 Ui_MainWindow 类自定义节点(对象): def __init__(自我,数据): self._data = 数据 如果类型(数据)== 元组: self._data = 列表(数据) 如果 type(data) 是 str 或不是 hasattr(data, '__getitem__'): self._data = [数据] self._columncount = len(self._data) self._children = [] self._parent = 无 self._row = 0 def getlastChild(self): 返回 self._children[-1] 定义数据(自我,列): 如果列 >= 0 且列 < len(self._data): 返回 self._data[列] def columnCount(self): 返回 self._columncount def childCount(self): 返回 len(self._children) def 孩子(自我,行): 如果行 >= 0 并且行 < self.childCount(): 返回 self._children[row] 定义父母(自己): 返回self._parent 定义行(自我): 返回self._row def addChild(自我,孩子): child._parent = 自己 child._row = len(self._children) self._children.append(child) self._columncount = max(child.columnCount(), self._columncount) 类CustomModel(QtCore.QAbstractItemModel): def __init__(自我,节点): QtCore.QAbstractItemModel.__init__(self) self._root = CustomNode(无) 对于节点中的节点: self._root.addChild(节点) def rowCount(self, index: object) -> object: 如果 index.isValid(): 返回 index.internalPointer().childCount() 返回 self._root.childCount() def addChild(self, node, _parent): 如果不是 _parent 或不是 _parent.isValid(): 父母 = self._root 别的: 父 = _parent.internalPointer() parent.addChild(节点) def index(self, row, column, _parent=None): 如果不是 _parent 或不是 _parent.isValid(): 父母 = self._root 别的: 父 = _parent.internalPointer() 如果不是 QtCore.QAbstractItemModel.hasIndex(self, row, column, _parent): 返回 QtCore.QModelIndex() 孩子=父母。孩子(行) 如果是孩子: return QtCore.QAbstractItemModel.createIndex(self, row, column, child) 别的: 返回 QtCore.QModelIndex() def 父母(自我,索引): 如果 index.isValid(): p = index.internalPointer().parent() 如果 p: 返回 QtCore.QAbstractItemModel.createIndex(self, p.row(), 0, p) 返回 QtCore.QModelIndex() def columnCount(self, index): 如果 index.isValid(): 返回 index.internalPointer().columnCount() 返回 self._root.columnCount() def 数据(自我、索引、角色): 如果不是 index.isValid(): 返回无 节点 = index.internalPointer() 如果角色 == QtCore.Qt.DisplayRole: 返回 node.data(index.column()) 返回无 类我的树(): def __init__(self, ref_dict, treeView): self.items = [] self.ref_dict = ref_dict self.items = [] self.index_dict = ref_dict '''Erzeugt eine List aus den Hauptitems''' 对于 self.ref_dict 中的键: self.items += [CustomNode(key)] self.add_object(self.ref_dict[key], prefix1=self.items[-1]) '''Aufruf der View um die Anzeige darzustellen''' # self.tw = QtWidgets.QTreeView() self.tw = 树视图 self.tw.setModel(CustomModel(self.items)) #referenz auf das Modell, gibt das Modell zurück 打印(self.tw.model()) '''功能 um das Dictionary zu iterieren und in einer Treeview darzustellen''' def add_object(self, v: object, prefix1: object) -> object: 如果是实例(v,dict): 对于 v.items() 中的 k,v2: prefix1.addChild(CustomNode(k)) 前缀 = prefix1.getlastChild() self.add_object(v2, 前缀) elif isinstance(v,列表): 对于 v 中的 e: prefix1.addChild(CustomNode(e)) elif isinstance(v, type(np.array([]))): [prefix1.addChild(CustomNode(int(i))) for i in v] 别的: prefix1.addChild(CustomNode(v)) 类比较(): def __init__(self, standard_dict): self.__standard_dict = copy.deepcopy(standard_dict) @财产 def 标准字典(自我): 返回自我.__standard_dict @standard_dict.setter def 标准字典(自我,标准字典): self.__standard_dict = 标准字典 # Signal auslösen mit kompletten Update vom Model '''Funktion um mit DeepDiff die beiden Dictionaries zu vergleichen''' 定义比较(自我,new_dict): 返回 DeepDiff(self.__standard_dict, new_dict) 类 MainWindow(QtWidgets.QMainWindow): def __init__(self): 超级(主窗口,自我).__init__() self.ui = Ui_MainWindow() self.ui.setupUi(自我) mytree = MyTree(standard_dict, self.ui.treeView) mytree_2 = MyTree(standard_dict, self.ui.treeView_2) self.ui.pushButton.clicked.connect(self.compare_dict) self.comp = 比较(standard_dict=standard_dict) def compare_dict(self): 标准字典['section1']['category2'] += 1 diff = self.comp.compare(standard_dict) self.ui.plainTextEdit.insertPlainText(str(standard_dict)) self.ui.plainTextEdit.insertPlainText(str(diff)) #mytree_3 = MyTree(standard_dict, self.ui.treeView_2) #nur die Stelle des geänderten Indexes updaten und keine neue View erstellen 如果 __name__ == "__main__": 标准字典 = { “第 1 节”:{ 'category1': np.array([1, 2, 3, 5]), “类别2”:2 }, “第 2 节”:{ 'category1': [4, 5, 6], 'category2': {'category2a': {'t': 1}} } } 应用程序 = QtWidgets.QApplication(sys.argv) 窗口 = 主窗口() 窗口.show() sys.exit(app.exec_())
非常感谢提前