Skip to content

Instantly share code, notes, and snippets.

@JokerMartini
Created December 11, 2015 12:16
Show Gist options
  • Save JokerMartini/1c4c999f261d0478f6b2 to your computer and use it in GitHub Desktop.
Save JokerMartini/1c4c999f261d0478f6b2 to your computer and use it in GitHub Desktop.
PySide + Python: Gets the selection level and items for a QTreeWidget
"""
- return[0] true if all items selected are the same level in heirarchy tree
- return[1] unique list of all levels containing a item selected
- return[2] select items
"""
def get_treewidget_selection_level(treewidget):
items = treewidget.selectedItems()
levels = set()
for item in items:
level = 0
index = treewidget.indexFromItem(item, 0)
while index.parent().isValid():
index = index.parent()
level += 1
levels.add(level)
if len(levels) == 1:
return ( True, list(levels)[0], items )
else:
return ( False, list(levels), items )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment