I inserted the XML file into the QTreeWidget. And I want to show all the elements without one special child.
This is what the XML file looks like:
<dir name="Work space" id="directory_0">
<dir name="Directory 1" id="directory_1">
<document name="Document 1" id="document_1_1">
<slot name="Slot 1" id="slot_1_1_1"></slot>
<slot name="Slot 2" id="slot_1_1_2"></slot>
<slot name="Slot 3" id="slot_1_1_3"></slot>
<slot name="Slot 4" id="slot_1_1_4"></slot>
</document>
<document name="Document 2" id="document_1_2"></document>
<document name="Document 3" id="document_1_3"></document>
<document name="Document 4" id="document_1_4">
<slot name="Slot 1" id="slot_1_4_1"></slot>
<slot name="Slot 2" id="slot_1_4_2"></slot>
<slot name="Slot 3" id="slot_1_4_3"></slot>
</document>
<document name="Document 5" id="document_1_5"></document>
<document name="Document 6" id="document_1_6"></document>
</dir>
<dir name="Directory 2" id="directory_2">
<document name="Document 1" id="document_2_1"></document>
<document name="Document 2" id="document_2_2"></document>
<document name="Document 3" id="document_2_3"></document>
<dir name="Directory 3" id="directory_3"></dir>
<dir name="Directory 4" id="directory_4"></dir>
</dir>
</dir>
And my current QTreeWidget looks like this: IMAGE QTREEWIDGET
But I want it to look like this without modifying the XML file: Image how I want it to look
And I don't know how to put a different icon for a directory element than an element called a document. For example, element that is has one icon and the element has another icon.
This is my code to display the XML file in QTreeWidget:
def displayTree(tree,childs):
for child in childs:
branch = QTreeWidgetItem([child.attrib.get("id")])
branch.setIcon(0, QtGui.QIcon("resources/icons/document.png"))
if(child.find("slot")):
print("slot")
tree.addChild(branch)
tree.setIcon(0, QtGui.QIcon("resources/icons/folder.png"))
displayTree(branch, child)
self.iface.list_view.hide()
displayTree(tree, fileOpen)
I added this piece of code above the branch if(child.findall("slot")):
, and now it doesn't show the elements it contains in the slot.
def displayTree(tree,childs):
for child in childs:
if(child.findall("slot")):
print("slot")
else:
branch = QTreeWidgetItem([child.attrib.get("id")])
branch.setIcon(0, QtGui.QIcon("resources/icons/document.png"))
tree.addChild(branch)
tree.setIcon(0, QtGui.QIcon("resources/icons/folder.png"))
displayTree(branch, child)
self.iface.list_view.hide()
displayTree(tree, fileOpen)
After adding the code, the element containing the slot is not visible
CodePudding user response:
You should add children only if child.findall("slot")
doesn't return a truthfully value; in that case you can go on with the recursive call to displayTree
and set the icon as a folder, otherwise you can assume it's a document and set the relative icon:
def displayTree(tree,childs):
for child in childs:
branch = QTreeWidgetItem([child.attrib.get("id")])
tree.addChild(branch)
if child.findall("slot"):
branch.setIcon(0, QtGui.QIcon("resources/icons/document.png"))
else:
branch.setIcon(0, QtGui.QIcon("resources/icons/folder.png"))
displayTree(branch, child)
self.iface.list_view.hide()
displayTree(tree, fileOpen)