简体   繁体   English

Python (PyQT):如何在没有最后一个子级的 QTreeWidget 中插入 XML 文件

[英]Python (PyQT): How to insert a XML file in QTreeWidget without the last child

I inserted the XML file into the QTreeWidget.我将 XML 文件插入到 QTreeWidget 中。 And I want to show all the elements without one special child.我想展示没有一个特殊孩子的所有元素。

This is what the XML file looks like:这是 XML 文件的样子:

<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我当前的 QTreeWidget 看起来像这样: IMAGE QTREEWIDGET

But I want it to look like this without modifying the XML file: Image how I want it to look但我希望它在不修改 XML 文件的情况下看起来像这样:图像我希望它看起来如何

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:这是我在 QTreeWidget 中显示 XML 文件的代码:

    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.我在分支if(child.findall("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添加代码后,包含slot的元素不可见

You should add children only if child.findall("slot") doesn't return a truthfully value;只有当child.findall("slot")没有返回真实值时,您才应该添加孩子; 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:在这种情况下,您可以 go 使用对displayTree的递归调用并将图标设置为文件夹,否则您可以假设它是一个文档并设置相关图标:

        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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM