'Save expanded items in a dict'

def save_state(self, item):

    result = {item.name: {}}
    if item.isExpanded():
        for i in range(item.childCount()):
            child = item.child(i)
            result[item.name].update(self.save_state(child))
    return result
