2

我目前正在尝试做的是采用在底部子级具有复选框的填充树(qtreewidget),如果选中该框,则将路径文本返回给子级。我想这样做的原因是,如果检查了一个孩子,它将更改字典中键中的值。(创建树的“原始”字典)。这是我的意思的视觉示例:

  1. 从用户输入和服务器目录爬取中,我们填充了一个看起来像这样的树:(只有最低级别的子项有复选框。)对于可怕的树图,我们深表歉意!希望这是有道理的...

已编辑

学习

- 主题 1

- 日期

- -[]C

---[]d

---[]e

- 主题 2

- 日期

- -[]G

- -[]H

  1. 如果有人检查(例如)“g”级别的孩子,那么无论如何都会以[1,B,g]或1-Bg或1/B/g等形式获取“g”的路径.?

  2. 其中一个子级别(例如示例 A 和 B)也设置为用户可编辑。所以我需要来自树的信息,而不是最初填充树的信息。

我尝试打印 self.ui.treeWidget 索引,但没有真正获得我想要的东西。我觉得好像有一个简单的解决方案,但我似乎无法找到它。希望有人可以提供帮助!

实际代码片段:

    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')
4

1 回答 1

3

您所需要的只是一种方法,该方法沿着父/子链向上抓取每个项目的文本,直到父项为None

def getTreePath(self, item):
    path = []
    while item is not None:
        path.append(str(item.text(0)))
        item = item.parent()
    return '/'.join(reversed(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_())
于 2014-03-15T06:23:57.400 回答