Created
December 11, 2015 12:16
-
-
Save JokerMartini/1c4c999f261d0478f6b2 to your computer and use it in GitHub Desktop.
PySide + Python: Gets the selection level and items for a QTreeWidget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
- 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