python - Extract dictionary from QTreeWidget -


i managed create treewidget dictionary, want reverse engineer it, , want create dictionary qtreewidget. quite lost on how that, seems straight forward fail.

the idea parent items

for index in range(self.treewidget.toplevelitemcount()):         parent = self.treewidget.toplevelitem(index) 

from there on need recursively go through each sublevel parent.childcount(), if childcount valid, iterate on child.

but dont know how create dictionary that. please me out.. ideally this

"core.global_vars.arrunner_qmenu_object_name" "core.global_vars.metadata_label" "core.logger.console_verbosity" 

or in best case {"core:{"global_vars:{"arrunner_qmenu_object_name":none}}, ...}

enter image description here

update

replacejson main functions gets root parents, there on gather childrens , add them global children list. after that, generate string "root.child.child.child" can expand dict {"root":"{"child":...}}

then in replace json add string config.. there way easier way.. appreciated.

so came with:

def children(self, parent):     global children     childcount = parent.childcount()     if childcount:         index in range(childcount):             self.children(parent.child(index))     elif not parent.parent() , not parent.text(1):  # top levels without children         children.append(parent)     if parent.text(1):         children.append(parent)  def generatestring(self, treeitem):     def getparent(item):         if item.parent():             global parents             parents.append(str(item.parent().text(0)))             getparent(item.parent())     global parents     parents = [str(treeitem.text(0))]     getparent(treeitem)     attribute, value = '.'.join(parents[::-1]), treeitem.text(1)     return attribute, value  def _replacejson(self):     super(jsoneditor, self)._replacejson()     writetofile(self.configfile, "{}")     # getting children     global children     children = []     index in range(self.treewidget.toplevelitemcount()):         parent = self.treewidget.toplevelitem(index)         self.children(parent)     # generating config string     child in children:         attribute, value = self.generatestring(child)         self._addtoconfig(attribute, value)     self.log.info("updated config file: {}".format(os.path.basename(self.configfile)))     self.close() 


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -