PyQt4 Qtreewidget - get hierarchy text if child ch

2019-05-27 04:45发布

问题:

What I am currently trying to do is take a populated tree (qtreewidget) that has checkboxes at the bottom child level, and return the text of the path to the child if the box is checked. The reason I want to do this, is if a child is checked, it will then change a value in a key in a dictionary. (The "raw" dictionary the tree was created from). Here's a visual example of what I mean:

  1. From user input and server directory crawling, we have populated a tree that looks something like this: (Only the lowest level child items have checkboxes.) And sorry for the horrible tree diagram!! Hopefully it makes sense...

edited

study

-subject 1

--date

---[]c

---[]d

---[]e

-subject 2

--date

---[]g

---[]h

  1. If someone checks (for example) the "g" levle child, is there anyway to then get the path to "g" in a form something like [1, B, g] or 1-B-g or 1/B/g, etc.?

  2. One of the children levels (let's say in the example A and B) are also set to be user editable. So I'd need the info from the tree, not the info the tree was originally populated from.

I have tried printing self.ui.treeWidget indexes with no real luck in getting what I want. I feel as though there is an easy solution for this, but I can't seem to find it. Hopefully someone can help!

Actual Code Snippet:

    for h,study in enumerate(tree_dict['study']):
        study_name = study['study_name']
        treeSTUDY = QtGui.QTreeWidgetItem(self.ui.treeWidget, [study_name])
        treeSTUDY.setFlags(QtCore.Qt.ItemIsEnabled)
        self.ui.treeWidget.expandItem(treeSTUDY)            

        for i,subject in enumerate(study['subject']):
            subject = subject['ID']
            treeSUBJECT = QtGui.QTreeWidgetItem(treeSTUDY, [subject_id])
            treeSUBJECT.setFlags(QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled)

            for j,visit in enumerate(subject['visit']):
                scan_date = visit['date']                     
                treeDATE = QtGui.QTreeWidgetItem(treeSUBJECT, [scan_date[4:6])
                treeDATE.setFlags(QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled)

                for k,ser in enumerate(visit['series']):
                    s_name = ser['name'] + '-' + ser['description']
                    count =  str(ser['count'])
                    treeSCAN = QtGui.QTreeWidgetItem(treeDATE)
                    treeSCAN.setFlags(QtCore.Qt.ItemIsEditable | QtCore.Qt.ItemIsEnabled | QtCore.Qt.ItemIsUserCheckable)
                    treeSCAN.setCheckState(0, QtCore.Qt.Unchecked)   
                    treeSCAN.setText(0, s_name)
                    treeSCAN.setText(1, ser['time'])
                    treeSCAN.setText(2, ser['number'])
                    treeSCAN.setText(3, 'count')

回答1:

All you need is a method that walks up the parent/child chain grabbing the text of each item until the parent is None:

def getTreePath(self, item):
    path = []
    while item is not None:
        path.append(str(item.text(0)))
        item = item.parent()
    return '/'.join(reversed(path))

UPDATE:

Here is a demo script that shows how to get the checked item and retrieve its path:

from PyQt4 import QtCore, QtGui

class Window(QtGui.QWidget):
    def __init__(self):
        QtGui.QWidget.__init__(self)
        self.tree = QtGui.QTreeWidget(self)
        self.tree.setHeaderHidden(True)
        for index in range(2):
            parent = self.addItem(self.tree, 'Item%d' % index)
            for color in 'Red Green Blue'.split():
                subitem = self.addItem(parent, color)
                for letter in 'ABC':
                    self.addItem(subitem, letter, True, False)
        layout = QtGui.QVBoxLayout(self)
        layout.addWidget(self.tree)
        self.tree.itemChanged.connect(self.handleItemChanged)

    def addItem(self, parent, text, checkable=False, expanded=True):
        item = QtGui.QTreeWidgetItem(parent, [text])
        if checkable:
            item.setCheckState(0, QtCore.Qt.Unchecked)
        else:
            item.setFlags(
                item.flags() & ~QtCore.Qt.ItemIsUserCheckable)
        item.setExpanded(expanded)
        return item

    def handleItemChanged(self, item, column):
        if item.flags() & QtCore.Qt.ItemIsUserCheckable:
            path = self.getTreePath(item)
            if item.checkState(0) == QtCore.Qt.Checked:
                print('%s: Checked' % path)
            else:
                print('%s: UnChecked' % path)

    def getTreePath(self, item):
        path = []
        while item is not None:
            path.append(str(item.text(0)))
            item = item.parent()
        return '/'.join(reversed(path))

if __name__ == '__main__':

    import sys
    app = QtGui.QApplication(sys.argv)
    window = Window()
    window.setGeometry(500, 300, 250, 450)
    window.show()
    sys.exit(app.exec_())