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:
- 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
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.?
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')
All you need is a method that walks up the parent/child chain grabbing the text of each item until the parent is
None
:UPDATE:
Here is a demo script that shows how to get the checked item and retrieve its path: